From 519d49bd10e094568b4ebc8a0ebdfc0f24d580c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 29 Dec 2017 15:04:44 +0100 Subject: [PATCH] Remove finish function and respond in the main middleware --- lib/cartodb/controllers/named_maps_admin.js | 67 ++++++++++----------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/lib/cartodb/controllers/named_maps_admin.js b/lib/cartodb/controllers/named_maps_admin.js index b46de702..ca7b4901 100644 --- a/lib/cartodb/controllers/named_maps_admin.js +++ b/lib/cartodb/controllers/named_maps_admin.js @@ -80,9 +80,12 @@ NamedMapsAdminController.prototype.create = function(req, res, next) { }, function prepareResponse(err, tpl_id){ assert.ifError(err); - return { template_id: tpl_id }; - }, - finishFn(self, req, res, 'POST TEMPLATE', null, next) + + res.status(200); + + const method = req.query.callback ? 'jsonp' : 'json'; + res[method]({ template_id: tpl_id }); + } ); }; @@ -102,9 +105,11 @@ NamedMapsAdminController.prototype.update = function(req, res, next) { function prepareResponse(err){ assert.ifError(err); - return { template_id: tpl_id }; - }, - finishFn(self, req, res, 'PUT TEMPLATE', null, next) + res.status(200); + + const method = req.query.callback ? 'jsonp' : 'json'; + res[method]({ template_id: tpl_id }); + } ); }; @@ -123,16 +128,19 @@ NamedMapsAdminController.prototype.retrieve = function(req, res, next) { function prepareResponse(err, tpl_val) { assert.ifError(err); if ( ! tpl_val ) { - err = new Error("Cannot find template '" + tpl_id + "' of user '" + cdbuser + "'"); - err.http_status = 404; - throw err; + const error = new Error(`Cannot find template '${tpl_id}' of user '${cdbuser}'`); + error.http_status = 404; + return next(error); } // auth_id was added by ourselves, // so we remove it before returning to the user delete tpl_val.auth_id; - return { template: tpl_val }; - }, - finishFn(self, req, res, 'GET TEMPLATE', null, next) + + res.status(200); + + const method = req.query.callback ? 'jsonp' : 'json'; + res[method]({ template: tpl_val }); + } ); }; @@ -150,9 +158,12 @@ NamedMapsAdminController.prototype.destroy = function(req, res, next) { }, function prepareResponse(err/*, tpl_val*/){ assert.ifError(err); - return ''; - }, - finishFn(self, req, res, 'DELETE TEMPLATE', 204, next) + + res.status(204); + + const method = req.query.callback ? 'jsonp' : 'json'; + res[method](''); + } ); }; @@ -168,9 +179,12 @@ NamedMapsAdminController.prototype.list = function(req, res, next) { }, function prepareResponse(err, tpl_ids){ assert.ifError(err); - return { template_ids: tpl_ids }; - }, - finishFn(self, req, res, 'GET TEMPLATE LIST', null, next) + + res.status(200); + + const method = req.query.callback ? 'jsonp' : 'json'; + res[method]({ template_ids: tpl_ids }); + } ); }; @@ -205,20 +219,3 @@ NamedMapsAdminController.prototype.checkContentType = function (action, label) { next(); }; }; - -function finishFn(controller, req, res, description, status, next) { - return function finish(err, body){ - if (err) { - err.label = description; - next(err); - } else { - res.status(status || 200); - - if (req.query && req.query.callback) { - res.jsonp(body); - } else { - res.json(body); - } - } - }; -}