mirror of
https://github.com/strapi/strapi.git
synced 2025-11-08 06:07:41 +00:00
Improve policies loading into dictionary and lazy load hooks
This commit is contained in:
parent
5a9ca376db
commit
dd13dff7ad
@ -50,7 +50,7 @@ class Strapi extends EventEmitter {
|
|||||||
scope: ['dependencies', 'devDependencies'],
|
scope: ['dependencies', 'devDependencies'],
|
||||||
replaceString: /^koa(-|\.)/,
|
replaceString: /^koa(-|\.)/,
|
||||||
camelize: true,
|
camelize: true,
|
||||||
lazy: false
|
lazy: true
|
||||||
});
|
});
|
||||||
|
|
||||||
// New Winston logger.
|
// New Winston logger.
|
||||||
|
|||||||
@ -60,16 +60,18 @@ module.exports = strapi => {
|
|||||||
try {
|
try {
|
||||||
const {route, policies, action, validate} = routerChecker(value, endpoint);
|
const {route, policies, action, validate} = routerChecker(value, endpoint);
|
||||||
|
|
||||||
|
if (_.isUndefined(action) || !_.isFunction(action)) {
|
||||||
|
return strapi.log.warn('Ignored attempt to bind route `' + endpoint + '` to unknown controller/action.');
|
||||||
|
}
|
||||||
|
|
||||||
strapi.router.route(_.omitBy({
|
strapi.router.route(_.omitBy({
|
||||||
method: value.method,
|
method: value.method,
|
||||||
path: value.path,
|
path: value.path,
|
||||||
handler: [strapi.middlewares.compose(policies), action],
|
handler: [strapi.middlewares.compose(policies), action],
|
||||||
validate: validate
|
validate: validate
|
||||||
}, _.isEmpty));
|
}, _.isEmpty));
|
||||||
|
|
||||||
// strapi.router[route.verb.toLowerCase()](route.endpoint, strapi.middlewares.compose(policies), action);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
strapi.log.warn('Ignored attempt to bind route `' + endpoint + '` to unknown controller/action.');
|
cb(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -89,14 +91,18 @@ module.exports = strapi => {
|
|||||||
try {
|
try {
|
||||||
const {route, policies, action, validate} = routerChecker(value, endpoint, plugin);
|
const {route, policies, action, validate} = routerChecker(value, endpoint, plugin);
|
||||||
|
|
||||||
|
if (_.isUndefined(action) || !_.isFunction(action)) {
|
||||||
|
return strapi.log.warn('Ignored attempt to bind route `' + endpoint + '` to unknown controller/action.');
|
||||||
|
}
|
||||||
|
|
||||||
router.route(_.omitBy({
|
router.route(_.omitBy({
|
||||||
method: value.method,
|
method: value.method,
|
||||||
path: value.path,
|
path: value.path,
|
||||||
handler: [strapi.middlewares.compose(policies), action],
|
handler: _.remove([strapi.middlewares.compose(policies), action], o => _.isFunction(o)),
|
||||||
validate: validate
|
validate: validate
|
||||||
}, _.isEmpty));
|
}, _.isEmpty));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
strapi.log.warn('Ignored attempt to bind route `' + endpoint + '` to unknown controller/action.');
|
cb(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -110,14 +116,18 @@ module.exports = strapi => {
|
|||||||
try {
|
try {
|
||||||
const {route, policies, action, validate} = routerChecker(value, endpoint, plugin);
|
const {route, policies, action, validate} = routerChecker(value, endpoint, plugin);
|
||||||
|
|
||||||
|
if (_.isUndefined(action) || !_.isFunction(action)) {
|
||||||
|
return strapi.log.warn('Ignored attempt to bind route `' + endpoint + '` to unknown controller/action.');
|
||||||
|
}
|
||||||
|
|
||||||
strapi.router.route(_.omitBy({
|
strapi.router.route(_.omitBy({
|
||||||
method: value.method,
|
method: value.method,
|
||||||
path: value.path,
|
path: value.path,
|
||||||
handler: [strapi.middlewares.compose(policies), action],
|
handler: _.remove([strapi.middlewares.compose(policies), action], o => _.isFunction(o)),
|
||||||
validate: validate
|
validate: validate
|
||||||
}, _.isEmpty));
|
}, _.isEmpty));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
strapi.log.warn('Ignored attempt to bind route `' + endpoint + '` to unknown controller/action.');
|
cb(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -180,6 +190,10 @@ module.exports = strapi => {
|
|||||||
const controller = strapi.controllers[handler[0].toLowerCase()] || strapi.plugins[plugin].controllers[handler[0].toLowerCase()];
|
const controller = strapi.controllers[handler[0].toLowerCase()] || strapi.plugins[plugin].controllers[handler[0].toLowerCase()];
|
||||||
const action = controller[handler[1]];
|
const action = controller[handler[1]];
|
||||||
|
|
||||||
|
// Retrieve the API's name where the controller is located
|
||||||
|
// to access to the right validators
|
||||||
|
const currentApiName = finder(strapi.api, controller);
|
||||||
|
|
||||||
// Init policies array.
|
// Init policies array.
|
||||||
const policies = [];
|
const policies = [];
|
||||||
// Add the `globalPolicy`.
|
// Add the `globalPolicy`.
|
||||||
@ -187,6 +201,7 @@ module.exports = strapi => {
|
|||||||
|
|
||||||
// Add the `responsesPolicy`.
|
// Add the `responsesPolicy`.
|
||||||
policies.push(responsesPolicy);
|
policies.push(responsesPolicy);
|
||||||
|
|
||||||
// Allow string instead of array of policies
|
// Allow string instead of array of policies
|
||||||
if (!_.isArray(_.get(value, 'config.policies')) && !_.isEmpty(_.get(value, 'config.policies'))) {
|
if (!_.isArray(_.get(value, 'config.policies')) && !_.isEmpty(_.get(value, 'config.policies'))) {
|
||||||
value.config.policies = [value.config.policies];
|
value.config.policies = [value.config.policies];
|
||||||
@ -194,12 +209,14 @@ module.exports = strapi => {
|
|||||||
|
|
||||||
if (_.isArray(_.get(value, 'config.policies')) && !_.isEmpty(_.get(value, 'config.policies'))) {
|
if (_.isArray(_.get(value, 'config.policies')) && !_.isEmpty(_.get(value, 'config.policies'))) {
|
||||||
_.forEach(value.config.policies, policy => {
|
_.forEach(value.config.policies, policy => {
|
||||||
if (strapi.policies[policy]) {
|
// Looking for global policy or namespaced
|
||||||
return policies.push(strapi.policies[policy]);
|
if (_.startsWith(policy, '*', 0) && !_.isEmpty(_.get(strapi.policies, policy.substring(1).toLowerCase()))) {
|
||||||
|
return policies.push(strapi.policies[policy.substring(1).toLowerCase()]);
|
||||||
|
} else if (!_.startsWith(policy, '*', 0) && !_.isEmpty(_.get(strapi.api, currentApiName + '.policies.' + policy.toLowerCase()))) {
|
||||||
|
return policies.push(strapi.api[currentApiName].policies[policy.toLowerCase()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
strapi.log.error('Ignored attempt to bind route `' + endpoint + '` with unknown policy `' + policy + '`.');
|
strapi.log.error('Ignored attempt to bind route `' + endpoint + '` with unknown policy `' + policy + '`.');
|
||||||
process.exit(1);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -207,10 +224,7 @@ module.exports = strapi => {
|
|||||||
const validate = {};
|
const validate = {};
|
||||||
|
|
||||||
if (_.isString(_.get(value, 'config.validate')) && !_.isEmpty(_.get(value, 'config.validate'))) {
|
if (_.isString(_.get(value, 'config.validate')) && !_.isEmpty(_.get(value, 'config.validate'))) {
|
||||||
// Retrieve the API's name where the controller is located
|
const validator = _.get(strapi.api, currentApiName + '.validators.' + value.config.validate);
|
||||||
// to access to the right validators
|
|
||||||
const api = finder(strapi.api, controller);
|
|
||||||
const validator = _.get(strapi.api, api + '.validators.' + value.config.validate);
|
|
||||||
|
|
||||||
_.merge(validate, _.mapValues(validator, value => {
|
_.merge(validate, _.mapValues(validator, value => {
|
||||||
return builder.build(value);
|
return builder.build(value);
|
||||||
|
|||||||
@ -84,16 +84,16 @@ module.exports = strapi => {
|
|||||||
dictionary.optional({
|
dictionary.optional({
|
||||||
dirname: path.resolve(strapi.config.appPath, strapi.config.paths.api, api, strapi.config.paths.config, strapi.config.paths.policies),
|
dirname: path.resolve(strapi.config.appPath, strapi.config.paths.api, api, strapi.config.paths.config, strapi.config.paths.policies),
|
||||||
filter: /(.+)\.(js)$/,
|
filter: /(.+)\.(js)$/,
|
||||||
depth: 1
|
depth: 2
|
||||||
}, cb);
|
}, cb);
|
||||||
},
|
},
|
||||||
|
|
||||||
// Load API validators from `./api/*/config/validators/*.js`.
|
// Load API validators from `./api/*/config/validators/*.js`.
|
||||||
'validators/*': cb => {
|
'validators/*': cb => {
|
||||||
dictionary.optional({
|
dictionary.aggregate({
|
||||||
dirname: path.resolve(strapi.config.appPath, strapi.config.paths.api, api, strapi.config.paths.config, strapi.config.paths.validators),
|
dirname: path.resolve(strapi.config.appPath, strapi.config.paths.api, api, strapi.config.paths.config, strapi.config.paths.validators),
|
||||||
filter: /(.+)\.(json|js)$/,
|
filter: /(.+)\.(json|js)$/,
|
||||||
depth: 1
|
depth: 2
|
||||||
}, cb);
|
}, cb);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@ -42,7 +42,6 @@ module.exports = strapi => {
|
|||||||
|
|
||||||
// Callback.
|
// Callback.
|
||||||
(err, hooks) => {
|
(err, hooks) => {
|
||||||
|
|
||||||
// Just in case there is an error.
|
// Just in case there is an error.
|
||||||
if (err) {
|
if (err) {
|
||||||
return cb(err);
|
return cb(err);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user