summaryrefslogtreecommitdiffstats
path: root/server/lib/httpresponse.js
diff options
context:
space:
mode:
authorUdo Walter2019-03-31 19:26:26 +0200
committerUdo Walter2019-03-31 19:26:26 +0200
commit86383d4f5108eef9ef42730150c19311e1637950 (patch)
tree4ae3340c740d05832e0f428c2ea1bf875954212e /server/lib/httpresponse.js
parentMerge because Udo committed eslint errors :| (diff)
downloadbas-86383d4f5108eef9ef42730150c19311e1637950.tar.gz
bas-86383d4f5108eef9ef42730150c19311e1637950.tar.xz
bas-86383d4f5108eef9ef42730150c19311e1637950.zip
[server/httpresponse] small rework
Diffstat (limited to 'server/lib/httpresponse.js')
-rw-r--r--server/lib/httpresponse.js52
1 files changed, 39 insertions, 13 deletions
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