website/helpers/api.js
RedDucks 1486511463 Starting bringing over a better comment scheme for methods (see helpers/api.js), addedemail confirmation sending, proper logging, fixed registration
Server now sends confirmation emails to the provided email address. The values can not yet be validated, but they are stored so validation can be added at any time. Added Winston for event logging. Winston has a log level system that merges certain log types into the same file. Maybe we should use a custom logger solution? Registration now works (was not working for me), with added reCaptcha protection, password validation, and the storing of usernames/email validation values. Also changed the PID to a number, cuz it's a number
2018-11-28 14:49:31 -05:00

71 lines
1.3 KiB
JavaScript

/*
api.js -
common api returns
*/
/**
* Send generic API response
* @param {ServerResponse} response An express ServerResponse response
* @param {Object} data Response data
* @param {Array} errors Request errors
*/
function sendReturn(response, data, errors) {
return response.status(data.code || 200).json(
Object.assign({
code: 200,
success: true,
errors: [] + (errors ? errors : [])
}, data)
);
}
/**
* Send API 404
* @param {ServerResponse} response An express ServerResponse response
*/
function sendApi404(response) {
return sendApiError(response, 404, [
'Endpoint not in use'
]);
}
/**
* Send user not authenticated error
* @param {ServerResponse} response An express ServerResponse response
*/
function sendApiAuthError(response) {
return sendApiError(response, 401, [
'Not authenticated'
]);
}
/**
* Send a generic API error
* @param {ServerResponse} response An express ServerResponse response
*/
function sendApiGenericError(response) {
return sendApiError(response, 400, [
'Bad request'
]);
}
/**
* Send an API error
* @param {ServerResponse} response An express ServerResponse response
*/
function sendApiError(response, code, errors) {
return sendReturn(response, {
code,
success: false
}, errors);
}
module.exports = {
sendReturn,
sendApi404,
sendApiGenericError,
sendApiError,
sendApiAuthError
};