Skip to content

Commit

Permalink
reduce cognitive complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
aattiyah committed Sep 3, 2024
1 parent 09ad0fe commit b11f015
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
Binary file added dump.rdb
Binary file not shown.
48 changes: 26 additions & 22 deletions src/controllers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,31 @@ const helpers = module.exports;
const relative_path = nconf.get('relative_path');
const url = nconf.get('url');

const handleSuccessfulResponse = (statusCode, res, payload) => {
if (res.req.loggedIn) {
res.set('cache-control', 'private');
}

let code = 'ok';
let message = 'OK';
switch (statusCode) {
case 202:
code = 'accepted';
message = 'Accepted';
break;

case 204:
code = 'no-content';
message = 'No Content';
break;
}

res.status(statusCode).json({
status: { code, message },
response: payload || {},
});
};

helpers.noScriptErrors = async function (req, res, error, httpStatus) {
if (req.body.noscript !== 'true') {
if (typeof error === 'string') {
Expand Down Expand Up @@ -454,28 +479,7 @@ helpers.formatApiResponse = async (statusCode, res, payload) => {
}

if (String(statusCode).startsWith('2')) {
if (res.req.loggedIn) {
res.set('cache-control', 'private');
}

let code = 'ok';
let message = 'OK';
switch (statusCode) {
case 202:
code = 'accepted';
message = 'Accepted';
break;

case 204:
code = 'no-content';
message = 'No Content';
break;
}

res.status(statusCode).json({
status: { code, message },
response: payload || {},
});
handleSuccessfulResponse(statusCode, res, payload);
} else if (payload instanceof Error || typeof payload === 'string') {
const message = payload instanceof Error ? payload.message : payload;
const response = {};
Expand Down

0 comments on commit b11f015

Please sign in to comment.