-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponses.js
72 lines (72 loc) · 2.08 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
module.exports = function createResponses(res) {
return {
generic: (msg) => {
res.status(200).send(msg);
},
success: (msg, data) => {
res.status(200).json({
status: "ok",
message: msg,
...data
});
},
successData: (data) => {
res.status(200).json(data);
},
successFile: (path) => {
res.type("application/octet-stream").status(200).sendFile(path, { maxAge: 2592000000 });
},
created: (msg, data) => {
res.status(201).json({
status: "ok",
message: msg,
...data
});
},
createdData: (data) => {
res.status(201).json(data);
},
badRequest: (msg = "Unexpected bad request") => {
res.status(400).json({
status: "error",
message: msg
});
},
unauthorized: (msg = "Not authorized") => {
res.status(401).json({
status: "error",
message: msg
})
},
notFound: (msg = "Resource not found") => {
res.status(404).json({
status: "error",
message: msg
});
},
unprocessable: (msg = "Bad request") => {
res.status(422).json({
status: "error",
message: msg
});
},
internalError: (msg = "Unexpected server error") => {
res.status(500).json({
status: "error",
message: msg
});
},
notImplemented: (msg = "Endpoint or method not implemented") => {
res.status(501).json({
status: "error",
message: msg
});
},
serviceUnavailable: (msg = "Unexpected fatal server error. Please try again later") => {
res.status(500).json({
status: "error",
message: msg
});
}
}
}