summaryrefslogblamecommitdiffstats
path: root/server/lib/httpresponse.js
blob: 4ef6ab1a11c7b5be090b68cacd950188dfb322ea (plain) (tree)
1
2
3
4
5
6
7
8


                                                                                                                                                




                                                                                                      



                                                                                                                                                                           

                                                                          

  
                         
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 = {}) => {
  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 })
}

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