Revert "refactor: closes #10509"

This reverts commit 7f241dbbe6.
isekai-main
Barış Soner Uşaklı 3 years ago
parent 7f241dbbe6
commit d98ca43799

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

Loading…
Cancel
Save