1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const OK = (body = '', headers = {}) => {
|
17 | return { headers, body };
|
18 | };
|
19 |
|
20 | const Created = (resource = '', headers = {}) => {
|
21 | return {
|
22 | statusCode: 201,
|
23 | headers,
|
24 | body: resource
|
25 | };
|
26 | };
|
27 |
|
28 | const Accepted = (body = '', headers = {}) => {
|
29 | return {
|
30 | statusCode: 202,
|
31 | headers,
|
32 | body
|
33 | };
|
34 | };
|
35 |
|
36 | const NoContent = (headers = {}) => {
|
37 | return {
|
38 | statusCode: 204,
|
39 | headers,
|
40 | body: ''
|
41 | };
|
42 | };
|
43 |
|
44 | const NotFound = (headers = {}) => {
|
45 | return {
|
46 | statusCode: 404,
|
47 | headers,
|
48 | body: ''
|
49 | };
|
50 | };
|
51 |
|
52 | const redirect = (url, body = 'Redirecting...', status = '302 Found') => {
|
53 | return {
|
54 | status,
|
55 | headers: { Location: url },
|
56 | type: 'text/plain',
|
57 | body
|
58 | };
|
59 | };
|
60 |
|
61 | const json = (content, statusCode = 200) => {
|
62 | return {
|
63 | statusCode,
|
64 | body: JSON.stringify(content),
|
65 | type: 'application/json'
|
66 | };
|
67 | };
|
68 |
|
69 | const html = content => {
|
70 | return {
|
71 | statusCode: 200,
|
72 | type: 'text/html',
|
73 | body: content
|
74 | };
|
75 | };
|
76 |
|
77 | const Unauthorized = () => {
|
78 | return {
|
79 | statusCode: 401,
|
80 |
|
81 | body: ''
|
82 | };
|
83 | };
|
84 |
|
85 | const Forbidden = message => {
|
86 | return {
|
87 | statusCode: 403,
|
88 | body: message
|
89 | };
|
90 | };
|
91 |
|
92 | const InternalServerError = message => {
|
93 | return {
|
94 | statusCode: 500,
|
95 | body: message
|
96 | };
|
97 | };
|
98 |
|
99 | module.exports = {
|
100 | OK,
|
101 | Created,
|
102 | Accepted,
|
103 | redirect,
|
104 | html,
|
105 | json,
|
106 | NotFound,
|
107 | NoContent,
|
108 | Unauthorized,
|
109 | Forbidden,
|
110 | InternalServerError
|
111 | };
|