From dd13dff7ade75bb71a4465ff537e86e684e5b016 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Georget?= Date: Tue, 25 Oct 2016 18:04:32 +0200 Subject: [PATCH] Improve policies loading into dictionary and lazy load hooks --- packages/strapi/lib/Strapi.js | 2 +- .../configuration/hooks/core/router/index.js | 42 ++++++++++++------- .../hooks/dictionary/_api/index.js | 6 +-- .../hooks/dictionary/_externalHooks/index.js | 1 - 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/packages/strapi/lib/Strapi.js b/packages/strapi/lib/Strapi.js index 048fb28ab3..24ad6d4795 100644 --- a/packages/strapi/lib/Strapi.js +++ b/packages/strapi/lib/Strapi.js @@ -50,7 +50,7 @@ class Strapi extends EventEmitter { scope: ['dependencies', 'devDependencies'], replaceString: /^koa(-|\.)/, camelize: true, - lazy: false + lazy: true }); // New Winston logger. diff --git a/packages/strapi/lib/configuration/hooks/core/router/index.js b/packages/strapi/lib/configuration/hooks/core/router/index.js index eec3f354fc..eb39359daf 100644 --- a/packages/strapi/lib/configuration/hooks/core/router/index.js +++ b/packages/strapi/lib/configuration/hooks/core/router/index.js @@ -60,16 +60,18 @@ module.exports = strapi => { try { 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({ method: value.method, path: value.path, handler: [strapi.middlewares.compose(policies), action], validate: validate }, _.isEmpty)); - - // strapi.router[route.verb.toLowerCase()](route.endpoint, strapi.middlewares.compose(policies), action); } 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 { 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({ method: value.method, path: value.path, - handler: [strapi.middlewares.compose(policies), action], + handler: _.remove([strapi.middlewares.compose(policies), action], o => _.isFunction(o)), validate: validate }, _.isEmpty)); } 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 { 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({ method: value.method, path: value.path, - handler: [strapi.middlewares.compose(policies), action], + handler: _.remove([strapi.middlewares.compose(policies), action], o => _.isFunction(o)), validate: validate }, _.isEmpty)); } 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 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. const policies = []; // Add the `globalPolicy`. @@ -187,6 +201,7 @@ module.exports = strapi => { // Add the `responsesPolicy`. policies.push(responsesPolicy); + // Allow string instead of array of policies if (!_.isArray(_.get(value, 'config.policies')) && !_.isEmpty(_.get(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'))) { _.forEach(value.config.policies, policy => { - if (strapi.policies[policy]) { - return policies.push(strapi.policies[policy]); + // Looking for global policy or namespaced + 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 + '`.'); - process.exit(1); }); } @@ -207,10 +224,7 @@ module.exports = strapi => { const validate = {}; if (_.isString(_.get(value, 'config.validate')) && !_.isEmpty(_.get(value, 'config.validate'))) { - // Retrieve the API's name where the controller is located - // to access to the right validators - const api = finder(strapi.api, controller); - const validator = _.get(strapi.api, api + '.validators.' + value.config.validate); + const validator = _.get(strapi.api, currentApiName + '.validators.' + value.config.validate); _.merge(validate, _.mapValues(validator, value => { return builder.build(value); diff --git a/packages/strapi/lib/configuration/hooks/dictionary/_api/index.js b/packages/strapi/lib/configuration/hooks/dictionary/_api/index.js index 02096f9dbb..89c79c4490 100644 --- a/packages/strapi/lib/configuration/hooks/dictionary/_api/index.js +++ b/packages/strapi/lib/configuration/hooks/dictionary/_api/index.js @@ -84,16 +84,16 @@ module.exports = strapi => { dictionary.optional({ dirname: path.resolve(strapi.config.appPath, strapi.config.paths.api, api, strapi.config.paths.config, strapi.config.paths.policies), filter: /(.+)\.(js)$/, - depth: 1 + depth: 2 }, cb); }, // Load API validators from `./api/*/config/validators/*.js`. '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), filter: /(.+)\.(json|js)$/, - depth: 1 + depth: 2 }, cb); }, diff --git a/packages/strapi/lib/configuration/hooks/dictionary/_externalHooks/index.js b/packages/strapi/lib/configuration/hooks/dictionary/_externalHooks/index.js index 2960b23f13..78fe35b00c 100644 --- a/packages/strapi/lib/configuration/hooks/dictionary/_externalHooks/index.js +++ b/packages/strapi/lib/configuration/hooks/dictionary/_externalHooks/index.js @@ -42,7 +42,6 @@ module.exports = strapi => { // Callback. (err, hooks) => { - // Just in case there is an error. if (err) { return cb(err);