From a5347c27e317efb38662a5bb084b5531adc3343d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Tue, 14 Nov 2017 13:49:12 +0100 Subject: [PATCH] Move sendResponse method to a middleware --- lib/cartodb/controllers/analyses.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/cartodb/controllers/analyses.js b/lib/cartodb/controllers/analyses.js index f183e834..52c2976e 100644 --- a/lib/cartodb/controllers/analyses.js +++ b/lib/cartodb/controllers/analyses.js @@ -18,24 +18,26 @@ AnalysesController.prototype.register = function(app) { cors(), userMiddleware, this.prepareContext, - this.catalog.bind(this) + this.catalog.bind(this), + this.sendResponse() ); }; -AnalysesController.prototype.sendResponse = function(req, res, body) { - res.set('Cache-Control', 'public,max-age=10,must-revalidate'); +AnalysesController.prototype.sendResponse = function() { + return function sendResponseMiddleware (req, res) { + res.set('Cache-Control', 'public,max-age=10,must-revalidate'); - res.status(200); + res.status(200); - if (req.query && req.query.callback) { - res.jsonp(body); - } else { - res.json(body); - } + if (req.query && req.query.callback) { + res.jsonp(res.body); + } else { + res.json(res.body); + } + }; }; AnalysesController.prototype.catalog = function (req, res, next) { - var self = this; var username = res.locals.user; step( @@ -77,7 +79,7 @@ AnalysesController.prototype.catalog = function (req, res, next) { return -1; }); }, - function sendResponse(err, catalogWithTables) { + function prepareResponse(err, catalogWithTables) { if (err) { if (err.message.match(/permission\sdenied/)) { err = new Error('Unauthorized'); @@ -86,7 +88,8 @@ AnalysesController.prototype.catalog = function (req, res, next) { next(err); } else { - self.sendResponse(req, res, { catalog: catalogWithTables }); + res.body = { catalog: catalogWithTables }; + next(); } } );