Instantly share code, notes, and snippets.

@whtswrng /.js
Last active Nov 8, 2018

Embed
What would you like to do?
const {INTERNAL_SERVER_ERROR, OK } = require('http-status-codes');
async function login(request, response, loginAction) {
try {
const actionResult = await loginAction(request.body);
handleResponse(request, response, actionResult);
} catch (e) {
sendInternalServerError(response);
}
}
function handleResponse(request, response, actionResult) {
if (result(actionResult).hasStatusCode(OK)) {
sendSuccessfulResponse(response, request, actionResult);
} else {
sendInternalServerError(response);
}
}
function sendSuccessfulResponse(response, request, actionResult) {
const headers = {'set-cookie': actionResult.headers['set-cookie']};
const body = {};
response.send(OK, body, headers);
}
function sendInternalServerError(response) {
response.send(INTERNAL_SERVER_ERROR, { errorMessage: 'An unexpected error occurred.' });
}
function result(actionResult) {
return {
hasStatusCode: statusCode => {
return actionResult.statusCode === statusCode;
},
};
}
module.exports = login;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment