-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSON.helper.js
39 lines (37 loc) · 907 Bytes
/
JSON.helper.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
class JSONResponse {
/**
* Sends a successful json response to the request
* @param {*} res Res from the middleware
* @param {*} status Status number of the response
* @param {*} message Message to send along with the repsponse
* @param {*} data Relevant data
*/
static success(res, status = 200, message = 'Success', data = null) {
res.status(status).json({
status: status,
message,
data,
})
}
/**
* Sends an erroneous json response to the request
* @param {*} res Res from the middleware
* @param {*} status Status number of the response
* @param {*} message Message to send along with the repsponse
* @param {*} error Relevant error
*/
static error(
res,
status = 500,
message = 'Error',
error = new Error(message)
) {
console.error(error)
res.status(status).json({
status: status,
message,
error,
})
}
}
module.exports = JSONResponse