@modern-js/bff-core 1.22.1 → 1.22.2-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +135 -17
- package/dist/js/modern/api.js +49 -34
- package/dist/js/modern/client/generate-client.js +52 -34
- package/dist/js/modern/client/index.js +1 -1
- package/dist/js/modern/client/result.js +10 -8
- package/dist/js/modern/errors/http.js +8 -14
- package/dist/js/modern/index.js +19 -3
- package/dist/js/modern/operators/http.js +139 -116
- package/dist/js/modern/router/constants.js +31 -13
- package/dist/js/modern/router/index.js +68 -131
- package/dist/js/modern/router/types.js +0 -1
- package/dist/js/modern/router/utils.js +36 -54
- package/dist/js/modern/types.js +45 -47
- package/dist/js/modern/utils/alias.js +35 -50
- package/dist/js/modern/utils/debug.js +5 -2
- package/dist/js/modern/utils/index.js +5 -2
- package/dist/js/modern/utils/meta.js +8 -4
- package/dist/js/modern/utils/storage.js +8 -14
- package/dist/js/modern/utils/validate.js +23 -29
- package/dist/js/node/api.js +78 -46
- package/dist/js/node/client/generate-client.js +85 -55
- package/dist/js/node/client/index.js +17 -18
- package/dist/js/node/client/result.js +32 -18
- package/dist/js/node/errors/http.js +28 -22
- package/dist/js/node/index.js +45 -122
- package/dist/js/node/operators/http.js +184 -169
- package/dist/js/node/router/constants.js +60 -28
- package/dist/js/node/router/index.js +112 -201
- package/dist/js/node/router/types.js +15 -5
- package/dist/js/node/router/utils.js +71 -78
- package/dist/js/node/types.js +71 -57
- package/dist/js/node/utils/alias.js +65 -73
- package/dist/js/node/utils/debug.js +27 -10
- package/dist/js/node/utils/index.js +28 -68
- package/dist/js/node/utils/meta.js +30 -12
- package/dist/js/node/utils/storage.js +36 -24
- package/dist/js/node/utils/validate.js +50 -44
- package/package.json +12 -40
|
@@ -1,104 +1,83 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import
|
|
4
|
-
import { fs, logger } from '@modern-js/utils';
|
|
5
|
-
import 'reflect-metadata';
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { fs, logger } from "@modern-js/utils";
|
|
3
|
+
import "reflect-metadata";
|
|
6
4
|
import { HttpMethod, httpMethods, OperatorType, TriggerType } from "../types";
|
|
7
5
|
import { debug } from "../utils";
|
|
8
|
-
import {
|
|
9
|
-
|
|
6
|
+
import {
|
|
7
|
+
APIMode,
|
|
8
|
+
FRAMEWORK_MODE_LAMBDA_DIR,
|
|
9
|
+
API_FILE_RULES,
|
|
10
|
+
FRAMEWORK_MODE_APP_DIR
|
|
11
|
+
} from "./constants";
|
|
12
|
+
import {
|
|
13
|
+
getFiles,
|
|
14
|
+
getPathFromFilename,
|
|
15
|
+
requireHandlerModule,
|
|
16
|
+
sortRoutes
|
|
17
|
+
} from "./utils";
|
|
10
18
|
export * from "./types";
|
|
11
19
|
export * from "./constants";
|
|
12
|
-
|
|
13
|
-
// lambdaDir is the dir which equal to the apiDir in function mode, and equal to the api/lambda dir in framework mode
|
|
20
|
+
class ApiRouter {
|
|
14
21
|
constructor({
|
|
15
|
-
apiDir
|
|
16
|
-
lambdaDir
|
|
22
|
+
apiDir,
|
|
23
|
+
lambdaDir,
|
|
17
24
|
prefix
|
|
18
25
|
}) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
_defineProperty(this, "apiDir", void 0);
|
|
22
|
-
|
|
23
|
-
_defineProperty(this, "existLambdaDir", void 0);
|
|
24
|
-
|
|
25
|
-
_defineProperty(this, "lambdaDir", void 0);
|
|
26
|
-
|
|
27
|
-
_defineProperty(this, "prefix", void 0);
|
|
28
|
-
|
|
29
|
-
_defineProperty(this, "apiFiles", []);
|
|
30
|
-
|
|
31
|
-
_defineProperty(this, "getExactApiMode", apiDir => {
|
|
26
|
+
this.apiFiles = [];
|
|
27
|
+
this.getExactApiMode = (apiDir) => {
|
|
32
28
|
const exist = this.createExistChecker(apiDir);
|
|
33
29
|
const existLambdaDir = exist(FRAMEWORK_MODE_LAMBDA_DIR);
|
|
34
30
|
const existAppDir = exist(FRAMEWORK_MODE_APP_DIR);
|
|
35
|
-
const existAppFile = exist(
|
|
36
|
-
|
|
31
|
+
const existAppFile = exist("app.ts") || exist("app.js");
|
|
37
32
|
if (existLambdaDir || existAppDir || existAppFile) {
|
|
38
33
|
return APIMode.FARMEWORK;
|
|
39
34
|
}
|
|
40
|
-
|
|
41
35
|
return APIMode.FUNCTION;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
_defineProperty(this, "getExactLambdaDir", apiDir => {
|
|
36
|
+
};
|
|
37
|
+
this.createExistChecker = (base) => (target) => fs.pathExistsSync(path.resolve(base, target));
|
|
38
|
+
this.getExactLambdaDir = (apiDir) => {
|
|
47
39
|
if (this.lambdaDir) {
|
|
48
40
|
return this.lambdaDir;
|
|
49
41
|
}
|
|
50
|
-
|
|
51
42
|
const lambdaDir = this.apiMode === APIMode.FARMEWORK ? path.join(apiDir, FRAMEWORK_MODE_LAMBDA_DIR) : apiDir;
|
|
52
43
|
return lambdaDir;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
this.validateAbsolute(
|
|
56
|
-
this.validateAbsolute(_lambdaDir, 'lambdaDir');
|
|
44
|
+
};
|
|
45
|
+
this.validateAbsolute(apiDir, "apiDir");
|
|
46
|
+
this.validateAbsolute(lambdaDir, "lambdaDir");
|
|
57
47
|
this.prefix = this.initPrefix(prefix);
|
|
58
|
-
this.apiDir =
|
|
59
|
-
this.apiMode = this.getExactApiMode(
|
|
60
|
-
this.lambdaDir =
|
|
48
|
+
this.apiDir = apiDir;
|
|
49
|
+
this.apiMode = this.getExactApiMode(apiDir);
|
|
50
|
+
this.lambdaDir = lambdaDir || this.getExactLambdaDir(this.apiDir);
|
|
61
51
|
this.existLambdaDir = fs.existsSync(this.lambdaDir);
|
|
62
52
|
}
|
|
63
|
-
|
|
64
53
|
isExistLambda() {
|
|
65
54
|
return this.existLambdaDir;
|
|
66
55
|
}
|
|
67
|
-
|
|
68
56
|
getApiMode() {
|
|
69
57
|
return this.apiMode;
|
|
70
58
|
}
|
|
71
|
-
|
|
72
59
|
getLambdaDir() {
|
|
73
60
|
return this.lambdaDir;
|
|
74
61
|
}
|
|
75
|
-
|
|
76
62
|
isApiFile(filename) {
|
|
77
63
|
if (this.existLambdaDir) {
|
|
78
64
|
return false;
|
|
79
65
|
}
|
|
80
|
-
|
|
81
66
|
if (!this.apiFiles.includes(filename)) {
|
|
82
67
|
return false;
|
|
83
68
|
}
|
|
84
|
-
|
|
85
69
|
return true;
|
|
86
70
|
}
|
|
87
|
-
|
|
88
71
|
getSingleModuleHandlers(filename) {
|
|
89
72
|
const moduleInfo = this.getModuleInfo(filename);
|
|
90
|
-
|
|
91
73
|
if (moduleInfo) {
|
|
92
74
|
return this.getModuleHandlerInfos(moduleInfo);
|
|
93
75
|
}
|
|
94
|
-
|
|
95
76
|
return null;
|
|
96
77
|
}
|
|
97
|
-
|
|
98
78
|
getHandlerInfo(filename, originFuncName, handler) {
|
|
99
79
|
const httpMethod = this.getHttpMethod(originFuncName, handler);
|
|
100
80
|
const routeName = this.getRouteName(filename, handler);
|
|
101
|
-
|
|
102
81
|
if (httpMethod && routeName) {
|
|
103
82
|
return {
|
|
104
83
|
handler,
|
|
@@ -109,135 +88,101 @@ export class ApiRouter {
|
|
|
109
88
|
routePath: this.getRoutePath(this.prefix, routeName)
|
|
110
89
|
};
|
|
111
90
|
}
|
|
112
|
-
|
|
113
91
|
return null;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
|
|
92
|
+
}
|
|
117
93
|
getSafeRoutePath(filename, handler) {
|
|
118
94
|
this.loadApiFiles();
|
|
119
95
|
this.validateValidApifile(filename);
|
|
120
96
|
return this.getRouteName(filename, handler);
|
|
121
97
|
}
|
|
122
|
-
|
|
123
98
|
getRouteName(filename, handler) {
|
|
124
99
|
if (handler) {
|
|
125
100
|
const trigger = Reflect.getMetadata(OperatorType.Trigger, handler);
|
|
126
|
-
|
|
127
101
|
if (trigger && trigger.type === TriggerType.Http) {
|
|
128
102
|
if (!trigger.path) {
|
|
129
|
-
throw new Error(
|
|
103
|
+
throw new Error(
|
|
104
|
+
`The http trigger ${trigger.name} needs to specify a path`
|
|
105
|
+
);
|
|
130
106
|
}
|
|
131
|
-
|
|
132
107
|
return trigger.path;
|
|
133
108
|
}
|
|
134
109
|
}
|
|
135
|
-
|
|
136
110
|
const routePath = getPathFromFilename(this.lambdaDir, filename);
|
|
137
111
|
return routePath;
|
|
138
112
|
}
|
|
139
|
-
|
|
140
113
|
getHttpMethod(originHandlerName, handler) {
|
|
141
114
|
if (handler) {
|
|
142
115
|
const trigger = Reflect.getMetadata(OperatorType.Trigger, handler);
|
|
143
|
-
|
|
144
116
|
if (trigger && httpMethods.includes(trigger.method)) {
|
|
145
117
|
return trigger.method;
|
|
146
118
|
}
|
|
147
119
|
}
|
|
148
|
-
|
|
149
120
|
const upperName = originHandlerName.toUpperCase();
|
|
150
|
-
|
|
151
121
|
switch (upperName) {
|
|
152
|
-
case
|
|
122
|
+
case "GET":
|
|
153
123
|
return HttpMethod.Get;
|
|
154
|
-
|
|
155
|
-
case 'POST':
|
|
124
|
+
case "POST":
|
|
156
125
|
return HttpMethod.Post;
|
|
157
|
-
|
|
158
|
-
case 'PUT':
|
|
126
|
+
case "PUT":
|
|
159
127
|
return HttpMethod.Put;
|
|
160
|
-
|
|
161
|
-
case
|
|
162
|
-
case 'DEL':
|
|
128
|
+
case "DELETE":
|
|
129
|
+
case "DEL":
|
|
163
130
|
return HttpMethod.Delete;
|
|
164
|
-
|
|
165
|
-
case 'CONNECT':
|
|
131
|
+
case "CONNECT":
|
|
166
132
|
return HttpMethod.Connect;
|
|
167
|
-
|
|
168
|
-
case 'TRACE':
|
|
133
|
+
case "TRACE":
|
|
169
134
|
return HttpMethod.Trace;
|
|
170
|
-
|
|
171
|
-
case 'PATCH':
|
|
135
|
+
case "PATCH":
|
|
172
136
|
return HttpMethod.Patch;
|
|
173
|
-
|
|
174
|
-
case 'OPTION':
|
|
137
|
+
case "OPTION":
|
|
175
138
|
return HttpMethod.Option;
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
return HttpMethod.Get;
|
|
180
|
-
}
|
|
181
|
-
|
|
139
|
+
case "DEFAULT": {
|
|
140
|
+
return HttpMethod.Get;
|
|
141
|
+
}
|
|
182
142
|
default:
|
|
183
|
-
logger.warn(
|
|
143
|
+
logger.warn(
|
|
144
|
+
`Only api handlers are allowd to be exported, please remove the function ${originHandlerName} from exports`
|
|
145
|
+
);
|
|
184
146
|
return null;
|
|
185
147
|
}
|
|
186
148
|
}
|
|
187
|
-
|
|
188
149
|
loadApiFiles() {
|
|
189
150
|
if (!this.existLambdaDir) {
|
|
190
151
|
return [];
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
|
|
152
|
+
}
|
|
194
153
|
const apiFiles = this.apiFiles = getFiles(this.lambdaDir, API_FILE_RULES);
|
|
195
154
|
return apiFiles;
|
|
196
155
|
}
|
|
197
|
-
|
|
198
156
|
getApiFiles() {
|
|
199
157
|
if (!this.existLambdaDir) {
|
|
200
158
|
return [];
|
|
201
159
|
}
|
|
202
|
-
|
|
203
160
|
if (this.apiFiles.length > 0) {
|
|
204
161
|
return this.apiFiles;
|
|
205
162
|
}
|
|
206
|
-
|
|
207
163
|
return this.loadApiFiles();
|
|
208
164
|
}
|
|
209
|
-
|
|
210
165
|
getApiHandlers() {
|
|
211
166
|
const filenames = this.getApiFiles();
|
|
212
167
|
const moduleInfos = this.getModuleInfos(filenames);
|
|
213
168
|
const apiHandlers = this.getHandlerInfos(moduleInfos);
|
|
214
|
-
debug(
|
|
169
|
+
debug("apiHandlers", apiHandlers.length, apiHandlers);
|
|
215
170
|
return apiHandlers;
|
|
216
171
|
}
|
|
217
|
-
/**
|
|
218
|
-
* 如果用户未传入或传入空串,默认为 /api
|
|
219
|
-
* 如果传入 /,则 prefix 为 /
|
|
220
|
-
*/
|
|
221
|
-
|
|
222
|
-
|
|
223
172
|
initPrefix(prefix) {
|
|
224
|
-
if (prefix ===
|
|
225
|
-
return
|
|
173
|
+
if (prefix === "/") {
|
|
174
|
+
return "";
|
|
226
175
|
}
|
|
227
|
-
|
|
228
|
-
return prefix || '/api';
|
|
176
|
+
return prefix || "/api";
|
|
229
177
|
}
|
|
230
|
-
|
|
231
178
|
validateAbsolute(filename, paramsName) {
|
|
232
|
-
if (typeof filename ===
|
|
179
|
+
if (typeof filename === "string" && !path.isAbsolute(filename)) {
|
|
233
180
|
throw new Error(`The ${paramsName} ${filename} is not a abolute path`);
|
|
234
181
|
}
|
|
235
182
|
}
|
|
236
|
-
|
|
237
183
|
getModuleInfos(filenames) {
|
|
238
|
-
return filenames.map(filename => this.getModuleInfo(filename)).filter(moduleInfo => Boolean(moduleInfo));
|
|
184
|
+
return filenames.map((filename) => this.getModuleInfo(filename)).filter((moduleInfo) => Boolean(moduleInfo));
|
|
239
185
|
}
|
|
240
|
-
|
|
241
186
|
getModuleInfo(filename) {
|
|
242
187
|
try {
|
|
243
188
|
const module = requireHandlerModule(filename);
|
|
@@ -246,7 +191,7 @@ export class ApiRouter {
|
|
|
246
191
|
module
|
|
247
192
|
};
|
|
248
193
|
} catch (err) {
|
|
249
|
-
if (process.env.NODE_ENV ===
|
|
194
|
+
if (process.env.NODE_ENV === "production") {
|
|
250
195
|
throw err;
|
|
251
196
|
} else {
|
|
252
197
|
console.error(err);
|
|
@@ -254,12 +199,10 @@ export class ApiRouter {
|
|
|
254
199
|
}
|
|
255
200
|
}
|
|
256
201
|
}
|
|
257
|
-
|
|
258
202
|
getHandlerInfos(moduleInfos) {
|
|
259
203
|
let apiHandlers = [];
|
|
260
|
-
moduleInfos.forEach(moduleInfo => {
|
|
204
|
+
moduleInfos.forEach((moduleInfo) => {
|
|
261
205
|
const handlerInfos = this.getModuleHandlerInfos(moduleInfo);
|
|
262
|
-
|
|
263
206
|
if (handlerInfos) {
|
|
264
207
|
apiHandlers = apiHandlers.concat(handlerInfos);
|
|
265
208
|
}
|
|
@@ -267,33 +210,27 @@ export class ApiRouter {
|
|
|
267
210
|
const sortedHandlers = sortRoutes(apiHandlers);
|
|
268
211
|
return sortedHandlers;
|
|
269
212
|
}
|
|
270
|
-
|
|
271
213
|
getModuleHandlerInfos(moduleInfo) {
|
|
272
|
-
const {
|
|
273
|
-
|
|
274
|
-
filename
|
|
275
|
-
} = moduleInfo;
|
|
276
|
-
return Object.entries(module).filter(([, handler]) => typeof handler === 'function').map(([key]) => {
|
|
214
|
+
const { module, filename } = moduleInfo;
|
|
215
|
+
return Object.entries(module).filter(([, handler]) => typeof handler === "function").map(([key]) => {
|
|
277
216
|
const handler = module[key];
|
|
278
217
|
const handlerInfo = this.getHandlerInfo(filename, key, handler);
|
|
279
218
|
return handlerInfo;
|
|
280
|
-
}).filter(handlerInfo => Boolean(handlerInfo));
|
|
219
|
+
}).filter((handlerInfo) => Boolean(handlerInfo));
|
|
281
220
|
}
|
|
282
|
-
|
|
283
221
|
validateValidApifile(filename) {
|
|
284
222
|
if (!this.apiFiles.includes(filename)) {
|
|
285
223
|
throw new Error(`The ${filename} is not a valid api file.`);
|
|
286
224
|
}
|
|
287
225
|
}
|
|
288
|
-
|
|
289
226
|
getRoutePath(prefix, routeName) {
|
|
290
|
-
const finalRouteName = routeName ===
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
return '/';
|
|
227
|
+
const finalRouteName = routeName === "/" ? "" : routeName;
|
|
228
|
+
if (prefix === "" && finalRouteName === "") {
|
|
229
|
+
return "/";
|
|
294
230
|
}
|
|
295
|
-
|
|
296
231
|
return `${prefix}${finalRouteName}`;
|
|
297
232
|
}
|
|
298
|
-
|
|
299
|
-
|
|
233
|
+
}
|
|
234
|
+
export {
|
|
235
|
+
ApiRouter
|
|
236
|
+
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,101 +1,83 @@
|
|
|
1
|
-
import path from
|
|
2
|
-
import { globby } from
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { globby } from "@modern-js/utils";
|
|
3
3
|
import { INDEX_SUFFIX } from "./constants";
|
|
4
|
-
|
|
4
|
+
const getFiles = (lambdaDir, rules) => globby.sync(rules, {
|
|
5
5
|
cwd: lambdaDir,
|
|
6
6
|
gitignore: true
|
|
7
|
-
}).map(file => path.resolve(lambdaDir, file));
|
|
8
|
-
|
|
7
|
+
}).map((file) => path.resolve(lambdaDir, file));
|
|
8
|
+
const getPathFromFilename = (baseDir, filename) => {
|
|
9
9
|
const relativeName = filename.substring(baseDir.length);
|
|
10
|
-
const relativePath = relativeName.split(
|
|
11
|
-
const nameSplit = relativePath.split(path.sep).map(item => {
|
|
10
|
+
const relativePath = relativeName.split(".").slice(0, -1).join(".");
|
|
11
|
+
const nameSplit = relativePath.split(path.sep).map((item) => {
|
|
12
12
|
if (item.length > 2) {
|
|
13
|
-
if (item.startsWith(
|
|
13
|
+
if (item.startsWith("[") && item.endsWith("]")) {
|
|
14
14
|
return `:${item.substring(1, item.length - 1)}`;
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
|
-
|
|
18
17
|
return item;
|
|
19
18
|
});
|
|
20
|
-
const name = nameSplit.join(
|
|
19
|
+
const name = nameSplit.join("/");
|
|
21
20
|
const finalName = name.endsWith(INDEX_SUFFIX) ? name.substring(0, name.length - INDEX_SUFFIX.length) : name;
|
|
22
21
|
return clearRouteName(finalName);
|
|
23
22
|
};
|
|
24
|
-
|
|
25
|
-
const clearRouteName = routeName => {
|
|
23
|
+
const clearRouteName = (routeName) => {
|
|
26
24
|
let finalRouteName = routeName.trim();
|
|
27
|
-
|
|
28
|
-
if (!finalRouteName.startsWith('/')) {
|
|
25
|
+
if (!finalRouteName.startsWith("/")) {
|
|
29
26
|
finalRouteName = `/${finalRouteName}`;
|
|
30
27
|
}
|
|
31
|
-
|
|
32
|
-
if (finalRouteName.length > 1 && finalRouteName.endsWith('/')) {
|
|
28
|
+
if (finalRouteName.length > 1 && finalRouteName.endsWith("/")) {
|
|
33
29
|
finalRouteName = finalRouteName.substring(0, finalRouteName.length - 1);
|
|
34
30
|
}
|
|
35
|
-
|
|
36
31
|
return finalRouteName;
|
|
37
32
|
};
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const enableRegister = requireFn => {
|
|
42
|
-
// esbuild-register 做 unRegister 时,不会删除 register 添加的 require.extensions,导致第二次调用时 require.extensions['.ts'] 是 nodejs 默认 loader
|
|
43
|
-
// 所以这里根据第一次调用时,require.extensions 有没有,来判断是否需要使用 esbuild-register
|
|
33
|
+
const isHandler = (input) => input && typeof input === "function";
|
|
34
|
+
const enableRegister = (requireFn) => {
|
|
44
35
|
let existTsLoader = false;
|
|
45
36
|
let firstCall = true;
|
|
46
|
-
return modulePath => {
|
|
37
|
+
return (modulePath) => {
|
|
47
38
|
if (firstCall) {
|
|
48
|
-
|
|
49
|
-
existTsLoader = Boolean(require.extensions['.ts']);
|
|
39
|
+
existTsLoader = Boolean(require.extensions[".ts"]);
|
|
50
40
|
firstCall = false;
|
|
51
41
|
}
|
|
52
|
-
|
|
53
42
|
if (!existTsLoader) {
|
|
54
43
|
const {
|
|
55
44
|
register
|
|
56
|
-
} = require(
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
unregister
|
|
60
|
-
} = register({
|
|
61
|
-
extensions: ['.ts']
|
|
45
|
+
} = require("esbuild-register/dist/node");
|
|
46
|
+
const { unregister } = register({
|
|
47
|
+
extensions: [".ts"]
|
|
62
48
|
});
|
|
63
|
-
const
|
|
49
|
+
const requiredModule2 = requireFn(modulePath);
|
|
64
50
|
unregister();
|
|
65
|
-
return
|
|
51
|
+
return requiredModule2;
|
|
66
52
|
}
|
|
67
|
-
|
|
68
53
|
const requiredModule = requireFn(modulePath);
|
|
69
54
|
return requiredModule;
|
|
70
55
|
};
|
|
71
56
|
};
|
|
72
|
-
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
export const requireHandlerModule = enableRegister(modulePath => {
|
|
76
|
-
// 测试环境不走缓存,因为缓存的 h andler 文件,会被 mockAPI 函数进行 mock,升级 jest28,setupFilesAfterEnv 能做异步操作的话,可解此问题
|
|
77
|
-
const originRequire = process.env.NODE_ENV === 'test' ? jest.requireActual : require;
|
|
57
|
+
const isFunction = (input) => input && {}.toString.call(input) === "[object Function]";
|
|
58
|
+
const requireHandlerModule = enableRegister((modulePath) => {
|
|
59
|
+
const originRequire = process.env.NODE_ENV === "test" ? jest.requireActual : require;
|
|
78
60
|
const module = originRequire(modulePath);
|
|
79
|
-
|
|
80
61
|
if (isFunction(module)) {
|
|
81
|
-
return {
|
|
82
|
-
default: module
|
|
83
|
-
};
|
|
62
|
+
return { default: module };
|
|
84
63
|
}
|
|
85
|
-
|
|
86
64
|
return module;
|
|
87
65
|
});
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
if (routePath.includes(':')) {
|
|
66
|
+
const routeValue = (routePath) => {
|
|
67
|
+
if (routePath.includes(":")) {
|
|
91
68
|
return 11;
|
|
92
69
|
}
|
|
93
|
-
|
|
94
70
|
return 1;
|
|
95
71
|
};
|
|
96
|
-
|
|
97
|
-
export const sortRoutes = apiHandlers => {
|
|
72
|
+
const sortRoutes = (apiHandlers) => {
|
|
98
73
|
return apiHandlers.sort((handlerA, handlerB) => {
|
|
99
74
|
return routeValue(handlerA.routeName) - routeValue(handlerB.routeName);
|
|
100
75
|
});
|
|
101
|
-
};
|
|
76
|
+
};
|
|
77
|
+
export {
|
|
78
|
+
getFiles,
|
|
79
|
+
getPathFromFilename,
|
|
80
|
+
isHandler,
|
|
81
|
+
requireHandlerModule,
|
|
82
|
+
sortRoutes
|
|
83
|
+
};
|
package/dist/js/modern/types.js
CHANGED
|
@@ -1,47 +1,45 @@
|
|
|
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
|
-
HttpMethod
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export const httpMethods = Object.values(HttpMethod);
|
|
1
|
+
var OperatorType = /* @__PURE__ */ ((OperatorType2) => {
|
|
2
|
+
OperatorType2[OperatorType2["Trigger"] = 0] = "Trigger";
|
|
3
|
+
OperatorType2[OperatorType2["Middleware"] = 1] = "Middleware";
|
|
4
|
+
return OperatorType2;
|
|
5
|
+
})(OperatorType || {});
|
|
6
|
+
var TriggerType = /* @__PURE__ */ ((TriggerType2) => {
|
|
7
|
+
TriggerType2[TriggerType2["Http"] = 0] = "Http";
|
|
8
|
+
return TriggerType2;
|
|
9
|
+
})(TriggerType || {});
|
|
10
|
+
var HttpMetadata = /* @__PURE__ */ ((HttpMetadata2) => {
|
|
11
|
+
HttpMetadata2["Method"] = "METHOD";
|
|
12
|
+
HttpMetadata2["Data"] = "DATA";
|
|
13
|
+
HttpMetadata2["Query"] = "QUERY";
|
|
14
|
+
HttpMetadata2["Params"] = "PARAMS";
|
|
15
|
+
HttpMetadata2["Headers"] = "HEADERS";
|
|
16
|
+
HttpMetadata2["Response"] = "RESPONSE";
|
|
17
|
+
return HttpMetadata2;
|
|
18
|
+
})(HttpMetadata || {});
|
|
19
|
+
var ResponseMetaType = /* @__PURE__ */ ((ResponseMetaType2) => {
|
|
20
|
+
ResponseMetaType2[ResponseMetaType2["StatusCode"] = 0] = "StatusCode";
|
|
21
|
+
ResponseMetaType2[ResponseMetaType2["Redirect"] = 1] = "Redirect";
|
|
22
|
+
ResponseMetaType2[ResponseMetaType2["Headers"] = 2] = "Headers";
|
|
23
|
+
return ResponseMetaType2;
|
|
24
|
+
})(ResponseMetaType || {});
|
|
25
|
+
var HttpMethod = /* @__PURE__ */ ((HttpMethod2) => {
|
|
26
|
+
HttpMethod2["Get"] = "GET";
|
|
27
|
+
HttpMethod2["Post"] = "POST";
|
|
28
|
+
HttpMethod2["Put"] = "PUT";
|
|
29
|
+
HttpMethod2["Delete"] = "DELETE";
|
|
30
|
+
HttpMethod2["Connect"] = "CONNECT";
|
|
31
|
+
HttpMethod2["Trace"] = "TRACE";
|
|
32
|
+
HttpMethod2["Patch"] = "PATCH";
|
|
33
|
+
HttpMethod2["Option"] = "OPTION";
|
|
34
|
+
HttpMethod2["Head"] = "HEAD";
|
|
35
|
+
return HttpMethod2;
|
|
36
|
+
})(HttpMethod || {});
|
|
37
|
+
const httpMethods = Object.values(HttpMethod);
|
|
38
|
+
export {
|
|
39
|
+
HttpMetadata,
|
|
40
|
+
HttpMethod,
|
|
41
|
+
OperatorType,
|
|
42
|
+
ResponseMetaType,
|
|
43
|
+
TriggerType,
|
|
44
|
+
httpMethods
|
|
45
|
+
};
|