refactor: closes #10509

deprecate third param(middleware) to setupPageRoute/setupAdminPageRoute
make middlewares optional on all 3 helpers
isekai-main
Barış Soner Uşaklı 3 years ago
parent 976914e71f
commit 7f241dbbe6

@ -1,17 +1,18 @@
'use strict';
const helpers = module.exports;
const winston = require('winston');
const middleware = require('../middleware');
const controllerHelpers = require('../controllers/helpers');
function _handleArgs(middleware, middlewares, controller) {
if (typeof middlewares === 'function') {
if (controller) {
middlewares = [middlewares];
} else {
controller = middlewares;
middlewares = [];
}
// router, name, middleware(deprecated), middlewares(optional), controller
helpers.setupPageRoute = function (...args) {
const [router, name] = args;
let middlewares = args.length > 3 ? args[args.length - 2] : [];
const controller = args[args.length - 1];
if (args.length === 5) {
winston.warn('[helpers.setupPageRotue] passing middleware as third param is deprecated');
}
middlewares = [
@ -20,15 +21,9 @@ function _handleArgs(middleware, middlewares, controller) {
middleware.registrationComplete,
middleware.pluginHooks,
...middlewares,
middleware.pageView,
];
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,
@ -39,13 +34,31 @@ helpers.setupPageRoute = function (router, name, middleware, middlewares, contro
router.get(`/api${name}`, middlewares, helpers.tryRoute(controller));
};
helpers.setupAdminPageRoute = function (router, name, middleware, middlewares, controller) {
// router, name, middleware(deprecated), middlewares(optional), controller
helpers.setupAdminPageRoute = function (...args) {
const [router, name] = args;
const middlewares = args.length > 3 ? args[args.length - 2] : [];
const controller = args[args.length - 1];
if (args.length === 5) {
winston.warn('[helpers.setupAdminPageRoute] passing middleware as third param is deprecated');
}
router.get(name, middleware.admin.buildHeader, middlewares, helpers.tryRoute(controller));
router.get(`/api${name}`, middlewares, helpers.tryRoute(controller));
};
helpers.setupApiRoute = function (router, verb, name, middlewares, controller) {
({ middlewares, controller } = _handleArgs(middleware, middlewares, controller));
// router, verb, name, middlewares(optional), controller
helpers.setupApiRoute = function (...args) {
const [router, verb, name] = args;
let middlewares = args.length > 4 ? args[args.length - 2] : [];
const controller = args[args.length - 1];
middlewares = [
middleware.authenticateRequest,
middleware.maintenanceMode,
middleware.registrationComplete,
middleware.pluginHooks,
...middlewares,
];
router[verb](name, middlewares, helpers.tryRoute(controller, (err, res) => {
controllerHelpers.formatApiResponse(400, res, err);

Loading…
Cancel
Save