-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathgenerateRoute.js
More file actions
151 lines (141 loc) · 4.26 KB
/
generateRoute.js
File metadata and controls
151 lines (141 loc) · 4.26 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import React from 'react';
import chalk from 'chalk';
import { replaceValues } from '../utils/propsUtil';
import { renderPage } from './renderPage';
import { log } from './helpers';
import { ErrorMsg } from '../components/Error';
import { typeormParams } from './typeormParams';
async function paramfn(sq, req, res, next, options) {
// eslint-disable-next-line no-restricted-syntax
for (const param of sq) {
switch (param.type) {
case 'console.log': {
const colors = [
'black',
'red',
'green',
'yellow',
'blue',
'magenta',
'cyan',
'white',
'blackBright',
'redBright',
'greenBright',
'yellowBright',
'blueBright',
'magentaBright',
'cyanBright',
'whiteBright',
];
const bgColors = [
'bgBlack',
'bgRed',
'bgGreen',
'bgYellow',
'bgBlue',
'bgMagenta',
'bgCyan',
'bgWhite',
'bgBlackBright',
'bgRedBright',
'bgGreenBright',
'bgYellowBright',
'bgBlueBright',
'bgMagentaBright',
'bgCyanBright',
'bgWhiteBright',
];
if (!!param.content.color && !colors.includes(param.content.color))
throw new Error(`Wrong color value. Available colors: ${colors.join(', ')}`);
if (!!param.content.bgColor && !bgColors.includes(param.content.bgColor))
throw new Error(`Wrong background color value. Available colors: ${bgColors.join(', ')}`);
const logContent =
typeof param.content.children === 'function'
? param.content.children(req)
: param.content.children;
const key1 = param.content.color;
const key2 = param.content.bgColor;
if (!!key1 && !!key2) {
console.log(chalk[key1][key2](logContent));
} else if (key1) {
console.log(chalk[key1](logContent));
} else if (key2) {
console.log(chalk[key2](logContent));
} else {
console.log(logContent);
}
break;
}
case 'header':
res.setHeader(param.content.name, param.content.value);
break;
case 'json':
res.send(param.content);
break;
case 'text':
res.send(replaceValues(req, param.content));
break;
case 'status':
res.statusCode = param.content;
break;
case 'contentType':
res.setHeader('Content-Type', param.content);
break;
case 'redirect':
if (param.content.statusCode) res.redirect(param.content.statusCode, param.content.path);
else res.redirect(param.content.path);
// res.end();
break;
case 'render':
res.send(renderPage(param.content, { req, res }, options));
break;
case 'send-file':
res.sendFile(param.content.path, param.content.options, (err) => {
if (err) {
param.content.onError(err);
next();
}
});
break;
default:
break;
}
if (param.type.includes('typeorm')) {
typeormParams(param, req, res);
}
}
}
export function generateRoute(router, props, options = {}) {
router[props.method](
props.path || '/',
...[
...(props.middlewares || []),
async (req, res, next) => {
if (props.handler)
try {
await props.handler(req, res, next, (Component) =>
renderPage(Component, { req, res }, options)
);
} catch (error) {
const msg = `Error in the handler passed to this route <${props.method[0].toUpperCase()}${props.method.slice(
1
)} path="${props.path || '/'}">`;
log('error', msg);
res.writableEnded = true;
res.statusCode = 500;
if (props.method === 'get') {
res.end(
renderPage(() => <ErrorMsg msg={msg} error={error} />, { req, res }, options)
);
} else {
res.end(msg);
}
}
if (props.paramsSeq && !res.writableEnded) {
paramfn(props.paramsSeq, req, res, next, options);
}
},
]
);
}