mirror of
https://github.com/PretendoNetwork/website.git
synced 2026-03-26 19:55:04 -05:00
Added morgan for some route debugging. Removed body-parser since it's built into express now. Made some error returns more clear and modern-ized some random snippets
71 lines
1.2 KiB
JavaScript
71 lines
1.2 KiB
JavaScript
/*
|
|
|
|
api.js -
|
|
common api returns
|
|
|
|
*/
|
|
|
|
// use for any api return. it has basic layout used for every return.
|
|
function sendReturn(response, data, errors) {
|
|
response.status(200).json(
|
|
// combine 2 objects
|
|
Object.assign({
|
|
code: 200,
|
|
success: true,
|
|
errors: [] + (errors ? errors : [])
|
|
}, data)
|
|
);
|
|
}
|
|
|
|
// shows 404 template. takes express response object
|
|
function send404(res) {
|
|
res.status(404).send('404');
|
|
}
|
|
|
|
// use if api endpoint doesnt exist
|
|
function sendApi404(response) {
|
|
response.status(404).json({
|
|
code: 404,
|
|
errors: [
|
|
'Endpoint not in use'
|
|
]
|
|
});
|
|
}
|
|
|
|
// use if not logged in and is required (handled with middleware)
|
|
function sendApiAuthError(response) {
|
|
response.status(401).json({
|
|
code: 401,
|
|
errors: [
|
|
'Not authenticated'
|
|
]
|
|
});
|
|
}
|
|
|
|
// use for completely broken requests
|
|
function sendApiGenericError(response) {
|
|
response.status(400).json({
|
|
code: 400,
|
|
success: false,
|
|
errors: [
|
|
'Bad request'
|
|
]
|
|
});
|
|
}
|
|
|
|
// use for any api not successfull
|
|
function sendApiError(response, code, errors) {
|
|
response.status(code).json({
|
|
code,
|
|
success: false,
|
|
errors
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
sendReturn,
|
|
sendApi404,
|
|
sendApiGenericError,
|
|
sendApiError,
|
|
sendApiAuthError
|
|
}; |