-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponses.js
49 lines (41 loc) · 831 Bytes
/
responses.js
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
/**
* @fileoverview Convenience objects for responding to HTTP requests.
*/
'use strict'
class Response {}
class ApiError extends Response {
constructor(status, message) {
super()
this._status = status
this._message = message
}
respond(res) {
res.status(this._status).send({error: this._message})
}
}
class ApiResponse extends Response {
constructor(status, data) {
super()
this._status = status
this._data = data
}
respond(res) {
res.status(this._status).send({data: this._data})
}
}
class TemplateResponse extends Response {
constructor(template, data) {
super()
this._template = template
this._data = data
}
respond(res) {
res.render(this._template, this._data)
}
}
module.exports = {
ApiError,
ApiResponse,
Response,
TemplateResponse,
}