Remove finish function and respond in the main middleware

This commit is contained in:
Daniel García Aubert
2017-12-29 15:04:44 +01:00
parent bf814c4442
commit 519d49bd10

View File

@@ -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);
}
}
};
}