summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUdo Walter2019-03-31 19:26:26 +0200
committerUdo Walter2019-03-31 19:26:26 +0200
commit86383d4f5108eef9ef42730150c19311e1637950 (patch)
tree4ae3340c740d05832e0f428c2ea1bf875954212e
parentMerge because Udo committed eslint errors :| (diff)
downloadbas-86383d4f5108eef9ef42730150c19311e1637950.tar.gz
bas-86383d4f5108eef9ef42730150c19311e1637950.tar.xz
bas-86383d4f5108eef9ef42730150c19311e1637950.zip
[server/httpresponse] small rework
-rw-r--r--server/api/clients.js19
-rw-r--r--server/api/groups.js38
-rw-r--r--server/api/ipxeentries.js22
-rw-r--r--server/lib/httpresponse.js52
-rw-r--r--webapp/src/components/LogModule.vue4
5 files changed, 81 insertions, 54 deletions
diff --git a/server/api/clients.js b/server/api/clients.js
index 6cd34a2..32b845d 100644
--- a/server/api/clients.js
+++ b/server/api/clients.js
@@ -6,7 +6,7 @@ const backendHelper = require(path.join(__appdir, 'lib', 'external-backends', 'b
var express = require('express')
const { decorateApp } = require('@awaitjs/express')
var router = decorateApp(express.Router())
-const httpResponse = require(path.join(__appdir, 'lib', 'httpresponse'))
+const HttpResponse = require(path.join(__appdir, 'lib', 'httpresponse'))
// ############################################################################
// ########################### GET requests #################################
@@ -17,9 +17,10 @@ router.getAsync('', async (req, res) => {
})
router.getAsync('/:id', async (req, res) => {
+ if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res)
const client = await db.client.findOne({ where: { id: req.params.id }, include: ['groups'] })
if (client) res.status(200).send(client)
- else httpResponse.notFound(res, req.params.id)
+ else HttpResponse.notFound(req.params.id).send(res)
})
// ############################################################################
@@ -27,10 +28,10 @@ router.getAsync('/:id', async (req, res) => {
router.postAsync(['', '/:id'], async (req, res) => {
if (req.query.delete !== undefined && req.query.delete !== 'false') {
- if (!Array.isArray(req.body.ids)) return httpResponse.invalidBodyValue(res, 'ids', 'an array')
+ if (!Array.isArray(req.body.ids)) return HttpResponse.invalidBodyValue('ids', 'an array').send(res)
await backendHelper.deleteClients(req.body.ids)
const count = await db.client.destroy({ where: { id: req.body.ids } })
- httpResponse.successBatch(res, 'deleted', 'client', count)
+ HttpResponse.successBatch('deleted', 'client', count).send(res)
} else {
let client
let action = 'updated'
@@ -40,13 +41,13 @@ router.postAsync(['', '/:id'], async (req, res) => {
io.in('broadcast newClient').emit('notifications newAlert', { type: 'info', text: 'New client!' })
} else if (req.params.id > 0) {
client = await db.client.findOne({ where: { id: req.params.id } })
- if (!client) return httpResponse.notFound(res, req.params.id)
+ if (!client) return HttpResponse.notFound(req.params.id).send(res)
else await client.update(req.body.data)
} else {
- return httpResponse.invalidId(res)
+ return HttpResponse.invalidId().send(res)
}
await client.setGroups(req.body.groupIds)
- httpResponse.success(res, action, 'client', client.id)
+ HttpResponse.success(action, 'client', client.id).send(res)
}
})
@@ -55,8 +56,8 @@ router.postAsync(['', '/:id'], async (req, res) => {
router.delete('/:id', async (req, res) => {
const count = await db.client.destroy({ where: { id: req.params.id } })
- if (count) httpResponse.success(res, 'deleted', 'client', req.params.id)
- else httpResponse.notFound(res, req.params.id)
+ if (count) HttpResponse.success('deleted', 'client', req.params.id).send(res)
+ else HttpResponse.notFound(req.params.id).send(res)
})
// ############################################################################
diff --git a/server/api/groups.js b/server/api/groups.js
index 99260ea..e3683bf 100644
--- a/server/api/groups.js
+++ b/server/api/groups.js
@@ -6,7 +6,7 @@ const ipHelper = require(path.join(__appdir, 'lib', 'iphelper'))
const express = require('express')
const { decorateApp } = require('@awaitjs/express')
const router = decorateApp(express.Router())
-const httpResponse = require(path.join(__appdir, 'lib', 'httpresponse'))
+const HttpResponse = require(path.join(__appdir, 'lib', 'httpresponse'))
// ############################################################################
// ########################### GET requests #################################
@@ -29,7 +29,7 @@ router.getAsync('/:id', async (req, res) => {
if (all) res.status(200).send({ ...group.get({ plain: true }), ...await groupUtil.getAllChildren([group]) })
else res.status(200).send(group)
} else {
- return httpResponse.notFound(res, req.params.id)
+ return HttpResponse.notFound(req.params.id).send(res)
}
} else if (req.params.id === '0') {
const [subgroups, clients] = await Promise.all([
@@ -38,7 +38,7 @@ router.getAsync('/:id', async (req, res) => {
])
res.send({ id: 0, subgroups, clients })
} else {
- httpResponse.invalidId(res, true)
+ HttpResponse.invalidId(true).send(res)
}
})
@@ -47,9 +47,9 @@ router.getAsync('/:id', async (req, res) => {
router.postAsync(['', '/:id'], async (req, res) => {
if (req.query.delete !== undefined && req.query.delete !== 'false') {
- if (!Array.isArray(req.body.ids)) return httpResponse.invalidBodyValue(res, 'ids', 'an array')
+ if (!Array.isArray(req.body.ids)) return HttpResponse.invalidBodyValue('ids', 'an array').send(res)
const count = await db.group.destroy({ where: { id: req.body.ids } })
- httpResponse.successBatch(res, 'deleted', 'group', count)
+ HttpResponse.successBatch('deleted', 'group', count).send(res)
} else {
let group
let action = 'updated'
@@ -58,10 +58,10 @@ router.postAsync(['', '/:id'], async (req, res) => {
action = 'created'
} else if (req.params.id > 0) {
group = await db.group.findOne({ where: { id: req.params.id }, include: ['ipranges'] })
- if (!group) return httpResponse.notFound(res, req.params.id)
+ if (!group) return HttpResponse.notFound(req.params.id).send(res)
else await group.update(req.body.data)
} else {
- return httpResponse.invalidId(res)
+ return HttpResponse.invalidId().send(res)
}
const promises = []
// Set group parents
@@ -96,39 +96,39 @@ router.postAsync(['', '/:id'], async (req, res) => {
}
}
await Promise.all(promises)
- httpResponse.success(res, action, 'group', group.id)
+ HttpResponse.success(action, 'group', group.id).send(res)
}
})
router.postAsync('/:id/subgroups', async (req, res) => {
- if (!(req.params.id > 0)) return httpResponse.invalidId(res)
+ if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res)
const group = await db.group.findOne({ where: { id: req.params.id } })
if (group) {
if (req.query.delete !== undefined && req.query.delete !== 'false') {
const count = await group.removeSubgroups(req.body.ids)
- httpResponse.successBatch(res, 'removed', 'subgroup', count)
+ HttpResponse.successBatch('removed', 'subgroup', count).send(res)
} else {
const count = await group.addSubgroups(req.body.ids)
- httpResponse.successBatch(res, 'added', 'subgroup', count)
+ HttpResponse.successBatch('added', 'subgroup', count).send(res)
}
} else {
- httpResponse.notFound(res, req.params.id)
+ HttpResponse.notFound(req.params.id).send(res)
}
})
router.postAsync('/:id/clients', async (req, res) => {
- if (!(req.params.id > 0)) return httpResponse.invalidId(res)
+ if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res)
const group = await db.group.findOne({ where: { id: req.params.id } })
if (group) {
if (req.query.delete !== undefined && req.query.delete !== 'false') {
const count = await group.removeClients(req.body.ids)
- httpResponse.successBatch(res, 'removed', 'client', count)
+ HttpResponse.successBatch('removed', 'client', count).send(res)
} else {
const count = await group.addClients(req.body.ids)
- httpResponse.successBatch(res, 'added', 'client', count)
+ HttpResponse.successBatch('added', 'client', count).send(res)
}
} else {
- httpResponse.notFound(res, req.params.id)
+ HttpResponse.notFound(req.params.id).send(res)
}
})
@@ -136,10 +136,10 @@ router.postAsync('/:id/clients', async (req, res) => {
// ########################## DELETE requests ###############################
router.delete('/:id', async (req, res) => {
- if (!(req.params.id > 0)) return httpResponse.invalidId(res)
+ if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res)
const count = await db.group.destroy({ where: { id: req.params.id } })
- if (count) httpResponse.success(res, 'deleted', 'group', req.params.id)
- else httpResponse.notFound(res, req.params.id)
+ if (count) HttpResponse.success('deleted', 'group', req.params.id).send(res)
+ else HttpResponse.notFound(req.params.id).send(res)
})
// ############################################################################
diff --git a/server/api/ipxeentries.js b/server/api/ipxeentries.js
index 45cdc9d..bfdb29d 100644
--- a/server/api/ipxeentries.js
+++ b/server/api/ipxeentries.js
@@ -4,7 +4,7 @@ var db = require(path.join(__appdir, 'lib', 'sequelize'))
var express = require('express')
const { decorateApp } = require('@awaitjs/express')
var router = decorateApp(express.Router())
-const httpResponse = require(path.join(__appdir, 'lib', 'httpresponse'))
+const HttpResponse = require(path.join(__appdir, 'lib', 'httpresponse'))
// ############################################################################
// ########################### GET requests #################################
@@ -15,10 +15,10 @@ router.getAsync('', async (req, res) => {
})
router.getAsync('/:id', async (req, res) => {
- if (!(req.params.id > 0)) httpResponse.invalidId(res)
+ if (!(req.params.id > 0)) HttpResponse.invalidId().send(res)
const entry = await db.entry.findOne({ where: { id: req.params.id } })
if (entry) res.status(200).send(entry)
- else httpResponse.notFound(res, req.params.id)
+ else HttpResponse.notFound(req.params.id).send(res)
})
// ############################################################################
@@ -26,9 +26,9 @@ router.getAsync('/:id', async (req, res) => {
router.postAsync(['', '/:id'], async (req, res) => {
if (req.query.delete !== undefined && req.query.delete !== 'false') {
- if (!Array.isArray(req.body.ids)) return httpResponse.invalidBodyValue(res, 'ids', 'an array')
+ if (!Array.isArray(req.body.ids)) return HttpResponse.invalidBodyValue('ids', 'an array').send(res)
const count = await db.entry.destroy({ where: { id: req.body.ids } })
- httpResponse.successBatch(res, 'deleted', ['ipxe entry', 'ipxe entries'], count)
+ HttpResponse.successBatch('deleted', ['ipxe entry', 'ipxe entries'], count).send(res)
} else {
let entry
let action = 'updated'
@@ -37,12 +37,12 @@ router.postAsync(['', '/:id'], async (req, res) => {
action = 'created'
} else if (req.params.id > 0) {
entry = await db.entry.findOne({ where: { id: req.params.id } })
- if (!entry) return httpResponse.notFound(res, req.params.id)
+ if (!entry) return HttpResponse.notFound(req.params.id).send(res)
else await entry.update(req.body.data)
} else {
- return httpResponse.invalidId(res)
+ return HttpResponse.invalidId().send(res)
}
- httpResponse.success(res, action, 'ipxe entry', entry.id)
+ HttpResponse.success(action, 'ipxe entry', entry.id).send(res)
}
})
@@ -50,10 +50,10 @@ router.postAsync(['', '/:id'], async (req, res) => {
// ########################## DELETE requests ###############################
router.deleteAsync('/:id', async (req, res) => {
- if (!(req.params.id > 0)) return httpResponse.invalidId(res)
+ if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res)
const count = await db.entry.destroy({ where: { id: req.params.id } })
- if (count) httpResponse.success(res, 'deleted', ['ipxe entry', 'ipxe entries'], req.params.id)
- else httpResponse.notFound(res, req.params.id)
+ if (count) HttpResponse.success('deleted', ['ipxe entry', 'ipxe entries'], req.params.id).send(res)
+ else HttpResponse.notFound(req.params.id).send(res)
})
// ############################################################################
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
diff --git a/webapp/src/components/LogModule.vue b/webapp/src/components/LogModule.vue
index e30d28e..6f580cb 100644
--- a/webapp/src/components/LogModule.vue
+++ b/webapp/src/components/LogModule.vue
@@ -217,8 +217,8 @@ export default {
data () {
return {
CATEGORIES: [
- { id: 1, name: 'CLIENT_REGISTRATION' },
- { id: 2, name: 'BACKEND_ERROR' }
+ { id: 1, name: 'REGISTRATION' },
+ { id: 2, name: 'ERROR_BACKEND' }
],
tabs: 0,
log: [],