1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const { dirname, join } = require('path');
|
15 | const { read } = require('./filesystem');
|
16 | const { render } = require('./view');
|
17 |
|
18 | const cwd = process.cwd();
|
19 |
|
20 |
|
21 | const OK = (body = '', headers = {}) => {
|
22 | return { headers, body };
|
23 | };
|
24 |
|
25 | const Created = (resource = '', headers = {}) => {
|
26 | return {
|
27 | statusCode: 201,
|
28 | headers,
|
29 | body: resource
|
30 | };
|
31 | };
|
32 |
|
33 | const Accepted = (body = '', headers = {}) => {
|
34 | return {
|
35 | statusCode: 202,
|
36 | headers,
|
37 | body
|
38 | };
|
39 | };
|
40 |
|
41 | const NoContent = (headers = {}) => {
|
42 | return {
|
43 | statusCode: 204,
|
44 | headers,
|
45 | body: ''
|
46 | };
|
47 | };
|
48 |
|
49 | const NotFound = (headers = {}) => {
|
50 | return {
|
51 | statusCode: 404,
|
52 | headers,
|
53 | body: ''
|
54 | };
|
55 | };
|
56 |
|
57 | const Redirect = (url, body = 'Redirecting...', statusCode = 302) => {
|
58 | return {
|
59 | statusCode,
|
60 | headers: { Location: url },
|
61 | type: 'text/plain',
|
62 | body
|
63 | };
|
64 | };
|
65 |
|
66 | const JSONPayload = (content, statusCode = 200) => {
|
67 | return {
|
68 | statusCode,
|
69 | body: JSON.stringify(content),
|
70 | type: 'application/json'
|
71 | };
|
72 | };
|
73 |
|
74 | const 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 |
|
84 | const HTMLString = content => {
|
85 | return {
|
86 | statusCode: 200,
|
87 | type: 'text/html',
|
88 | body: content
|
89 | };
|
90 | };
|
91 |
|
92 | const Unauthorized = () => {
|
93 | return {
|
94 | statusCode: 401,
|
95 |
|
96 | body: ''
|
97 | };
|
98 | };
|
99 |
|
100 | const Forbidden = message => {
|
101 | return {
|
102 | statusCode: 403,
|
103 | body: message
|
104 | };
|
105 | };
|
106 |
|
107 | const InternalServerError = message => {
|
108 | return {
|
109 | statusCode: 500,
|
110 | body: message
|
111 | };
|
112 | };
|
113 |
|
114 | const cache = process.env.NODE_ENV === 'production';
|
115 |
|
116 | const 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 |
|
142 | module.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 | };
|