Skip to content

Commit

Permalink
fix: update respond middleware to use res.customStatusCode instead of…
Browse files Browse the repository at this point in the history
… native res.statusCode
  • Loading branch information
jakowenko committed Sep 7, 2021
1 parent 7cedbe7 commit caf6a89
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions api/src/middlewares/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@ const respond = Object.assign(
},
{
success: (res, input) => {
const status = res.statusCode ? res.statusCode : !input ? NO_CONTENT : OK;
const status = res.customStatusCode ? res.customStatusCode : !input ? NO_CONTENT : OK;
if (!input) return { status };
return { status, body: input };
},
error: (res, input) => {
const status = res.statusCode || SERVER_ERROR;
const status = res.customStatusCode || SERVER_ERROR;
const body = { error: input.message };
return { status, body };
},
}
);

module.exports = (req, res, next) => {
res.statusCode = null;
const _send = res.send;
const _end = res.end;
const _status = res.status;

res.send = (input) => {
if (input instanceof Error && res.statusCode === SERVER_ERROR) console.error(input);
Expand All @@ -39,17 +38,17 @@ module.exports = (req, res, next) => {
return;
}

_send.call(res, body);
return _send.call(res, body);
};

res.error = (input) => {
const error = input instanceof Error ? input : new Error(input);
res.send.call(res, error);
res.status = (input) => {
res.customStatusCode = input;
return _status.call(res, input);
};

res.end = (input) => {
res.statusCode = OK;
_end.call(res, input);
res.error = (input) => {
const error = input instanceof Error ? input : new Error(input);
return res.send.call(res, error);
};

next();
Expand Down

0 comments on commit caf6a89

Please sign in to comment.