2020-06-09 11:48:49 +02:00
|
|
|
'use strict';
|
|
|
|
|
2020-06-08 15:13:26 +02:00
|
|
|
const _ = require('lodash');
|
2020-06-08 11:01:20 +02:00
|
|
|
const { yup } = require('strapi-utils');
|
|
|
|
const { validateRegisterProviderAction } = require('../validation/action-provider');
|
|
|
|
const { getActionId, createAction } = require('../domain/action');
|
|
|
|
|
2020-06-09 18:34:13 +02:00
|
|
|
const createActionProvider = () => {
|
2020-06-08 11:01:20 +02:00
|
|
|
const actions = new Map();
|
|
|
|
|
|
|
|
return {
|
2020-06-09 12:23:35 +02:00
|
|
|
/**
|
|
|
|
* Get an action
|
|
|
|
* @param {String} uid uid given when registering the action
|
|
|
|
* @param {String} pluginName name of the plugin which is related to the action
|
|
|
|
* @returns {Promise<Action>}
|
|
|
|
*/
|
2020-06-08 11:01:20 +02:00
|
|
|
get(uid, pluginName) {
|
|
|
|
const actionId = getActionId({ pluginName, uid });
|
2020-06-08 19:12:25 +02:00
|
|
|
const action = actions.get(actionId);
|
2020-06-08 15:13:26 +02:00
|
|
|
return _.cloneDeep(action);
|
2020-06-08 11:01:20 +02:00
|
|
|
},
|
2020-06-09 12:23:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all actions stored in an array
|
|
|
|
* @returns {Promise<Array<Action>>}
|
|
|
|
*/
|
2020-06-08 11:01:20 +02:00
|
|
|
getAll() {
|
2020-06-08 15:13:26 +02:00
|
|
|
return _.cloneDeep(Array.from(actions.values()));
|
|
|
|
},
|
2020-06-09 12:23:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all actions stored in a Map
|
|
|
|
* @returns {Promise<Map<uid, Action>>}
|
|
|
|
*/
|
2020-06-08 15:13:26 +02:00
|
|
|
getAllByMap() {
|
|
|
|
return _.cloneDeep(actions);
|
2020-06-08 11:01:20 +02:00
|
|
|
},
|
2020-06-09 12:23:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register actions
|
|
|
|
* @param {Array} newActions actions to register
|
|
|
|
*/
|
2020-06-08 11:01:20 +02:00
|
|
|
register(newActions) {
|
2020-06-08 15:13:26 +02:00
|
|
|
if (strapi.isLoaded) {
|
|
|
|
throw new Error(`You can't register new actions outside of the bootstrap function.`);
|
|
|
|
}
|
2020-06-08 11:01:20 +02:00
|
|
|
validateRegisterProviderAction(newActions);
|
|
|
|
newActions.forEach(newAction => {
|
|
|
|
const actionId = getActionId(newAction);
|
|
|
|
if (actions.has(actionId)) {
|
|
|
|
throw new yup.ValidationError(
|
|
|
|
`Duplicated action id: ${actionId}. You may want to change the actions name.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
actions.set(actionId, createAction(newAction));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-06-09 18:34:13 +02:00
|
|
|
module.exports = createActionProvider();
|