From 337b47685c0cdf6445f00adfba0418f9448f3c81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Tue, 13 Mar 2018 11:42:25 +0100 Subject: [PATCH] Remove controler context from middlewares --- lib/cartodb/controllers/analyses.js | 38 ++++++++++++++--------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/cartodb/controllers/analyses.js b/lib/cartodb/controllers/analyses.js index db3550c8..4318340e 100644 --- a/lib/cartodb/controllers/analyses.js +++ b/lib/cartodb/controllers/analyses.js @@ -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}'