Add .middlewares() method to build middleware stack to be mounted

This commit is contained in:
Daniel García Aubert 2018-05-11 16:07:25 +02:00
parent 3d7231929c
commit 1f717617b0

View File

@ -3,70 +3,90 @@ const credentials = require('../middlewares/credentials');
const rateLimit = require('../middlewares/rate-limit'); const rateLimit = require('../middlewares/rate-limit');
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit; const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit;
/** module.exports = class AdminTemplateController {
* @param {AuthBackend} authBackend /**
* @param {PgConnection} pgConnection * @param {AuthBackend} authBackend
* @param {TemplateMaps} templateMaps * @param {PgConnection} pgConnection
* @constructor * @param {TemplateMaps} templateMaps
*/ * @constructor
function AdminTemplateController(authBackend, templateMaps, userLimitsBackend) { */
this.authBackend = authBackend; constructor (authBackend, templateMaps, userLimitsBackend) {
this.templateMaps = templateMaps; this.authBackend = authBackend;
this.userLimitsBackend = userLimitsBackend; this.templateMaps = templateMaps;
} this.userLimitsBackend = userLimitsBackend;
}
module.exports = AdminTemplateController; register (templateRouter) {
templateRouter.options(`/:template_id`);
AdminTemplateController.prototype.register = function (templateRouter) { templateRouter.post('/', this.middlewares({
templateRouter.options(`/:template_id`); action: 'create',
label: 'POST TEMPLATE',
rateLimitGroup: RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_CREATE
}));
templateRouter.post( templateRouter.put('/:template_id', this.middlewares({
`/`, action: 'update',
credentials(), label: 'PUT TEMPLATE',
authorizedByAPIKey({ authBackend: this.authBackend, action: 'create', label: 'POST TEMPLATE' }), rateLimitGroup: RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_UPDATE
rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_CREATE), }));
checkContentType({ action: 'POST', label: 'POST TEMPLATE' }),
createTemplate({ templateMaps: this.templateMaps })
);
templateRouter.put( templateRouter.get('/:template_id', this.middlewares({
`/:template_id`, action: 'get',
credentials(), label: 'GET TEMPLATE',
authorizedByAPIKey({ authBackend: this.authBackend, action: 'update', label: 'PUT TEMPLATE' }), rateLimitGroup: RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_GET
rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_UPDATE), }));
checkContentType({ action: 'PUT', label: 'PUT TEMPLATE' }),
updateTemplate({ templateMaps: this.templateMaps })
);
templateRouter.get( templateRouter.delete('/:template_id', this.middlewares({
`/:template_id`, action: 'delete',
credentials(), label: 'DELETE TEMPLATE',
authorizedByAPIKey({ authBackend: this.authBackend, action: 'get', label: 'GET TEMPLATE' }), rateLimitGroup: RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_DELETE
rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_GET), }));
retrieveTemplate({ templateMaps: this.templateMaps })
);
templateRouter.delete( templateRouter.get('/', this.middlewares({
`/:template_id`, action: 'list',
credentials(), label: 'GET TEMPLATE LIST',
authorizedByAPIKey({ authBackend: this.authBackend, action: 'delete', label: 'DELETE TEMPLATE' }), rateLimitGroup: RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_LIST
rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_DELETE), }));
destroyTemplate({ templateMaps: this.templateMaps }) }
);
templateRouter.get( middlewares ({ action, label, rateLimitGroup }) {
`/`, let template;
credentials(),
authorizedByAPIKey({ authBackend: this.authBackend, action: 'list', label: 'GET TEMPLATE LIST' }), if (action === 'create') {
rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.NAMED_LIST), template = createTemplate;
listTemplates({ templateMaps: this.templateMaps }) }
);
if (action === 'update') {
template = updateTemplate;
}
if (action === 'get') {
template = retrieveTemplate;
}
if (action === 'delete') {
template = destroyTemplate;
}
if (action === 'list') {
template = listTemplates;
}
return [
credentials(),
authorizedByAPIKey({ authBackend: this.authBackend, action, label }),
rateLimit(this.userLimitsBackend, rateLimitGroup),
checkContentType({ action: 'POST', label: 'POST TEMPLATE' }),
template({ templateMaps: this.templateMaps })
];
}
}; };
function checkContentType ({ action, label }) { function checkContentType ({ label }) {
return function checkContentTypeMiddleware (req, res, next) { return function checkContentTypeMiddleware (req, res, next) {
if (!req.is('application/json')) { if ((req.method === 'POST' || req.method === 'PUT') && !req.is('application/json')) {
const error = new Error(`template ${action} data must be of type application/json`); const error = new Error(`${req.method} template data must be of type application/json`);
error.label = label; error.label = label;
return next(error); return next(error);
} }