switch to standard response format in v3

This commit is contained in:
Matteo Pagliazzi
2016-04-18 21:56:58 +02:00
parent 318abfeefa
commit 277248b8b7
6 changed files with 42 additions and 8 deletions

View File

@@ -38,6 +38,7 @@ describe('errorHandler', () => {
expect(res.status).to.be.calledWith(500); expect(res.status).to.be.calledWith(500);
expect(res.json).to.be.calledWith({ expect(res.json).to.be.calledWith({
success: false,
error: 'InternalServerError', error: 'InternalServerError',
message: 'An unexpected error occurred.', message: 'An unexpected error occurred.',
}); });
@@ -54,6 +55,7 @@ describe('errorHandler', () => {
expect(res.status).to.be.calledWith(400); expect(res.status).to.be.calledWith(400);
expect(res.json).to.be.calledWith({ expect(res.json).to.be.calledWith({
success: false,
error: 'Error', error: 'Error',
message: 'Error message', message: 'Error message',
}); });
@@ -70,6 +72,7 @@ describe('errorHandler', () => {
expect(res.status).to.be.calledWith(500); expect(res.status).to.be.calledWith(500);
expect(res.json).to.be.calledWith({ expect(res.json).to.be.calledWith({
success: false,
error: 'InternalServerError', error: 'InternalServerError',
message: 'An unexpected error occurred.', message: 'An unexpected error occurred.',
}); });
@@ -85,6 +88,7 @@ describe('errorHandler', () => {
expect(res.status).to.be.calledWith(400); expect(res.status).to.be.calledWith(400);
expect(res.json).to.be.calledWith({ expect(res.json).to.be.calledWith({
success: false,
error: 'BadRequest', error: 'BadRequest',
message: 'Bad request.', message: 'Bad request.',
}); });
@@ -101,6 +105,7 @@ describe('errorHandler', () => {
expect(res.status).to.be.calledWith(error.statusCode); expect(res.status).to.be.calledWith(error.statusCode);
expect(res.json).to.be.calledWith({ expect(res.json).to.be.calledWith({
success: false,
error: error.name, error: error.name,
message: error.message, message: error.message,
}); });
@@ -116,6 +121,7 @@ describe('errorHandler', () => {
expect(res.status).to.be.calledWith(400); expect(res.status).to.be.calledWith(400);
expect(res.json).to.be.calledWith({ expect(res.json).to.be.calledWith({
success: false,
error: 'BadRequest', error: 'BadRequest',
message: 'Invalid request parameters.', message: 'Invalid request parameters.',
errors: [ errors: [
@@ -143,6 +149,7 @@ describe('errorHandler', () => {
expect(res.status).to.be.calledWith(400); expect(res.status).to.be.calledWith(400);
expect(res.json).to.be.calledWith({ expect(res.json).to.be.calledWith({
success: false,
error: 'BadRequest', error: 'BadRequest',
message: 'User validation failed.', message: 'User validation failed.',
errors: [ errors: [

View File

@@ -30,7 +30,8 @@ describe('response middleware', () => {
expect(res.status).to.be.calledWith(200); expect(res.status).to.be.calledWith(200);
expect(res.json).to.be.calledWith({ expect(res.json).to.be.calledWith({
field: 1, success: true,
data: {field: 1},
}); });
}); });
@@ -43,7 +44,8 @@ describe('response middleware', () => {
expect(res.status).to.be.calledWith(403); expect(res.status).to.be.calledWith(403);
expect(res.json).to.be.calledWith({ expect(res.json).to.be.calledWith({
field: 1, success: false,
data: {field: 1},
}); });
}); });
}); });

View File

@@ -61,7 +61,7 @@ function _requestMaker (user, method, additionalSets = {}) {
let parsedError = _parseError(err); let parsedError = _parseError(err);
reject(parsedError); return reject(parsedError);
} }
// if any cookies was sent, save it for the next request // if any cookies was sent, save it for the next request
@@ -71,13 +71,31 @@ function _requestMaker (user, method, additionalSets = {}) {
}).join('; '); }).join('; ');
} }
let contentType = response.headers['content-type'] || ''; resolve(_parseRes(response));
resolve(contentType.indexOf('json') !== -1 ? response.body : response.text);
}); });
}); });
}; };
} }
function _parseRes (res) {
let contentType = res.headers['content-type'] || '';
let contentDisposition = res.headers['content-disposition'] || '';
if (contentType.indexOf('json') === -1) { // not a json response
return res.text;
}
if (contentDisposition.indexOf('attachment') !== -1) {
return res.body;
}
if (apiVersion === 'v2') {
return res.body;
} else if (apiVersion === 'v3') {
return res.body.data;
}
}
function _parseError (err) { function _parseError (err) {
let parsedError; let parsedError;

View File

@@ -96,7 +96,9 @@ api.getContent = {
res.set({ res.set({
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}); });
res.status(200).send(content); // TODO how to use res.respond here?
let jsonResString = `{"success": true, "data": ${content}}`;
res.status(200).send(jsonResString);
// save the file in background unless it's already cached or being written right now // save the file in background unless it's already cached or being written right now
if (cachedContentResponses[language] !== true && cacheBeingWritten[language] !== true) { if (cachedContentResponses[language] !== true && cacheBeingWritten[language] !== true) {

View File

@@ -62,6 +62,7 @@ module.exports = function errorHandler (err, req, res, next) { // eslint-disable
} }
let jsonRes = { let jsonRes = {
success: false,
error: responseErr.name, error: responseErr.name,
message: responseErr.message, message: responseErr.message,
}; };
@@ -72,5 +73,5 @@ module.exports = function errorHandler (err, req, res, next) { // eslint-disable
// In some occasions like when invalid JSON is supplied `res.respond` might be not yet avalaible, // In some occasions like when invalid JSON is supplied `res.respond` might be not yet avalaible,
// in this case we use the standard res.status(...).json(...) // in this case we use the standard res.status(...).json(...)
return res.respond ? res.respond(responseErr.httpCode, jsonRes) : res.status(responseErr.httpCode).json(jsonRes); return res.status(responseErr.httpCode).json(jsonRes);
}; };

View File

@@ -1,6 +1,10 @@
module.exports = function responseHandler (req, res, next) { module.exports = function responseHandler (req, res, next) {
// Only used for successful responses
res.respond = function respond (status = 200, data = {}) { res.respond = function respond (status = 200, data = {}) {
res.status(status).json(data); res.status(status).json({
success: status < 400,
data,
});
}; };
next(); next();