@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,147 +1,102 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var
|
|
7
|
-
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
10
|
};
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
var
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (key in exports && exports[key] === _constants[key]) return;
|
|
27
|
-
Object.defineProperty(exports, key, {
|
|
28
|
-
enumerable: true,
|
|
29
|
-
get: function () {
|
|
30
|
-
return _constants[key];
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
var _utils3 = require("./utils");
|
|
36
|
-
|
|
37
|
-
var _types2 = require("./types");
|
|
38
|
-
|
|
39
|
-
Object.keys(_types2).forEach(function (key) {
|
|
40
|
-
if (key === "default" || key === "__esModule") return;
|
|
41
|
-
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
42
|
-
if (key in exports && exports[key] === _types2[key]) return;
|
|
43
|
-
Object.defineProperty(exports, key, {
|
|
44
|
-
enumerable: true,
|
|
45
|
-
get: function () {
|
|
46
|
-
return _types2[key];
|
|
47
|
-
}
|
|
48
|
-
});
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
|
+
var router_exports = {};
|
|
26
|
+
__export(router_exports, {
|
|
27
|
+
ApiRouter: () => ApiRouter
|
|
49
28
|
});
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
29
|
+
module.exports = __toCommonJS(router_exports);
|
|
30
|
+
var import_path = __toESM(require("path"));
|
|
31
|
+
var import_utils = require("@modern-js/utils");
|
|
32
|
+
var import_reflect_metadata = require("reflect-metadata");
|
|
33
|
+
var import_types = require("../types");
|
|
34
|
+
var import_utils2 = require("../utils");
|
|
35
|
+
var import_constants = require("./constants");
|
|
36
|
+
var import_utils3 = require("./utils");
|
|
37
|
+
__reExport(router_exports, require("./types"), module.exports);
|
|
38
|
+
__reExport(router_exports, require("./constants"), module.exports);
|
|
55
39
|
class ApiRouter {
|
|
56
|
-
// lambdaDir is the dir which equal to the apiDir in function mode, and equal to the api/lambda dir in framework mode
|
|
57
40
|
constructor({
|
|
58
|
-
apiDir
|
|
59
|
-
lambdaDir
|
|
41
|
+
apiDir,
|
|
42
|
+
lambdaDir,
|
|
60
43
|
prefix
|
|
61
44
|
}) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
_defineProperty(this, "apiDir", void 0);
|
|
65
|
-
|
|
66
|
-
_defineProperty(this, "existLambdaDir", void 0);
|
|
67
|
-
|
|
68
|
-
_defineProperty(this, "lambdaDir", void 0);
|
|
69
|
-
|
|
70
|
-
_defineProperty(this, "prefix", void 0);
|
|
71
|
-
|
|
72
|
-
_defineProperty(this, "apiFiles", []);
|
|
73
|
-
|
|
74
|
-
_defineProperty(this, "getExactApiMode", apiDir => {
|
|
45
|
+
this.apiFiles = [];
|
|
46
|
+
this.getExactApiMode = (apiDir) => {
|
|
75
47
|
const exist = this.createExistChecker(apiDir);
|
|
76
|
-
const existLambdaDir = exist(
|
|
77
|
-
const existAppDir = exist(
|
|
78
|
-
const existAppFile = exist(
|
|
79
|
-
|
|
48
|
+
const existLambdaDir = exist(import_constants.FRAMEWORK_MODE_LAMBDA_DIR);
|
|
49
|
+
const existAppDir = exist(import_constants.FRAMEWORK_MODE_APP_DIR);
|
|
50
|
+
const existAppFile = exist("app.ts") || exist("app.js");
|
|
80
51
|
if (existLambdaDir || existAppDir || existAppFile) {
|
|
81
|
-
return
|
|
52
|
+
return import_constants.APIMode.FARMEWORK;
|
|
82
53
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
_defineProperty(this, "createExistChecker", base => target => _utils.fs.pathExistsSync(_path.default.resolve(base, target)));
|
|
88
|
-
|
|
89
|
-
_defineProperty(this, "getExactLambdaDir", apiDir => {
|
|
54
|
+
return import_constants.APIMode.FUNCTION;
|
|
55
|
+
};
|
|
56
|
+
this.createExistChecker = (base) => (target) => import_utils.fs.pathExistsSync(import_path.default.resolve(base, target));
|
|
57
|
+
this.getExactLambdaDir = (apiDir) => {
|
|
90
58
|
if (this.lambdaDir) {
|
|
91
59
|
return this.lambdaDir;
|
|
92
60
|
}
|
|
93
|
-
|
|
94
|
-
const lambdaDir = this.apiMode === _constants.APIMode.FARMEWORK ? _path.default.join(apiDir, _constants.FRAMEWORK_MODE_LAMBDA_DIR) : apiDir;
|
|
61
|
+
const lambdaDir = this.apiMode === import_constants.APIMode.FARMEWORK ? import_path.default.join(apiDir, import_constants.FRAMEWORK_MODE_LAMBDA_DIR) : apiDir;
|
|
95
62
|
return lambdaDir;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
this.validateAbsolute(
|
|
99
|
-
this.validateAbsolute(_lambdaDir, 'lambdaDir');
|
|
63
|
+
};
|
|
64
|
+
this.validateAbsolute(apiDir, "apiDir");
|
|
65
|
+
this.validateAbsolute(lambdaDir, "lambdaDir");
|
|
100
66
|
this.prefix = this.initPrefix(prefix);
|
|
101
|
-
this.apiDir =
|
|
102
|
-
this.apiMode = this.getExactApiMode(
|
|
103
|
-
this.lambdaDir =
|
|
104
|
-
this.existLambdaDir =
|
|
67
|
+
this.apiDir = apiDir;
|
|
68
|
+
this.apiMode = this.getExactApiMode(apiDir);
|
|
69
|
+
this.lambdaDir = lambdaDir || this.getExactLambdaDir(this.apiDir);
|
|
70
|
+
this.existLambdaDir = import_utils.fs.existsSync(this.lambdaDir);
|
|
105
71
|
}
|
|
106
|
-
|
|
107
72
|
isExistLambda() {
|
|
108
73
|
return this.existLambdaDir;
|
|
109
74
|
}
|
|
110
|
-
|
|
111
75
|
getApiMode() {
|
|
112
76
|
return this.apiMode;
|
|
113
77
|
}
|
|
114
|
-
|
|
115
78
|
getLambdaDir() {
|
|
116
79
|
return this.lambdaDir;
|
|
117
80
|
}
|
|
118
|
-
|
|
119
81
|
isApiFile(filename) {
|
|
120
82
|
if (this.existLambdaDir) {
|
|
121
83
|
return false;
|
|
122
84
|
}
|
|
123
|
-
|
|
124
85
|
if (!this.apiFiles.includes(filename)) {
|
|
125
86
|
return false;
|
|
126
87
|
}
|
|
127
|
-
|
|
128
88
|
return true;
|
|
129
89
|
}
|
|
130
|
-
|
|
131
90
|
getSingleModuleHandlers(filename) {
|
|
132
91
|
const moduleInfo = this.getModuleInfo(filename);
|
|
133
|
-
|
|
134
92
|
if (moduleInfo) {
|
|
135
93
|
return this.getModuleHandlerInfos(moduleInfo);
|
|
136
94
|
}
|
|
137
|
-
|
|
138
95
|
return null;
|
|
139
96
|
}
|
|
140
|
-
|
|
141
97
|
getHandlerInfo(filename, originFuncName, handler) {
|
|
142
98
|
const httpMethod = this.getHttpMethod(originFuncName, handler);
|
|
143
99
|
const routeName = this.getRouteName(filename, handler);
|
|
144
|
-
|
|
145
100
|
if (httpMethod && routeName) {
|
|
146
101
|
return {
|
|
147
102
|
handler,
|
|
@@ -152,145 +107,110 @@ class ApiRouter {
|
|
|
152
107
|
routePath: this.getRoutePath(this.prefix, routeName)
|
|
153
108
|
};
|
|
154
109
|
}
|
|
155
|
-
|
|
156
110
|
return null;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
|
|
111
|
+
}
|
|
160
112
|
getSafeRoutePath(filename, handler) {
|
|
161
113
|
this.loadApiFiles();
|
|
162
114
|
this.validateValidApifile(filename);
|
|
163
115
|
return this.getRouteName(filename, handler);
|
|
164
116
|
}
|
|
165
|
-
|
|
166
117
|
getRouteName(filename, handler) {
|
|
167
118
|
if (handler) {
|
|
168
|
-
const trigger = Reflect.getMetadata(
|
|
169
|
-
|
|
170
|
-
if (trigger && trigger.type === _types.TriggerType.Http) {
|
|
119
|
+
const trigger = Reflect.getMetadata(import_types.OperatorType.Trigger, handler);
|
|
120
|
+
if (trigger && trigger.type === import_types.TriggerType.Http) {
|
|
171
121
|
if (!trigger.path) {
|
|
172
|
-
throw new Error(
|
|
122
|
+
throw new Error(
|
|
123
|
+
`The http trigger ${trigger.name} needs to specify a path`
|
|
124
|
+
);
|
|
173
125
|
}
|
|
174
|
-
|
|
175
126
|
return trigger.path;
|
|
176
127
|
}
|
|
177
128
|
}
|
|
178
|
-
|
|
179
|
-
const routePath = (0, _utils3.getPathFromFilename)(this.lambdaDir, filename);
|
|
129
|
+
const routePath = (0, import_utils3.getPathFromFilename)(this.lambdaDir, filename);
|
|
180
130
|
return routePath;
|
|
181
131
|
}
|
|
182
|
-
|
|
183
132
|
getHttpMethod(originHandlerName, handler) {
|
|
184
133
|
if (handler) {
|
|
185
|
-
const trigger = Reflect.getMetadata(
|
|
186
|
-
|
|
187
|
-
if (trigger && _types.httpMethods.includes(trigger.method)) {
|
|
134
|
+
const trigger = Reflect.getMetadata(import_types.OperatorType.Trigger, handler);
|
|
135
|
+
if (trigger && import_types.httpMethods.includes(trigger.method)) {
|
|
188
136
|
return trigger.method;
|
|
189
137
|
}
|
|
190
138
|
}
|
|
191
|
-
|
|
192
139
|
const upperName = originHandlerName.toUpperCase();
|
|
193
|
-
|
|
194
140
|
switch (upperName) {
|
|
195
|
-
case
|
|
196
|
-
return
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
case
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
case
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
case
|
|
209
|
-
return
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
return _types.HttpMethod.Patch;
|
|
216
|
-
|
|
217
|
-
case 'OPTION':
|
|
218
|
-
return _types.HttpMethod.Option;
|
|
219
|
-
|
|
220
|
-
case 'DEFAULT':
|
|
221
|
-
{
|
|
222
|
-
return _types.HttpMethod.Get;
|
|
223
|
-
}
|
|
224
|
-
|
|
141
|
+
case "GET":
|
|
142
|
+
return import_types.HttpMethod.Get;
|
|
143
|
+
case "POST":
|
|
144
|
+
return import_types.HttpMethod.Post;
|
|
145
|
+
case "PUT":
|
|
146
|
+
return import_types.HttpMethod.Put;
|
|
147
|
+
case "DELETE":
|
|
148
|
+
case "DEL":
|
|
149
|
+
return import_types.HttpMethod.Delete;
|
|
150
|
+
case "CONNECT":
|
|
151
|
+
return import_types.HttpMethod.Connect;
|
|
152
|
+
case "TRACE":
|
|
153
|
+
return import_types.HttpMethod.Trace;
|
|
154
|
+
case "PATCH":
|
|
155
|
+
return import_types.HttpMethod.Patch;
|
|
156
|
+
case "OPTION":
|
|
157
|
+
return import_types.HttpMethod.Option;
|
|
158
|
+
case "DEFAULT": {
|
|
159
|
+
return import_types.HttpMethod.Get;
|
|
160
|
+
}
|
|
225
161
|
default:
|
|
226
|
-
|
|
227
|
-
|
|
162
|
+
import_utils.logger.warn(
|
|
163
|
+
`Only api handlers are allowd to be exported, please remove the function ${originHandlerName} from exports`
|
|
164
|
+
);
|
|
228
165
|
return null;
|
|
229
166
|
}
|
|
230
167
|
}
|
|
231
|
-
|
|
232
168
|
loadApiFiles() {
|
|
233
169
|
if (!this.existLambdaDir) {
|
|
234
170
|
return [];
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
const apiFiles = this.apiFiles = (0, _utils3.getFiles)(this.lambdaDir, _constants.API_FILE_RULES);
|
|
171
|
+
}
|
|
172
|
+
const apiFiles = this.apiFiles = (0, import_utils3.getFiles)(this.lambdaDir, import_constants.API_FILE_RULES);
|
|
239
173
|
return apiFiles;
|
|
240
174
|
}
|
|
241
|
-
|
|
242
175
|
getApiFiles() {
|
|
243
176
|
if (!this.existLambdaDir) {
|
|
244
177
|
return [];
|
|
245
178
|
}
|
|
246
|
-
|
|
247
179
|
if (this.apiFiles.length > 0) {
|
|
248
180
|
return this.apiFiles;
|
|
249
181
|
}
|
|
250
|
-
|
|
251
182
|
return this.loadApiFiles();
|
|
252
183
|
}
|
|
253
|
-
|
|
254
184
|
getApiHandlers() {
|
|
255
185
|
const filenames = this.getApiFiles();
|
|
256
186
|
const moduleInfos = this.getModuleInfos(filenames);
|
|
257
187
|
const apiHandlers = this.getHandlerInfos(moduleInfos);
|
|
258
|
-
(0,
|
|
188
|
+
(0, import_utils2.debug)("apiHandlers", apiHandlers.length, apiHandlers);
|
|
259
189
|
return apiHandlers;
|
|
260
190
|
}
|
|
261
|
-
/**
|
|
262
|
-
* 如果用户未传入或传入空串,默认为 /api
|
|
263
|
-
* 如果传入 /,则 prefix 为 /
|
|
264
|
-
*/
|
|
265
|
-
|
|
266
|
-
|
|
267
191
|
initPrefix(prefix) {
|
|
268
|
-
if (prefix ===
|
|
269
|
-
return
|
|
192
|
+
if (prefix === "/") {
|
|
193
|
+
return "";
|
|
270
194
|
}
|
|
271
|
-
|
|
272
|
-
return prefix || '/api';
|
|
195
|
+
return prefix || "/api";
|
|
273
196
|
}
|
|
274
|
-
|
|
275
197
|
validateAbsolute(filename, paramsName) {
|
|
276
|
-
if (typeof filename ===
|
|
198
|
+
if (typeof filename === "string" && !import_path.default.isAbsolute(filename)) {
|
|
277
199
|
throw new Error(`The ${paramsName} ${filename} is not a abolute path`);
|
|
278
200
|
}
|
|
279
201
|
}
|
|
280
|
-
|
|
281
202
|
getModuleInfos(filenames) {
|
|
282
|
-
return filenames.map(filename => this.getModuleInfo(filename)).filter(moduleInfo => Boolean(moduleInfo));
|
|
203
|
+
return filenames.map((filename) => this.getModuleInfo(filename)).filter((moduleInfo) => Boolean(moduleInfo));
|
|
283
204
|
}
|
|
284
|
-
|
|
285
205
|
getModuleInfo(filename) {
|
|
286
206
|
try {
|
|
287
|
-
const
|
|
207
|
+
const module2 = (0, import_utils3.requireHandlerModule)(filename);
|
|
288
208
|
return {
|
|
289
209
|
filename,
|
|
290
|
-
module
|
|
210
|
+
module: module2
|
|
291
211
|
};
|
|
292
212
|
} catch (err) {
|
|
293
|
-
if (process.env.NODE_ENV ===
|
|
213
|
+
if (process.env.NODE_ENV === "production") {
|
|
294
214
|
throw err;
|
|
295
215
|
} else {
|
|
296
216
|
console.error(err);
|
|
@@ -298,48 +218,39 @@ class ApiRouter {
|
|
|
298
218
|
}
|
|
299
219
|
}
|
|
300
220
|
}
|
|
301
|
-
|
|
302
221
|
getHandlerInfos(moduleInfos) {
|
|
303
222
|
let apiHandlers = [];
|
|
304
|
-
moduleInfos.forEach(moduleInfo => {
|
|
223
|
+
moduleInfos.forEach((moduleInfo) => {
|
|
305
224
|
const handlerInfos = this.getModuleHandlerInfos(moduleInfo);
|
|
306
|
-
|
|
307
225
|
if (handlerInfos) {
|
|
308
226
|
apiHandlers = apiHandlers.concat(handlerInfos);
|
|
309
227
|
}
|
|
310
228
|
});
|
|
311
|
-
const sortedHandlers = (0,
|
|
229
|
+
const sortedHandlers = (0, import_utils3.sortRoutes)(apiHandlers);
|
|
312
230
|
return sortedHandlers;
|
|
313
231
|
}
|
|
314
|
-
|
|
315
232
|
getModuleHandlerInfos(moduleInfo) {
|
|
316
|
-
const {
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
} = moduleInfo;
|
|
320
|
-
return Object.entries(module).filter(([, handler]) => typeof handler === 'function').map(([key]) => {
|
|
321
|
-
const handler = module[key];
|
|
233
|
+
const { module: module2, filename } = moduleInfo;
|
|
234
|
+
return Object.entries(module2).filter(([, handler]) => typeof handler === "function").map(([key]) => {
|
|
235
|
+
const handler = module2[key];
|
|
322
236
|
const handlerInfo = this.getHandlerInfo(filename, key, handler);
|
|
323
237
|
return handlerInfo;
|
|
324
|
-
}).filter(handlerInfo => Boolean(handlerInfo));
|
|
238
|
+
}).filter((handlerInfo) => Boolean(handlerInfo));
|
|
325
239
|
}
|
|
326
|
-
|
|
327
240
|
validateValidApifile(filename) {
|
|
328
241
|
if (!this.apiFiles.includes(filename)) {
|
|
329
242
|
throw new Error(`The ${filename} is not a valid api file.`);
|
|
330
243
|
}
|
|
331
244
|
}
|
|
332
|
-
|
|
333
245
|
getRoutePath(prefix, routeName) {
|
|
334
|
-
const finalRouteName = routeName ===
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
return '/';
|
|
246
|
+
const finalRouteName = routeName === "/" ? "" : routeName;
|
|
247
|
+
if (prefix === "" && finalRouteName === "") {
|
|
248
|
+
return "/";
|
|
338
249
|
}
|
|
339
|
-
|
|
340
250
|
return `${prefix}${finalRouteName}`;
|
|
341
251
|
}
|
|
342
|
-
|
|
343
252
|
}
|
|
344
|
-
|
|
345
|
-
exports
|
|
253
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
254
|
+
0 && (module.exports = {
|
|
255
|
+
ApiRouter
|
|
256
|
+
});
|
|
@@ -1,5 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
+
var types_exports = {};
|
|
15
|
+
module.exports = __toCommonJS(types_exports);
|
|
@@ -1,123 +1,116 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
var utils_exports = {};
|
|
25
|
+
__export(utils_exports, {
|
|
26
|
+
getFiles: () => getFiles,
|
|
27
|
+
getPathFromFilename: () => getPathFromFilename,
|
|
28
|
+
isHandler: () => isHandler,
|
|
29
|
+
requireHandlerModule: () => requireHandlerModule,
|
|
30
|
+
sortRoutes: () => sortRoutes
|
|
5
31
|
});
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
var _constants = require("./constants");
|
|
13
|
-
|
|
14
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
-
|
|
16
|
-
const getFiles = (lambdaDir, rules) => _utils.globby.sync(rules, {
|
|
32
|
+
module.exports = __toCommonJS(utils_exports);
|
|
33
|
+
var import_path = __toESM(require("path"));
|
|
34
|
+
var import_utils = require("@modern-js/utils");
|
|
35
|
+
var import_constants = require("./constants");
|
|
36
|
+
const getFiles = (lambdaDir, rules) => import_utils.globby.sync(rules, {
|
|
17
37
|
cwd: lambdaDir,
|
|
18
38
|
gitignore: true
|
|
19
|
-
}).map(file =>
|
|
20
|
-
|
|
21
|
-
exports.getFiles = getFiles;
|
|
22
|
-
|
|
39
|
+
}).map((file) => import_path.default.resolve(lambdaDir, file));
|
|
23
40
|
const getPathFromFilename = (baseDir, filename) => {
|
|
24
41
|
const relativeName = filename.substring(baseDir.length);
|
|
25
|
-
const relativePath = relativeName.split(
|
|
26
|
-
const nameSplit = relativePath.split(
|
|
42
|
+
const relativePath = relativeName.split(".").slice(0, -1).join(".");
|
|
43
|
+
const nameSplit = relativePath.split(import_path.default.sep).map((item) => {
|
|
27
44
|
if (item.length > 2) {
|
|
28
|
-
if (item.startsWith(
|
|
45
|
+
if (item.startsWith("[") && item.endsWith("]")) {
|
|
29
46
|
return `:${item.substring(1, item.length - 1)}`;
|
|
30
47
|
}
|
|
31
48
|
}
|
|
32
|
-
|
|
33
49
|
return item;
|
|
34
50
|
});
|
|
35
|
-
const name = nameSplit.join(
|
|
36
|
-
const finalName = name.endsWith(
|
|
51
|
+
const name = nameSplit.join("/");
|
|
52
|
+
const finalName = name.endsWith(import_constants.INDEX_SUFFIX) ? name.substring(0, name.length - import_constants.INDEX_SUFFIX.length) : name;
|
|
37
53
|
return clearRouteName(finalName);
|
|
38
54
|
};
|
|
39
|
-
|
|
40
|
-
exports.getPathFromFilename = getPathFromFilename;
|
|
41
|
-
|
|
42
|
-
const clearRouteName = routeName => {
|
|
55
|
+
const clearRouteName = (routeName) => {
|
|
43
56
|
let finalRouteName = routeName.trim();
|
|
44
|
-
|
|
45
|
-
if (!finalRouteName.startsWith('/')) {
|
|
57
|
+
if (!finalRouteName.startsWith("/")) {
|
|
46
58
|
finalRouteName = `/${finalRouteName}`;
|
|
47
59
|
}
|
|
48
|
-
|
|
49
|
-
if (finalRouteName.length > 1 && finalRouteName.endsWith('/')) {
|
|
60
|
+
if (finalRouteName.length > 1 && finalRouteName.endsWith("/")) {
|
|
50
61
|
finalRouteName = finalRouteName.substring(0, finalRouteName.length - 1);
|
|
51
62
|
}
|
|
52
|
-
|
|
53
63
|
return finalRouteName;
|
|
54
64
|
};
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
exports.isHandler = isHandler;
|
|
59
|
-
|
|
60
|
-
const enableRegister = requireFn => {
|
|
61
|
-
// esbuild-register 做 unRegister 时,不会删除 register 添加的 require.extensions,导致第二次调用时 require.extensions['.ts'] 是 nodejs 默认 loader
|
|
62
|
-
// 所以这里根据第一次调用时,require.extensions 有没有,来判断是否需要使用 esbuild-register
|
|
65
|
+
const isHandler = (input) => input && typeof input === "function";
|
|
66
|
+
const enableRegister = (requireFn) => {
|
|
63
67
|
let existTsLoader = false;
|
|
64
68
|
let firstCall = true;
|
|
65
|
-
return modulePath => {
|
|
69
|
+
return (modulePath) => {
|
|
66
70
|
if (firstCall) {
|
|
67
|
-
|
|
68
|
-
existTsLoader = Boolean(require.extensions['.ts']);
|
|
71
|
+
existTsLoader = Boolean(require.extensions[".ts"]);
|
|
69
72
|
firstCall = false;
|
|
70
73
|
}
|
|
71
|
-
|
|
72
74
|
if (!existTsLoader) {
|
|
73
75
|
const {
|
|
74
76
|
register
|
|
75
|
-
} = require(
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
unregister
|
|
79
|
-
} = register({
|
|
80
|
-
extensions: ['.ts']
|
|
77
|
+
} = require("esbuild-register/dist/node");
|
|
78
|
+
const { unregister } = register({
|
|
79
|
+
extensions: [".ts"]
|
|
81
80
|
});
|
|
82
|
-
const
|
|
81
|
+
const requiredModule2 = requireFn(modulePath);
|
|
83
82
|
unregister();
|
|
84
|
-
return
|
|
83
|
+
return requiredModule2;
|
|
85
84
|
}
|
|
86
|
-
|
|
87
85
|
const requiredModule = requireFn(modulePath);
|
|
88
86
|
return requiredModule;
|
|
89
87
|
};
|
|
90
88
|
};
|
|
91
|
-
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
const module = originRequire(modulePath);
|
|
98
|
-
|
|
99
|
-
if (isFunction(module)) {
|
|
100
|
-
return {
|
|
101
|
-
default: module
|
|
102
|
-
};
|
|
89
|
+
const isFunction = (input) => input && {}.toString.call(input) === "[object Function]";
|
|
90
|
+
const requireHandlerModule = enableRegister((modulePath) => {
|
|
91
|
+
const originRequire = process.env.NODE_ENV === "test" ? jest.requireActual : require;
|
|
92
|
+
const module2 = originRequire(modulePath);
|
|
93
|
+
if (isFunction(module2)) {
|
|
94
|
+
return { default: module2 };
|
|
103
95
|
}
|
|
104
|
-
|
|
105
|
-
return module;
|
|
96
|
+
return module2;
|
|
106
97
|
});
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const routeValue = routePath => {
|
|
110
|
-
if (routePath.includes(':')) {
|
|
98
|
+
const routeValue = (routePath) => {
|
|
99
|
+
if (routePath.includes(":")) {
|
|
111
100
|
return 11;
|
|
112
101
|
}
|
|
113
|
-
|
|
114
102
|
return 1;
|
|
115
103
|
};
|
|
116
|
-
|
|
117
|
-
const sortRoutes = apiHandlers => {
|
|
104
|
+
const sortRoutes = (apiHandlers) => {
|
|
118
105
|
return apiHandlers.sort((handlerA, handlerB) => {
|
|
119
106
|
return routeValue(handlerA.routeName) - routeValue(handlerB.routeName);
|
|
120
107
|
});
|
|
121
108
|
};
|
|
122
|
-
|
|
123
|
-
exports
|
|
109
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
110
|
+
0 && (module.exports = {
|
|
111
|
+
getFiles,
|
|
112
|
+
getPathFromFilename,
|
|
113
|
+
isHandler,
|
|
114
|
+
requireHandlerModule,
|
|
115
|
+
sortRoutes
|
|
116
|
+
});
|