@modern-js/bff-core 2.0.0-beta.2 → 2.0.0-beta.4
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 +72 -0
- package/dist/js/modern/api.js +49 -25
- package/dist/js/modern/client/generate-client.js +51 -24
- package/dist/js/modern/client/index.js +1 -1
- package/dist/js/modern/client/result.js +9 -8
- package/dist/js/modern/errors/http.js +8 -7
- package/dist/js/modern/index.js +19 -3
- package/dist/js/modern/operators/http.js +139 -87
- package/dist/js/modern/router/constants.js +31 -11
- package/dist/js/modern/router/index.js +66 -69
- package/dist/js/modern/router/types.js +0 -1
- package/dist/js/modern/router/utils.js +36 -37
- package/dist/js/modern/types.js +45 -37
- package/dist/js/modern/utils/alias.js +32 -30
- 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 -4
- package/dist/js/modern/utils/validate.js +22 -23
- package/dist/js/node/api.js +74 -32
- package/dist/js/node/client/generate-client.js +79 -37
- package/dist/js/node/client/index.js +17 -16
- package/dist/js/node/client/result.js +26 -14
- package/dist/js/node/errors/http.js +23 -11
- package/dist/js/node/index.js +35 -112
- package/dist/js/node/operators/http.js +166 -124
- package/dist/js/node/router/constants.js +50 -24
- package/dist/js/node/router/index.js +108 -127
- package/dist/js/node/router/types.js +0 -5
- package/dist/js/node/router/utils.js +63 -51
- package/dist/js/node/types.js +62 -47
- package/dist/js/node/utils/alias.js +57 -42
- package/dist/js/node/utils/debug.js +23 -8
- package/dist/js/node/utils/index.js +25 -60
- package/dist/js/node/utils/meta.js +25 -10
- package/dist/js/node/utils/storage.js +32 -11
- package/dist/js/node/utils/validate.js +43 -28
- package/dist/types/router/constants.d.ts +1 -0
- package/dist/types/router/index.d.ts +1 -0
- package/package.json +5 -10
|
@@ -1,83 +1,73 @@
|
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
});
|
|
27
|
-
var _utils3 = require("./utils");
|
|
28
|
-
var _types2 = require("./types");
|
|
29
|
-
Object.keys(_types2).forEach(function (key) {
|
|
30
|
-
if (key === "default" || key === "__esModule") return;
|
|
31
|
-
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
32
|
-
if (key in exports && exports[key] === _types2[key]) return;
|
|
33
|
-
Object.defineProperty(exports, key, {
|
|
34
|
-
enumerable: true,
|
|
35
|
-
get: function () {
|
|
36
|
-
return _types2[key];
|
|
37
|
-
}
|
|
38
|
-
});
|
|
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 stdin_exports = {};
|
|
26
|
+
__export(stdin_exports, {
|
|
27
|
+
ApiRouter: () => ApiRouter
|
|
39
28
|
});
|
|
40
|
-
|
|
41
|
-
|
|
29
|
+
module.exports = __toCommonJS(stdin_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(stdin_exports, require("./types"), module.exports);
|
|
38
|
+
__reExport(stdin_exports, require("./constants"), module.exports);
|
|
42
39
|
class ApiRouter {
|
|
43
|
-
// lambdaDir is the dir which equal to the apiDir in function mode, and equal to the api/lambda dir in framework mode
|
|
44
|
-
|
|
45
40
|
constructor({
|
|
46
|
-
apiDir
|
|
47
|
-
lambdaDir
|
|
41
|
+
apiDir,
|
|
42
|
+
lambdaDir,
|
|
48
43
|
prefix
|
|
49
44
|
}) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
_defineProperty(this, "getExactApiMode", apiDir => {
|
|
57
|
-
const exist = this.createExistChecker(apiDir);
|
|
58
|
-
const existLambdaDir = exist(_constants.FRAMEWORK_MODE_LAMBDA_DIR);
|
|
59
|
-
const existAppDir = exist(_constants.FRAMEWORK_MODE_APP_DIR);
|
|
60
|
-
const existAppFile = exist('app.ts') || exist('app.js');
|
|
45
|
+
this.apiFiles = [];
|
|
46
|
+
this.getExactApiMode = (apiDir2) => {
|
|
47
|
+
const exist = this.createExistChecker(apiDir2);
|
|
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");
|
|
61
51
|
if (existLambdaDir || existAppDir || existAppFile) {
|
|
62
|
-
return
|
|
52
|
+
return import_constants.APIMode.FARMEWORK;
|
|
63
53
|
}
|
|
64
|
-
return
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
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 = (apiDir2) => {
|
|
68
58
|
if (this.lambdaDir) {
|
|
69
59
|
return this.lambdaDir;
|
|
70
60
|
}
|
|
71
|
-
const
|
|
72
|
-
return
|
|
73
|
-
}
|
|
74
|
-
this.validateAbsolute(
|
|
75
|
-
this.validateAbsolute(
|
|
61
|
+
const lambdaDir2 = this.apiMode === import_constants.APIMode.FARMEWORK ? import_path.default.join(apiDir2, import_constants.FRAMEWORK_MODE_LAMBDA_DIR) : apiDir2;
|
|
62
|
+
return lambdaDir2;
|
|
63
|
+
};
|
|
64
|
+
this.validateAbsolute(apiDir, "apiDir");
|
|
65
|
+
this.validateAbsolute(lambdaDir, "lambdaDir");
|
|
76
66
|
this.prefix = this.initPrefix(prefix);
|
|
77
|
-
this.apiDir =
|
|
78
|
-
this.apiMode = this.getExactApiMode(
|
|
79
|
-
this.lambdaDir =
|
|
80
|
-
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);
|
|
81
71
|
}
|
|
82
72
|
isExistLambda() {
|
|
83
73
|
return this.existLambdaDir;
|
|
@@ -119,8 +109,6 @@ class ApiRouter {
|
|
|
119
109
|
}
|
|
120
110
|
return null;
|
|
121
111
|
}
|
|
122
|
-
|
|
123
|
-
// TODO: 性能提升,开发环境,判断下 lambda 目录修改时间
|
|
124
112
|
getSafeRoutePath(filename, handler) {
|
|
125
113
|
this.loadApiFiles();
|
|
126
114
|
this.validateValidApifile(filename);
|
|
@@ -128,49 +116,52 @@ class ApiRouter {
|
|
|
128
116
|
}
|
|
129
117
|
getRouteName(filename, handler) {
|
|
130
118
|
if (handler) {
|
|
131
|
-
const trigger = Reflect.getMetadata(
|
|
132
|
-
if (trigger && trigger.type ===
|
|
119
|
+
const trigger = Reflect.getMetadata(import_types.OperatorType.Trigger, handler);
|
|
120
|
+
if (trigger && trigger.type === import_types.TriggerType.Http) {
|
|
133
121
|
if (!trigger.path) {
|
|
134
|
-
throw new Error(
|
|
122
|
+
throw new Error(
|
|
123
|
+
`The http trigger ${trigger.name} needs to specify a path`
|
|
124
|
+
);
|
|
135
125
|
}
|
|
136
126
|
return trigger.path;
|
|
137
127
|
}
|
|
138
128
|
}
|
|
139
|
-
const routePath = (0,
|
|
129
|
+
const routePath = (0, import_utils3.getPathFromFilename)(this.lambdaDir, filename);
|
|
140
130
|
return routePath;
|
|
141
131
|
}
|
|
142
132
|
getHttpMethod(originHandlerName, handler) {
|
|
143
133
|
if (handler) {
|
|
144
|
-
const trigger = Reflect.getMetadata(
|
|
145
|
-
if (trigger &&
|
|
134
|
+
const trigger = Reflect.getMetadata(import_types.OperatorType.Trigger, handler);
|
|
135
|
+
if (trigger && import_types.httpMethods.includes(trigger.method)) {
|
|
146
136
|
return trigger.method;
|
|
147
137
|
}
|
|
148
138
|
}
|
|
149
139
|
const upperName = originHandlerName.toUpperCase();
|
|
150
140
|
switch (upperName) {
|
|
151
|
-
case
|
|
152
|
-
return
|
|
153
|
-
case
|
|
154
|
-
return
|
|
155
|
-
case
|
|
156
|
-
return
|
|
157
|
-
case
|
|
158
|
-
case
|
|
159
|
-
return
|
|
160
|
-
case
|
|
161
|
-
return
|
|
162
|
-
case
|
|
163
|
-
return
|
|
164
|
-
case
|
|
165
|
-
return
|
|
166
|
-
case
|
|
167
|
-
return
|
|
168
|
-
case
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
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
|
+
}
|
|
172
161
|
default:
|
|
173
|
-
|
|
162
|
+
import_utils.logger.warn(
|
|
163
|
+
`Only api handlers are allowd to be exported, please remove the function ${originHandlerName} from exports`
|
|
164
|
+
);
|
|
174
165
|
return null;
|
|
175
166
|
}
|
|
176
167
|
}
|
|
@@ -178,8 +169,7 @@ class ApiRouter {
|
|
|
178
169
|
if (!this.existLambdaDir) {
|
|
179
170
|
return [];
|
|
180
171
|
}
|
|
181
|
-
|
|
182
|
-
const apiFiles = this.apiFiles = (0, _utils3.getFiles)(this.lambdaDir, _constants.API_FILE_RULES);
|
|
172
|
+
const apiFiles = this.apiFiles = (0, import_utils3.getFiles)(this.lambdaDir, import_constants.API_FILE_RULES);
|
|
183
173
|
return apiFiles;
|
|
184
174
|
}
|
|
185
175
|
getApiFiles() {
|
|
@@ -195,37 +185,32 @@ class ApiRouter {
|
|
|
195
185
|
const filenames = this.getApiFiles();
|
|
196
186
|
const moduleInfos = this.getModuleInfos(filenames);
|
|
197
187
|
const apiHandlers = this.getHandlerInfos(moduleInfos);
|
|
198
|
-
(0,
|
|
188
|
+
(0, import_utils2.debug)("apiHandlers", apiHandlers.length, apiHandlers);
|
|
199
189
|
return apiHandlers;
|
|
200
190
|
}
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* 如果用户未传入或传入空串,默认为 /api
|
|
204
|
-
* 如果传入 /,则 prefix 为 /
|
|
205
|
-
*/
|
|
206
191
|
initPrefix(prefix) {
|
|
207
|
-
if (prefix ===
|
|
208
|
-
return
|
|
192
|
+
if (prefix === "/") {
|
|
193
|
+
return "";
|
|
209
194
|
}
|
|
210
|
-
return prefix ||
|
|
195
|
+
return prefix || "/api";
|
|
211
196
|
}
|
|
212
197
|
validateAbsolute(filename, paramsName) {
|
|
213
|
-
if (typeof filename ===
|
|
198
|
+
if (typeof filename === "string" && !import_path.default.isAbsolute(filename)) {
|
|
214
199
|
throw new Error(`The ${paramsName} ${filename} is not a abolute path`);
|
|
215
200
|
}
|
|
216
201
|
}
|
|
217
202
|
getModuleInfos(filenames) {
|
|
218
|
-
return filenames.map(filename => this.getModuleInfo(filename)).filter(moduleInfo => Boolean(moduleInfo));
|
|
203
|
+
return filenames.map((filename) => this.getModuleInfo(filename)).filter((moduleInfo) => Boolean(moduleInfo));
|
|
219
204
|
}
|
|
220
205
|
getModuleInfo(filename) {
|
|
221
206
|
try {
|
|
222
|
-
const
|
|
207
|
+
const module2 = (0, import_utils3.requireHandlerModule)(filename);
|
|
223
208
|
return {
|
|
224
209
|
filename,
|
|
225
|
-
module
|
|
210
|
+
module: module2
|
|
226
211
|
};
|
|
227
212
|
} catch (err) {
|
|
228
|
-
if (process.env.NODE_ENV ===
|
|
213
|
+
if (process.env.NODE_ENV === "production") {
|
|
229
214
|
throw err;
|
|
230
215
|
} else {
|
|
231
216
|
console.error(err);
|
|
@@ -235,25 +220,22 @@ class ApiRouter {
|
|
|
235
220
|
}
|
|
236
221
|
getHandlerInfos(moduleInfos) {
|
|
237
222
|
let apiHandlers = [];
|
|
238
|
-
moduleInfos.forEach(moduleInfo => {
|
|
223
|
+
moduleInfos.forEach((moduleInfo) => {
|
|
239
224
|
const handlerInfos = this.getModuleHandlerInfos(moduleInfo);
|
|
240
225
|
if (handlerInfos) {
|
|
241
226
|
apiHandlers = apiHandlers.concat(handlerInfos);
|
|
242
227
|
}
|
|
243
228
|
});
|
|
244
|
-
const sortedHandlers = (0,
|
|
229
|
+
const sortedHandlers = (0, import_utils3.sortRoutes)(apiHandlers);
|
|
245
230
|
return sortedHandlers;
|
|
246
231
|
}
|
|
247
232
|
getModuleHandlerInfos(moduleInfo) {
|
|
248
|
-
const {
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
} = moduleInfo;
|
|
252
|
-
return Object.entries(module).filter(([, handler]) => typeof handler === 'function').map(([key]) => {
|
|
253
|
-
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];
|
|
254
236
|
const handlerInfo = this.getHandlerInfo(filename, key, handler);
|
|
255
237
|
return handlerInfo;
|
|
256
|
-
}).filter(handlerInfo => Boolean(handlerInfo));
|
|
238
|
+
}).filter((handlerInfo) => Boolean(handlerInfo));
|
|
257
239
|
}
|
|
258
240
|
validateValidApifile(filename) {
|
|
259
241
|
if (!this.apiFiles.includes(filename)) {
|
|
@@ -261,11 +243,10 @@ class ApiRouter {
|
|
|
261
243
|
}
|
|
262
244
|
}
|
|
263
245
|
getRoutePath(prefix, routeName) {
|
|
264
|
-
const finalRouteName = routeName ===
|
|
265
|
-
if (prefix ===
|
|
266
|
-
return
|
|
246
|
+
const finalRouteName = routeName === "/" ? "" : routeName;
|
|
247
|
+
if (prefix === "" && finalRouteName === "") {
|
|
248
|
+
return "/";
|
|
267
249
|
}
|
|
268
250
|
return `${prefix}${finalRouteName}`;
|
|
269
251
|
}
|
|
270
252
|
}
|
|
271
|
-
exports.ApiRouter = ApiRouter;
|
|
@@ -1,96 +1,108 @@
|
|
|
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 stdin_exports = {};
|
|
25
|
+
__export(stdin_exports, {
|
|
26
|
+
getFiles: () => getFiles,
|
|
27
|
+
getPathFromFilename: () => getPathFromFilename,
|
|
28
|
+
isHandler: () => isHandler,
|
|
29
|
+
requireHandlerModule: () => requireHandlerModule,
|
|
30
|
+
sortRoutes: () => sortRoutes
|
|
5
31
|
});
|
|
6
|
-
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
const getFiles = (lambdaDir, rules) => _utils.globby.sync(rules, {
|
|
32
|
+
module.exports = __toCommonJS(stdin_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, {
|
|
12
37
|
cwd: lambdaDir,
|
|
13
38
|
gitignore: true
|
|
14
|
-
}).map(file =>
|
|
15
|
-
exports.getFiles = getFiles;
|
|
39
|
+
}).map((file) => import_path.default.resolve(lambdaDir, file));
|
|
16
40
|
const getPathFromFilename = (baseDir, filename) => {
|
|
17
41
|
const relativeName = filename.substring(baseDir.length);
|
|
18
|
-
const relativePath = relativeName.split(
|
|
19
|
-
const nameSplit = relativePath.split(
|
|
42
|
+
const relativePath = relativeName.split(".").slice(0, -1).join(".");
|
|
43
|
+
const nameSplit = relativePath.split(import_path.default.sep).map((item) => {
|
|
20
44
|
if (item.length > 2) {
|
|
21
|
-
if (item.startsWith(
|
|
45
|
+
if (item.startsWith("[") && item.endsWith("]")) {
|
|
22
46
|
return `:${item.substring(1, item.length - 1)}`;
|
|
23
47
|
}
|
|
24
48
|
}
|
|
25
49
|
return item;
|
|
26
50
|
});
|
|
27
|
-
const name = nameSplit.join(
|
|
28
|
-
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;
|
|
29
53
|
return clearRouteName(finalName);
|
|
30
54
|
};
|
|
31
|
-
|
|
32
|
-
const clearRouteName = routeName => {
|
|
55
|
+
const clearRouteName = (routeName) => {
|
|
33
56
|
let finalRouteName = routeName.trim();
|
|
34
|
-
if (!finalRouteName.startsWith(
|
|
57
|
+
if (!finalRouteName.startsWith("/")) {
|
|
35
58
|
finalRouteName = `/${finalRouteName}`;
|
|
36
59
|
}
|
|
37
|
-
if (finalRouteName.length > 1 && finalRouteName.endsWith(
|
|
60
|
+
if (finalRouteName.length > 1 && finalRouteName.endsWith("/")) {
|
|
38
61
|
finalRouteName = finalRouteName.substring(0, finalRouteName.length - 1);
|
|
39
62
|
}
|
|
40
63
|
return finalRouteName;
|
|
41
64
|
};
|
|
42
|
-
const isHandler = input => input && typeof input ===
|
|
43
|
-
|
|
44
|
-
const enableRegister = requireFn => {
|
|
45
|
-
// esbuild-register 做 unRegister 时,不会删除 register 添加的 require.extensions,导致第二次调用时 require.extensions['.ts'] 是 nodejs 默认 loader
|
|
46
|
-
// 所以这里根据第一次调用时,require.extensions 有没有,来判断是否需要使用 esbuild-register
|
|
65
|
+
const isHandler = (input) => input && typeof input === "function";
|
|
66
|
+
const enableRegister = (requireFn) => {
|
|
47
67
|
let existTsLoader = false;
|
|
48
68
|
let firstCall = true;
|
|
49
|
-
return modulePath => {
|
|
69
|
+
return (modulePath) => {
|
|
50
70
|
if (firstCall) {
|
|
51
|
-
|
|
52
|
-
existTsLoader = Boolean(require.extensions['.ts']);
|
|
71
|
+
existTsLoader = Boolean(require.extensions[".ts"]);
|
|
53
72
|
firstCall = false;
|
|
54
73
|
}
|
|
55
74
|
if (!existTsLoader) {
|
|
56
75
|
const {
|
|
57
76
|
register
|
|
58
|
-
} = require(
|
|
59
|
-
const {
|
|
60
|
-
|
|
61
|
-
} = register({
|
|
62
|
-
extensions: ['.ts']
|
|
77
|
+
} = require("esbuild-register/dist/node");
|
|
78
|
+
const { unregister } = register({
|
|
79
|
+
extensions: [".ts"]
|
|
63
80
|
});
|
|
64
|
-
const
|
|
81
|
+
const requiredModule2 = requireFn(modulePath);
|
|
65
82
|
unregister();
|
|
66
|
-
return
|
|
83
|
+
return requiredModule2;
|
|
67
84
|
}
|
|
68
85
|
const requiredModule = requireFn(modulePath);
|
|
69
86
|
return requiredModule;
|
|
70
87
|
};
|
|
71
88
|
};
|
|
72
|
-
const isFunction = input => input && {}.toString.call(input) ===
|
|
73
|
-
const requireHandlerModule = enableRegister(modulePath => {
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return {
|
|
79
|
-
default: module
|
|
80
|
-
};
|
|
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 };
|
|
81
95
|
}
|
|
82
|
-
return
|
|
96
|
+
return module2;
|
|
83
97
|
});
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (routePath.includes(':')) {
|
|
98
|
+
const routeValue = (routePath) => {
|
|
99
|
+
if (routePath.includes(":")) {
|
|
87
100
|
return 11;
|
|
88
101
|
}
|
|
89
102
|
return 1;
|
|
90
103
|
};
|
|
91
|
-
const sortRoutes = apiHandlers => {
|
|
104
|
+
const sortRoutes = (apiHandlers) => {
|
|
92
105
|
return apiHandlers.sort((handlerA, handlerB) => {
|
|
93
106
|
return routeValue(handlerA.routeName) - routeValue(handlerB.routeName);
|
|
94
107
|
});
|
|
95
108
|
};
|
|
96
|
-
exports.sortRoutes = sortRoutes;
|
package/dist/js/node/types.js
CHANGED
|
@@ -1,49 +1,64 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var stdin_exports = {};
|
|
19
|
+
__export(stdin_exports, {
|
|
20
|
+
HttpMetadata: () => HttpMetadata,
|
|
21
|
+
HttpMethod: () => HttpMethod,
|
|
22
|
+
OperatorType: () => OperatorType,
|
|
23
|
+
ResponseMetaType: () => ResponseMetaType,
|
|
24
|
+
TriggerType: () => TriggerType,
|
|
25
|
+
httpMethods: () => httpMethods
|
|
5
26
|
});
|
|
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
|
-
HttpMethod["Trace"] = "TRACE";
|
|
44
|
-
HttpMethod["Patch"] = "PATCH";
|
|
45
|
-
HttpMethod["Option"] = "OPTION";
|
|
46
|
-
HttpMethod["Head"] = "HEAD";
|
|
47
|
-
})(HttpMethod || (exports.HttpMethod = HttpMethod = {}));
|
|
27
|
+
module.exports = __toCommonJS(stdin_exports);
|
|
28
|
+
var OperatorType = /* @__PURE__ */ ((OperatorType2) => {
|
|
29
|
+
OperatorType2[OperatorType2["Trigger"] = 0] = "Trigger";
|
|
30
|
+
OperatorType2[OperatorType2["Middleware"] = 1] = "Middleware";
|
|
31
|
+
return OperatorType2;
|
|
32
|
+
})(OperatorType || {});
|
|
33
|
+
var TriggerType = /* @__PURE__ */ ((TriggerType2) => {
|
|
34
|
+
TriggerType2[TriggerType2["Http"] = 0] = "Http";
|
|
35
|
+
return TriggerType2;
|
|
36
|
+
})(TriggerType || {});
|
|
37
|
+
var HttpMetadata = /* @__PURE__ */ ((HttpMetadata2) => {
|
|
38
|
+
HttpMetadata2["Method"] = "METHOD";
|
|
39
|
+
HttpMetadata2["Data"] = "DATA";
|
|
40
|
+
HttpMetadata2["Query"] = "QUERY";
|
|
41
|
+
HttpMetadata2["Params"] = "PARAMS";
|
|
42
|
+
HttpMetadata2["Headers"] = "HEADERS";
|
|
43
|
+
HttpMetadata2["Response"] = "RESPONSE";
|
|
44
|
+
return HttpMetadata2;
|
|
45
|
+
})(HttpMetadata || {});
|
|
46
|
+
var ResponseMetaType = /* @__PURE__ */ ((ResponseMetaType2) => {
|
|
47
|
+
ResponseMetaType2[ResponseMetaType2["StatusCode"] = 0] = "StatusCode";
|
|
48
|
+
ResponseMetaType2[ResponseMetaType2["Redirect"] = 1] = "Redirect";
|
|
49
|
+
ResponseMetaType2[ResponseMetaType2["Headers"] = 2] = "Headers";
|
|
50
|
+
return ResponseMetaType2;
|
|
51
|
+
})(ResponseMetaType || {});
|
|
52
|
+
var HttpMethod = /* @__PURE__ */ ((HttpMethod2) => {
|
|
53
|
+
HttpMethod2["Get"] = "GET";
|
|
54
|
+
HttpMethod2["Post"] = "POST";
|
|
55
|
+
HttpMethod2["Put"] = "PUT";
|
|
56
|
+
HttpMethod2["Delete"] = "DELETE";
|
|
57
|
+
HttpMethod2["Connect"] = "CONNECT";
|
|
58
|
+
HttpMethod2["Trace"] = "TRACE";
|
|
59
|
+
HttpMethod2["Patch"] = "PATCH";
|
|
60
|
+
HttpMethod2["Option"] = "OPTION";
|
|
61
|
+
HttpMethod2["Head"] = "HEAD";
|
|
62
|
+
return HttpMethod2;
|
|
63
|
+
})(HttpMethod || {});
|
|
48
64
|
const httpMethods = Object.values(HttpMethod);
|
|
49
|
-
exports.httpMethods = httpMethods;
|