From 86383d4f5108eef9ef42730150c19311e1637950 Mon Sep 17 00:00:00 2001 From: Udo Walter Date: Sun, 31 Mar 2019 17:26:26 +0000 Subject: [server/httpresponse] small rework --- server/lib/httpresponse.js | 52 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 13 deletions(-) (limited to 'server/lib') diff --git a/server/lib/httpresponse.js b/server/lib/httpresponse.js index 4ef6ab1..8b6c452 100644 --- a/server/lib/httpresponse.js +++ b/server/lib/httpresponse.js @@ -1,17 +1,43 @@ -const response = {} +class HttpResponse { + constructor (httpStatus, idString, message, data = {}) { + this.httpStatus = httpStatus + this.idString = idString + this.message = message + this.data = data + } -response.success = (res, action, type, id, data = {}) => res.status(200).send({ message: `Successfully ${action} ${type} ${id}.`, id, ...data }) -response.successBatch = (res, action, type, count, data = {}) => { - if (Array.isArray(type) && type.length === 2) type = count === 1 ? type[0] : type[1] - else if (typeof type === 'string' && count !== 1) type += 's' - return res.status(200).send({ message: `Successfully ${action} ${count} ${type}.`, count, ...data }) + send (res) { + const response = {} + if (this.httpStatus >= 400) response.error = this.idString + else if (this.idString) response.action = this.idString + if (this.message) response.message = this.message + res.status(this.httpStatus).send({ ...response, ...this.data }) + } } -response.notFound = (res, id) => res.status(404).send({ error: 'NOT_FOUND', message: 'ID ' + id + ' does not exist.' }) -response.invalidId = (res, zeroAllowed) => res.status(400).send({ error: 'INVALID_ID', message: 'ID has to be an integer' + (zeroAllowed ? '' : ' greater than 0') + '.' }) -response.invalidBodyValue = (res, key, rule) => res.status(400).send({ - error: 'BAD_REQUEST', - message: 'JSON body value for key "' + key + '" has to be ' + rule + '.' -}) +// ############################################################################ +// ############################## SUCCESS ##################################### -module.exports = response +// General +HttpResponse.success = (action, type, id) => new HttpResponse(200, action.toUpperCase(), `Successfully ${action} ${type} ${id}.`, { id }) +HttpResponse.successBatch = (action, type, count) => { + if (Array.isArray(type)) type = type[count === 1 ? 0 : 1] + else if (count !== 1) type += 's' + return new HttpResponse(200, action.toUpperCase() + '_MULTIPLE', `Successfully ${action} ${count} ${type}.`, { count }) +} + +// ############################################################################ +// ############################### ERROR ###################################### + +// General +HttpResponse.notFound = (id) => new HttpResponse(404, 'NOT_FOUND', 'ID ' + id + ' does not exist.') +HttpResponse.invalidId = (zeroAllowed) => new HttpResponse(400, 'INVALID_ID', 'ID has to be an integer' + (zeroAllowed ? '' : ' greater than 0') + '.') +HttpResponse.invalidBodyValue = (key, rule) => new HttpResponse(400, 'BAD_REQUEST', 'JSON body value for key "' + key + '" has to be ' + rule + '.') + +// Authentication +HttpResponse.invalidToken = () => new HttpResponse(401, 'INVALID_TOKEN', 'The provided token is invalid.') + +// ############################################################################ +// ############################################################################ + +module.exports = HttpResponse -- cgit v1.2.3-55-g7522