summaryrefslogtreecommitdiffstats
path: root/server/lib/httpresponse.js
blob: ff1010a9834d0181f79a31125d1c707c62ad92d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
const response = {}

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 = {}) => res.status(200).send({ message: `Successfully ${action} ${count} ${type + (count === 1 ? '' : 's')}.`, count, ...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 + '.'
})

module.exports = response