-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
188 lines (153 loc) · 5.73 KB
/
index.js
File metadata and controls
188 lines (153 loc) · 5.73 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const mountPath = '/__swagger__escode__codegen__/save';
const chalk = require('chalk');
const fs = require('fs');
const path = require('path');
const compile = require('./lib/javascript');
const { exec } = require('child_process');
const isSwaggerLike = (value) => value && value.swagger && value.paths;
const isObject = function (val) {
return Object.prototype.toString.call(val) === '[object Object]';
}
const isArray = function (val) {
return Object.prototype.toString.call(val) === '[object Array]';
}
const isNotEmpty = function (val) { return val !== null && val != undefined; };
const isString = function (val) {
return Object.prototype.toString.call(val) === '[object String]';
}
const isFunction = function (val) {
return Object.prototype.toString.call(val) === '[object Function]';
}
const notEmptyNotString = function (val) {
return isNotEmpty(val) && !isString(val);
}
const urlPattern = /^((https?:)?\/\/([^/:]*))/;
const throwError = function (msg) {
console.error(chalk.red(msg));
console.log();
throw new Error();
}
const checkSettings = function (settings) {
const { prettyCmd, swaggerSavePath, codegen, homePage } = settings;
if (!isArray(homePage) && notEmptyNotString(homePage)) {
throwError('homePage type should be string or Array<string>');
}
const swaggerUis = isArray(homePage) ? homePage : [homePage];
swaggerUis.forEach(function (ui) {
if (!urlPattern.test(ui)) {
throwError(`homePage [${ui}] is not valid http[s] url`);
}
});
if (!isObject(codegen)) {
throwError('codegen type should be object');
}
const { tsType, tsApi, httpBase, responseWrapperName, responseWrapperPath, transformFileName } = codegen;
if (!isString(tsType)) {
throwError('tsType type should be string');
}
if (!isString(tsApi)) {
throwError('tsApi type should be string');
}
if (!isString(httpBase)) {
throwError('httpBase type should be string');
}
if (notEmptyNotString(responseWrapperPath)) {
throwError('responseWrapperPath type should be string or null');
}
if (notEmptyNotString(responseWrapperName)) {
throwError('responseWrapperName type should be string or null');
}
if (isNotEmpty(transformFileName) && !isFunction(transformFileName)) {
throwError('transformFileName type should be function or null');
}
if (notEmptyNotString(swaggerSavePath)) {
throwError('swaggerSavePath type should be string or null');
}
if (notEmptyNotString(prettyCmd)) {
throwError('prettyCmd type should be string or null');
}
}
const checkParams = function (params) {
if (!isArray(params)) {
throwError('params type should be Array<object>');
}
params.forEach(checkSettings);
}
module.exports = function (params) {
checkParams(params);
const urlMapIndex = params.reduce(function (total, item) {
const { homePage } = item;
const swaggerUis = isArray(homePage) ? homePage : [homePage];
swaggerUis.forEach(function (ui) {
total[ui] = item;
});
return total;
}, {});
const swaggerUrls = Object.keys(urlMapIndex);
return function (req, res, next) {
if (!/POST/.test(req.method) || req.path !== mountPath) {
next();
return;
}
const { swagger, documentUrl, swaggerUrl } = req.body || {};
if (!isSwaggerLike(swagger) || !documentUrl || !swaggerUrl) {
res.status(400).send('correct body structure: { swagger, documentUrl, swaggerUrl }');
return;
}
const equalMatchUrl = swaggerUrls.find(ui => documentUrl === ui);
const startMatchUrl = swaggerUrls.find(ui => documentUrl.indexOf(ui) === 0);
const isAllowed = equalMatchUrl || startMatchUrl;
if (!isAllowed) {
res.status(403).send('Allowd Hostnames: ' + swaggerUrls.join(', '));
return;
}
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,HEAD');
res.header('Access-Control-Max-Age', 3600);
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
if ('OPTIONS' === req.method) {
res.sendStatus(200);
return;
}
const settings = urlMapIndex[isAllowed];
const { prettyCmd, swaggerSavePath, codegen, } = settings;
const { tsType,
tsApi,
httpBase,
responseWrapperPath,
responseWrapperName,
transformFileName, } = codegen;
try {
if (swaggerSavePath) {
fs.writeFileSync(path.resolve(process.cwd(), swaggerSavePath), JSON.stringify(swagger, null, 2));
}
compile({
swagger,
tsType,
tsControler: tsApi,
httpBase,
// targetFolder: 'src/codegen/service/temp/api',
// difinitionFolder: 'src/codegen/service/temp/typedef',
commonType: responseWrapperPath,
commonTypeName: responseWrapperName,
getAPIFileName: transformFileName,
importStar: false,
});
} catch (e) {
console.error(e);
}
res.sendStatus(200);
if (prettyCmd) {
exec(prettyCmd, { cwd: process.cwd() }, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
if (stderr) {
console.error(`stderr: ${stderr}`);
}
});
}
}
}