2020-06-09 11:48:49 +02:00
|
|
|
'use strict';
|
|
|
|
|
2020-06-08 11:01:20 +02:00
|
|
|
const { yup } = require('strapi-utils');
|
2020-06-22 13:00:21 +02:00
|
|
|
const { validateRegisterProviderAction } = require('../../validation/action-provider');
|
|
|
|
const { getActionId, createAction } = require('../../domain/action');
|
2020-06-08 11:01:20 +02:00
|
|
|
|
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-08-11 16:39:05 +02:00
|
|
|
return actions.get(actionId);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an action by its actionId
|
|
|
|
* @param {string} actionId
|
|
|
|
* @returns {Action}
|
|
|
|
*/
|
|
|
|
getByActionId(actionId) {
|
|
|
|
return actions.get(actionId);
|
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-10 11:28:32 +02:00
|
|
|
return Array.from(actions.values());
|
2020-06-08 15:13:26 +02:00
|
|
|
},
|
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() {
|
2020-06-10 11:28:32 +02:00
|
|
|
return 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();
|