summaryrefslogtreecommitdiffstats
path: root/server/lib/httpresponse.js
blob: c402e397c62d2d6f3c55513afb2e583509c719ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class HttpResponse {
  constructor (httpStatus, idString, message, data = {}) {
    this.httpStatus = httpStatus
    this.idString = idString
    this.message = message
    this.data = 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({ ...this.data, ...response })
  }
}

// ############################################################################
// ############################## SUCCESS #####################################

// 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 })
}

// ############################################################################
// ############################## WARNING #####################################

HttpResponse.warningBatch = (action, type, successfull, count) => {
  const failed = count - successfull
  let tmpType = type
  if (failed > 1) tmpType += 's'
  if (Array.isArray(type)) type = type[successfull === 1 ? 0 : 1]
  else if (successfull !== 1) type += 's'
  return new HttpResponse(200, action.toUpperCase() + '_MULTIPLE', `Warning: ${action} ${successfull}/${count} ${type}. ${failed} ${tmpType} failed.`, { successfull, 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 + '.')
HttpResponse.noPermission = (id, permission) => new HttpResponse(403, 'NO_PERMISSION', 'Missing permission ' + permission + ' for object ' + id + '.')

// Authentication
HttpResponse.invalidToken = () => new HttpResponse(401, 'INVALID_TOKEN', 'The provided token is invalid.')

// ############################################################################
// ############################################################################

module.exports = HttpResponse