feat: allow missing (or non-array) middlewares argument in route helper methods

v1.18.x
Julian Lam 4 years ago
parent 5945907429
commit 4b5450853d

@ -4,15 +4,30 @@ const helpers = module.exports;
const middleware = require('../middleware');
const controllerHelpers = require('../controllers/helpers');
helpers.setupPageRoute = function (router, name, middleware, middlewares, controller) {
function _handleArgs(middleware, middlewares, controller) {
if (typeof middlewares === 'function') {
if (controller) {
middlewares = [middlewares];
} else {
controller = middlewares;
middlewares = [];
}
}
middlewares = [
middleware.maintenanceMode,
middleware.registrationComplete,
middleware.authenticateRequest,
middleware.pluginHooks,
middleware.pageView,
...middlewares];
return { middlewares, controller };
}
helpers.setupPageRoute = function (router, name, middleware, middlewares, controller) {
({ middlewares, controller } = _handleArgs(middleware, middlewares, controller));
middlewares.push(middleware.pageView);
router.get(
name,
middleware.busyCheck,
@ -30,13 +45,7 @@ helpers.setupAdminPageRoute = function (router, name, middleware, middlewares, c
};
helpers.setupApiRoute = function (router, verb, name, middlewares, controller) {
middlewares = [
middleware.maintenanceMode,
middleware.registrationComplete,
middleware.authenticateRequest,
middleware.pluginHooks,
...middlewares,
];
({ middlewares, controller } = _handleArgs(middleware, middlewares, controller));
router[verb](name, middlewares, helpers.tryRoute(controller, (err, res) => {
controllerHelpers.formatApiResponse(400, res, err);

Loading…
Cancel
Save