From d98ca437994b92ffe299738e0c5d02059c5cbd7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 22 Apr 2022 15:24:02 -0400 Subject: [PATCH] Revert "refactor: closes #10509" This reverts commit 7f241dbbe63d5c171b3a124971f5f8bb8c460b89. --- src/routes/helpers.js | 49 ++++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/src/routes/helpers.js b/src/routes/helpers.js index cea52ee050..8bcad07b22 100644 --- a/src/routes/helpers.js +++ b/src/routes/helpers.js @@ -1,18 +1,17 @@ 'use strict'; const helpers = module.exports; -const winston = require('winston'); const middleware = require('../middleware'); const controllerHelpers = require('../controllers/helpers'); -// 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'); +function _handleArgs(middleware, middlewares, controller) { + if (typeof middlewares === 'function') { + if (controller) { + middlewares = [middlewares]; + } else { + controller = middlewares; + middlewares = []; + } } middlewares = [ @@ -21,9 +20,15 @@ helpers.setupPageRoute = function (...args) { 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, @@ -34,31 +39,13 @@ helpers.setupPageRoute = function (...args) { router.get(`/api${name}`, middlewares, helpers.tryRoute(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'); - } +helpers.setupAdminPageRoute = function (router, name, middleware, middlewares, controller) { router.get(name, middleware.admin.buildHeader, middlewares, helpers.tryRoute(controller)); router.get(`/api${name}`, middlewares, helpers.tryRoute(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, - ]; +helpers.setupApiRoute = function (router, verb, name, middlewares, controller) { + ({ middlewares, controller } = _handleArgs(middleware, middlewares, controller)); router[verb](name, middlewares, helpers.tryRoute(controller, (err, res) => { controllerHelpers.formatApiResponse(400, res, err);