UNPKG

2.06 kBJavaScriptView Raw
1// Copyright 2019 Zaiste & contributors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14// TODO auto-create those functions?
15
16const OK = (body = '', headers = {}) => {
17 return { headers, body };
18};
19
20const Created = (resource = '', headers = {}) => {
21 return {
22 statusCode: 201,
23 headers,
24 body: resource
25 };
26};
27
28const Accepted = (body = '', headers = {}) => {
29 return {
30 statusCode: 202,
31 headers,
32 body
33 };
34};
35
36const NoContent = (headers = {}) => {
37 return {
38 statusCode: 204,
39 headers,
40 body: ''
41 };
42};
43
44const NotFound = (headers = {}) => {
45 return {
46 statusCode: 404,
47 headers,
48 body: ''
49 };
50};
51
52const redirect = (url, body = 'Redirecting...', status = '302 Found') => {
53 return {
54 status,
55 headers: { Location: url },
56 type: 'text/plain',
57 body
58 };
59};
60
61const json = (content, statusCode = 200) => {
62 return {
63 statusCode,
64 body: JSON.stringify(content),
65 type: 'application/json'
66 };
67};
68
69const html = content => {
70 return {
71 statusCode: 200,
72 type: 'text/html',
73 body: content
74 };
75};
76
77const Unauthorized = () => {
78 return {
79 statusCode: 401,
80 // TODO add WWW-Authenticate
81 body: ''
82 };
83};
84
85const Forbidden = message => {
86 return {
87 statusCode: 403,
88 body: message
89 };
90};
91
92const InternalServerError = message => {
93 return {
94 statusCode: 500,
95 body: message
96 };
97};
98
99module.exports = {
100 OK,
101 Created,
102 Accepted,
103 redirect,
104 html,
105 json,
106 NotFound,
107 NoContent,
108 Unauthorized,
109 Forbidden,
110 InternalServerError
111};