refactor: deprecate middleware param (#10513)

* refactor: deprecate middleware param

make middlewares optional

* chore: add node 18 again
isekai-main
Barış Soner Uşaklı 3 years ago committed by GitHub
parent 6eef08f9c1
commit 84f2726391
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -21,7 +21,7 @@ jobs:
fail-fast: false fail-fast: false
matrix: matrix:
os: [ubuntu-latest] os: [ubuntu-latest]
node: [14, 16] node: [14, 16, 18]
database: [mongo-dev, mongo, redis, postgres] database: [mongo-dev, mongo, redis, postgres]
include: include:
# only run coverage once # only run coverage once

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

Loading…
Cancel
Save