Remove controler context from middlewares

This commit is contained in:
Daniel García Aubert
2018-03-13 11:42:25 +01:00
parent dd4aa09d21
commit 337b47685c
+19 -19
View File
@@ -14,24 +14,24 @@ AnalysesController.prototype.register = function (app) {
cors(),
userMiddleware(),
this.prepareContext,
this.createPGClient(),
this.getDataFromQuery({ queryTemplate: catalogQueryTpl, key: 'catalog' }),
this.getDataFromQuery({ queryTemplate: tablesQueryTpl, key: 'tables' }),
this.prepareResponse(),
this.setCacheControlHeader(),
this.sendResponse(),
this.unathorizedError()
createPGClient(),
getDataFromQuery({ queryTemplate: catalogQueryTpl, key: 'catalog' }),
getDataFromQuery({ queryTemplate: tablesQueryTpl, key: 'tables' }),
prepareResponse(),
setCacheControlHeader(),
sendResponse(),
unathorizedError()
);
};
AnalysesController.prototype.createPGClient = function () {
function createPGClient () {
return function createPGClientMiddleware (req, res, next) {
res.locals.pg = new PSQL(dbParamsFromReqParams(res.locals));
next();
};
};
}
AnalysesController.prototype.getDataFromQuery = function ({ queryTemplate, key }) {
function getDataFromQuery({ queryTemplate, key }) {
const readOnlyTransactionOn = true;
return function getCatalogMiddleware(req, res, next) {
@@ -48,9 +48,9 @@ AnalysesController.prototype.getDataFromQuery = function ({ queryTemplate, key }
next();
}, readOnlyTransactionOn);
};
};
}
AnalysesController.prototype.prepareResponse = function () {
function prepareResponse () {
return function prepareResponseMiddleware (req, res, next) {
const { catalog, tables } = res.locals;
@@ -91,16 +91,16 @@ AnalysesController.prototype.prepareResponse = function () {
next();
};
};
}
AnalysesController.prototype.setCacheControlHeader = function () {
function setCacheControlHeader () {
return function setCacheControlHeaderMiddleware (req, res, next) {
res.set('Cache-Control', 'public,max-age=10,must-revalidate');
next();
};
};
}
AnalysesController.prototype.sendResponse = function() {
function sendResponse () {
return function sendResponseMiddleware (req, res) {
res.status(200);
@@ -110,9 +110,9 @@ AnalysesController.prototype.sendResponse = function() {
res.json(res.body);
}
};
};
}
AnalysesController.prototype.unathorizedError = function () {
function unathorizedError () {
return function unathorizedErrorMiddleware(err, req, res, next) {
if (err.message.match(/permission\sdenied/)) {
err = new Error('Unauthorized');
@@ -121,7 +121,7 @@ AnalysesController.prototype.unathorizedError = function () {
next(err);
};
};
}
const catalogQueryTpl = ctx => `
SELECT analysis_def->>'type' as type, * FROM cdb_analysis_catalog WHERE username = '${ctx._username}'