UNPKG

3.45 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
14const { dirname, join } = require('path');
15const { read } = require('./filesystem');
16const { render } = require('./view');
17
18const cwd = process.cwd();
19// TODO auto-create those functions?
20
21const OK = (body = '', headers = {}) => {
22 return { headers, body };
23};
24
25const Created = (resource = '', headers = {}) => {
26 return {
27 statusCode: 201,
28 headers,
29 body: resource
30 };
31};
32
33const Accepted = (body = '', headers = {}) => {
34 return {
35 statusCode: 202,
36 headers,
37 body
38 };
39};
40
41const NoContent = (headers = {}) => {
42 return {
43 statusCode: 204,
44 headers,
45 body: ''
46 };
47};
48
49const NotFound = (headers = {}) => {
50 return {
51 statusCode: 404,
52 headers,
53 body: ''
54 };
55};
56
57const Redirect = (url, body = 'Redirecting...', statusCode = 302) => {
58 return {
59 statusCode,
60 headers: { Location: url },
61 type: 'text/plain',
62 body
63 };
64};
65
66const JSONPayload = (content, statusCode = 200) => {
67 return {
68 statusCode,
69 body: JSON.stringify(content),
70 type: 'application/json'
71 };
72};
73
74const HTMLStream = content => {
75 const Readable = require('stream').Readable;
76
77 const s = new Readable();
78 s.push(content);
79 s.push(null);
80
81 return s;
82};
83
84const HTMLString = content => {
85 return {
86 statusCode: 200,
87 type: 'text/html',
88 body: content
89 };
90};
91
92const Unauthorized = () => {
93 return {
94 statusCode: 401,
95 // TODO add WWW-Authenticate
96 body: ''
97 };
98};
99
100const Forbidden = message => {
101 return {
102 statusCode: 403,
103 body: message
104 };
105};
106
107const InternalServerError = message => {
108 return {
109 statusCode: 500,
110 body: message
111 };
112};
113
114const cache = process.env.NODE_ENV === 'production';
115
116const Page = async (location, context) => {
117 if (location.endsWith('.html')) {
118 const dir = dirname(location);
119 const paths = [dir];
120 const content = await read(location, { cache });
121 const html = await render(content.toString(), { context, paths });
122 return HTMLString(html);
123 } else if (location.includes('@')) {
124 const [name, feature] = location.split('@');
125 const views = join(cwd, 'views');
126 const dir = join(cwd, 'features', feature, 'Page');
127 const path = join(dir, `${name}.html`);
128 const paths = [dir, views];
129 const content = await read(path, { cache });
130 const html = await render(content.toString(), { context, paths });
131 return HTMLString(html);
132 } else {
133 const views = join(cwd, 'views');
134 const path = join(views, `${location}.html`);
135 const paths = [views];
136 const content = await read(path, { cache });
137 const html = await render(content.toString(), { context, paths });
138 return HTMLString(html);
139 }
140};
141
142module.exports = {
143 OK,
144 Created,
145 Accepted,
146 Redirect,
147 HTMLString,
148 HTMLStream,
149 JSONPayload,
150 NotFound,
151 NoContent,
152 Unauthorized,
153 Forbidden,
154 InternalServerError,
155 Page
156};