@modern-js/bff-core 1.2.2 → 1.17.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 +34 -0
- package/dist/js/modern/index.js +2 -2
- package/dist/js/modern/router/index.js +57 -3
- package/dist/js/modern/router/utils.js +3 -1
- package/dist/js/modern/utils/alias.js +91 -0
- package/dist/js/modern/utils/debug.js +2 -0
- package/dist/js/modern/utils/index.js +5 -0
- package/dist/js/modern/utils/meta.js +4 -0
- package/dist/js/modern/utils/storage.js +48 -0
- package/dist/js/modern/utils/validate.js +49 -0
- package/dist/js/node/index.js +39 -16
- package/dist/js/node/router/index.js +57 -3
- package/dist/js/node/router/utils.js +3 -1
- package/dist/js/node/utils/alias.js +115 -0
- package/dist/js/node/utils/debug.js +11 -0
- package/dist/js/node/utils/index.js +72 -0
- package/dist/js/node/utils/meta.js +14 -0
- package/dist/js/node/utils/storage.js +59 -0
- package/dist/js/node/utils/validate.js +68 -0
- package/dist/types/index.d.ts +2 -2
- package/dist/types/router/index.d.ts +5 -1
- package/dist/types/utils/alias.d.ts +7 -0
- package/dist/types/utils/debug.d.ts +1 -0
- package/dist/types/utils/index.d.ts +5 -0
- package/dist/types/utils/meta.d.ts +2 -0
- package/dist/types/utils/storage.d.ts +5 -0
- package/dist/types/utils/validate.d.ts +5 -0
- package/package.json +5 -5
- package/dist/js/modern/utils.js +0 -136
- package/dist/js/node/utils.js +0 -170
- package/dist/types/utils.d.ts +0 -14
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# @modern-js/bff-core
|
|
2
|
+
|
|
3
|
+
## 1.17.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [1b9176f]
|
|
8
|
+
- Updated dependencies [77d3a38]
|
|
9
|
+
- Updated dependencies [151329d]
|
|
10
|
+
- Updated dependencies [5af9472]
|
|
11
|
+
- Updated dependencies [6b6a534]
|
|
12
|
+
- Updated dependencies [6b43a2b]
|
|
13
|
+
- Updated dependencies [a7be124]
|
|
14
|
+
- Updated dependencies [31547b4]
|
|
15
|
+
- @modern-js/utils@1.17.0
|
|
16
|
+
- @modern-js/bff-runtime@1.17.0
|
|
17
|
+
|
|
18
|
+
## 1.16.0
|
|
19
|
+
|
|
20
|
+
### Minor Changes
|
|
21
|
+
|
|
22
|
+
- 020b9bd52: feat: support frame mode without lambda directories
|
|
23
|
+
feat: 支持无 lambda 目录的框架模式
|
|
24
|
+
|
|
25
|
+
### Patch Changes
|
|
26
|
+
|
|
27
|
+
- Updated dependencies [641592f52]
|
|
28
|
+
- Updated dependencies [3904b30a5]
|
|
29
|
+
- Updated dependencies [1100dd58c]
|
|
30
|
+
- Updated dependencies [e04e6e76a]
|
|
31
|
+
- Updated dependencies [81c66e4a4]
|
|
32
|
+
- Updated dependencies [2c305b6f5]
|
|
33
|
+
- @modern-js/utils@1.16.0
|
|
34
|
+
- @modern-js/bff-runtime@1.16.0
|
package/dist/js/modern/index.js
CHANGED
|
@@ -2,6 +2,6 @@ export { Api } from "./api";
|
|
|
2
2
|
export { HttpError, ValidationError } from "./errors/http";
|
|
3
3
|
export * from "./router";
|
|
4
4
|
export * from "./types";
|
|
5
|
-
export * from "./utils";
|
|
6
5
|
export * from "./client";
|
|
7
|
-
export * from "./operators/http";
|
|
6
|
+
export * from "./operators/http";
|
|
7
|
+
export { getRelativeRuntimePath, HANDLER_WITH_META, isWithMetaHandler, createStorage, registerPaths } from "./utils";
|
|
@@ -19,6 +19,8 @@ export class ApiRouter {
|
|
|
19
19
|
|
|
20
20
|
_defineProperty(this, "lambdaDir", void 0);
|
|
21
21
|
|
|
22
|
+
_defineProperty(this, "existLambda", void 0);
|
|
23
|
+
|
|
22
24
|
_defineProperty(this, "prefix", void 0);
|
|
23
25
|
|
|
24
26
|
_defineProperty(this, "apiFiles", void 0);
|
|
@@ -35,7 +37,7 @@ export class ApiRouter {
|
|
|
35
37
|
|
|
36
38
|
_defineProperty(this, "createExistChecker", base => target => fs.pathExistsSync(path.resolve(base, target)));
|
|
37
39
|
|
|
38
|
-
_defineProperty(this, "
|
|
40
|
+
_defineProperty(this, "getExactLambdaDir", apiDir => {
|
|
39
41
|
if (this.lambdaDir) {
|
|
40
42
|
return this.lambdaDir;
|
|
41
43
|
}
|
|
@@ -57,10 +59,23 @@ export class ApiRouter {
|
|
|
57
59
|
|
|
58
60
|
this.prefix = this.initPrefix(prefix);
|
|
59
61
|
this.apiDir = _apiDir;
|
|
60
|
-
this.lambdaDir = _lambdaDir || this.
|
|
62
|
+
this.lambdaDir = _lambdaDir || this.getExactLambdaDir(this.apiDir);
|
|
63
|
+
this.existLambda = this.checkExistLambda(this.apiDir, this.lambdaDir);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
isExistLambda() {
|
|
67
|
+
return this.existLambda;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
getLambdaDir() {
|
|
71
|
+
return this.lambdaDir;
|
|
61
72
|
}
|
|
62
73
|
|
|
63
74
|
isApiFile(filename) {
|
|
75
|
+
if (this.existLambda) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
|
|
64
79
|
if (!this.apiFiles.includes(filename)) {
|
|
65
80
|
return false;
|
|
66
81
|
}
|
|
@@ -169,12 +184,20 @@ export class ApiRouter {
|
|
|
169
184
|
}
|
|
170
185
|
|
|
171
186
|
loadApiFiles() {
|
|
172
|
-
|
|
187
|
+
if (!this.existLambda) {
|
|
188
|
+
return [];
|
|
189
|
+
} // eslint-disable-next-line no-multi-assign
|
|
190
|
+
|
|
191
|
+
|
|
173
192
|
const apiFiles = this.apiFiles = getFiles(this.lambdaDir, API_FILE_RULES);
|
|
174
193
|
return apiFiles;
|
|
175
194
|
}
|
|
176
195
|
|
|
177
196
|
getApiFiles() {
|
|
197
|
+
if (!this.existLambda) {
|
|
198
|
+
return [];
|
|
199
|
+
}
|
|
200
|
+
|
|
178
201
|
if (this.apiFiles.length > 0) {
|
|
179
202
|
return this.apiFiles;
|
|
180
203
|
}
|
|
@@ -203,6 +226,37 @@ export class ApiRouter {
|
|
|
203
226
|
return prefix || '/api';
|
|
204
227
|
}
|
|
205
228
|
|
|
229
|
+
checkExistLambda(apiDir, lambdaDir) {
|
|
230
|
+
const isSame = apiDir === lambdaDir;
|
|
231
|
+
|
|
232
|
+
if (!isSame) {
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const exts = ['.ts', '.js'];
|
|
237
|
+
|
|
238
|
+
const existAppDir = apiDir => {
|
|
239
|
+
return fs.existsSync(path.join(apiDir, 'app'));
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const existAppFile = apiDir => {
|
|
243
|
+
const exists = exts.some(ext => {
|
|
244
|
+
return fs.existsSync(path.join(apiDir, `app${ext}`));
|
|
245
|
+
});
|
|
246
|
+
return exists;
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
if (isSame && existAppDir(apiDir)) {
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (isSame && existAppFile(apiDir)) {
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return true;
|
|
258
|
+
}
|
|
259
|
+
|
|
206
260
|
validateAbsolute(filename, paramsName) {
|
|
207
261
|
if (!path.isAbsolute(filename)) {
|
|
208
262
|
throw new Error(`The ${paramsName} ${filename} is not a abolute path`);
|
|
@@ -71,7 +71,9 @@ const enableRegister = requireFn => {
|
|
|
71
71
|
const isFunction = input => input && {}.toString.call(input) === '[object Function]';
|
|
72
72
|
|
|
73
73
|
export const requireHandlerModule = enableRegister(modulePath => {
|
|
74
|
-
|
|
74
|
+
// 测试环境不走缓存,因为缓存的 h andler 文件,会被 mockAPI 函数进行 mock,升级 jest28,setupFilesAfterEnv 能做异步操作的话,可解此问题
|
|
75
|
+
const originRequire = process.env.NODE_ENV === 'test' ? jest.requireActual : require;
|
|
76
|
+
const module = originRequire(modulePath);
|
|
75
77
|
|
|
76
78
|
if (isFunction(module)) {
|
|
77
79
|
return {
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import * as os from 'os';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import Module from 'module';
|
|
5
|
+
export const getRelativeRuntimePath = (appDirectory, serverRuntimePath) => {
|
|
6
|
+
let relativeRuntimePath = '';
|
|
7
|
+
|
|
8
|
+
if (os.platform() === 'win32') {
|
|
9
|
+
// isRelative function in babel-plugin-resolver plugin can't handle windows relative path correctly, see babel-plugin-resolver's utils.
|
|
10
|
+
relativeRuntimePath = `../${path.relative(appDirectory, serverRuntimePath)}`;
|
|
11
|
+
} else {
|
|
12
|
+
// Look up one level, because the artifacts after build have dist directories
|
|
13
|
+
relativeRuntimePath = path.join('../', path.relative(appDirectory, serverRuntimePath));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
|
|
17
|
+
relativeRuntimePath = `./${path.relative(appDirectory, serverRuntimePath)}`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return relativeRuntimePath;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const sortByLongestPrefix = arr => {
|
|
24
|
+
return arr.concat().sort((a, b) => b.length - a.length);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const createMatchPath = paths => {
|
|
28
|
+
const sortedKeys = sortByLongestPrefix(Object.keys(paths));
|
|
29
|
+
const sortedPaths = {};
|
|
30
|
+
sortedKeys.forEach(key => {
|
|
31
|
+
sortedPaths[key] = paths[key];
|
|
32
|
+
});
|
|
33
|
+
return request => {
|
|
34
|
+
const found = Object.keys(sortedPaths).find(key => {
|
|
35
|
+
return request.startsWith(key);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
if (found) {
|
|
39
|
+
let foundPaths = sortedPaths[found];
|
|
40
|
+
|
|
41
|
+
if (!Array.isArray(foundPaths)) {
|
|
42
|
+
foundPaths = [foundPaths];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
foundPaths = foundPaths.filter(foundPath => path.isAbsolute(foundPath));
|
|
46
|
+
|
|
47
|
+
for (const p of foundPaths) {
|
|
48
|
+
const foundPath = request.replace(found, p);
|
|
49
|
+
|
|
50
|
+
if (fs.existsSync(foundPath)) {
|
|
51
|
+
return foundPath;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return request.replace(found, foundPaths[0]);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return null;
|
|
59
|
+
};
|
|
60
|
+
}; // every path must be a absolute path;
|
|
61
|
+
|
|
62
|
+
export const registerPaths = paths => {
|
|
63
|
+
const originalResolveFilename = Module._resolveFilename; // eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
64
|
+
|
|
65
|
+
const {
|
|
66
|
+
builtinModules
|
|
67
|
+
} = Module;
|
|
68
|
+
const matchPath = createMatchPath(paths);
|
|
69
|
+
|
|
70
|
+
Module._resolveFilename = function (request, _parent) {
|
|
71
|
+
const isCoreModule = builtinModules.includes(request);
|
|
72
|
+
|
|
73
|
+
if (!isCoreModule) {
|
|
74
|
+
const matched = matchPath(request);
|
|
75
|
+
|
|
76
|
+
if (matched) {
|
|
77
|
+
// eslint-disable-next-line prefer-rest-params
|
|
78
|
+
const modifiedArguments = [matched, ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above.
|
|
79
|
+
|
|
80
|
+
return originalResolveFilename.apply(this, modifiedArguments);
|
|
81
|
+
}
|
|
82
|
+
} // eslint-disable-next-line prefer-rest-params
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
return originalResolveFilename.apply(this, arguments);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
return () => {
|
|
89
|
+
Module._resolveFilename = originalResolveFilename;
|
|
90
|
+
};
|
|
91
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import * as ah from 'async_hooks';
|
|
2
|
+
|
|
3
|
+
const createStorage = () => {
|
|
4
|
+
let storage;
|
|
5
|
+
|
|
6
|
+
if (typeof ah.AsyncLocalStorage !== 'undefined') {
|
|
7
|
+
storage = new ah.AsyncLocalStorage();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const run = (context, cb) => {
|
|
11
|
+
if (!storage) {
|
|
12
|
+
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
13
|
+
`);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
storage.run(context, () => {
|
|
18
|
+
try {
|
|
19
|
+
return resolve(cb());
|
|
20
|
+
} catch (error) {
|
|
21
|
+
return reject(error);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const useContext = () => {
|
|
28
|
+
if (!storage) {
|
|
29
|
+
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
30
|
+
`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const context = storage.getStore();
|
|
34
|
+
|
|
35
|
+
if (!context) {
|
|
36
|
+
throw new Error(`Can't call useContext out of scope, it should be placed in the bff function`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return context;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
run,
|
|
44
|
+
useContext
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export { createStorage };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import util from 'util'; // fork from https://github.com/nodejs/node/blob/master/lib/internal/errors.js
|
|
2
|
+
|
|
3
|
+
export const getTypeErrorMessage = actual => {
|
|
4
|
+
let msg = '';
|
|
5
|
+
|
|
6
|
+
if (actual == null) {
|
|
7
|
+
msg += `. Received ${actual}`;
|
|
8
|
+
} else if (typeof actual === 'function' && actual.name) {
|
|
9
|
+
msg += `. Received function ${actual.name}`;
|
|
10
|
+
} else if (typeof actual === 'object') {
|
|
11
|
+
var _actual$constructor;
|
|
12
|
+
|
|
13
|
+
if ((_actual$constructor = actual.constructor) !== null && _actual$constructor !== void 0 && _actual$constructor.name) {
|
|
14
|
+
msg += `. Received an instance of ${actual.constructor.name}`;
|
|
15
|
+
} else {
|
|
16
|
+
const inspected = util.inspect(actual, {
|
|
17
|
+
depth: -1
|
|
18
|
+
});
|
|
19
|
+
msg += `. Received ${inspected}`;
|
|
20
|
+
}
|
|
21
|
+
} else {
|
|
22
|
+
let inspected = util.inspect(actual, {
|
|
23
|
+
colors: false
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
if (inspected.length > 25) {
|
|
27
|
+
inspected = `${inspected.slice(0, 25)}...`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
msg += `. Received type ${typeof actual} (${inspected})`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return msg;
|
|
34
|
+
}; // eslint-disable-next-line @typescript-eslint/naming-convention
|
|
35
|
+
|
|
36
|
+
export class ERR_INVALID_ARG_TYPE extends Error {
|
|
37
|
+
constructor(funcName, expectedType, actual) {
|
|
38
|
+
const message = `[ERR_INVALID_ARG_TYPE]: The '${funcName}' argument must be of type ${expectedType}${getTypeErrorMessage(actual)}`;
|
|
39
|
+
super(message);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
}
|
|
43
|
+
export const validateFunction = (maybeFunc, name) => {
|
|
44
|
+
if (typeof maybeFunc !== 'function') {
|
|
45
|
+
throw new ERR_INVALID_ARG_TYPE(name, 'function', maybeFunc);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return true;
|
|
49
|
+
};
|
package/dist/js/node/index.js
CHANGED
|
@@ -6,7 +6,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
var _exportNames = {
|
|
7
7
|
Api: true,
|
|
8
8
|
HttpError: true,
|
|
9
|
-
ValidationError: true
|
|
9
|
+
ValidationError: true,
|
|
10
|
+
getRelativeRuntimePath: true,
|
|
11
|
+
HANDLER_WITH_META: true,
|
|
12
|
+
isWithMetaHandler: true,
|
|
13
|
+
createStorage: true,
|
|
14
|
+
registerPaths: true
|
|
10
15
|
};
|
|
11
16
|
Object.defineProperty(exports, "Api", {
|
|
12
17
|
enumerable: true,
|
|
@@ -14,6 +19,12 @@ Object.defineProperty(exports, "Api", {
|
|
|
14
19
|
return _api.Api;
|
|
15
20
|
}
|
|
16
21
|
});
|
|
22
|
+
Object.defineProperty(exports, "HANDLER_WITH_META", {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
get: function () {
|
|
25
|
+
return _utils.HANDLER_WITH_META;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
17
28
|
Object.defineProperty(exports, "HttpError", {
|
|
18
29
|
enumerable: true,
|
|
19
30
|
get: function () {
|
|
@@ -26,6 +37,30 @@ Object.defineProperty(exports, "ValidationError", {
|
|
|
26
37
|
return _http.ValidationError;
|
|
27
38
|
}
|
|
28
39
|
});
|
|
40
|
+
Object.defineProperty(exports, "createStorage", {
|
|
41
|
+
enumerable: true,
|
|
42
|
+
get: function () {
|
|
43
|
+
return _utils.createStorage;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
Object.defineProperty(exports, "getRelativeRuntimePath", {
|
|
47
|
+
enumerable: true,
|
|
48
|
+
get: function () {
|
|
49
|
+
return _utils.getRelativeRuntimePath;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
Object.defineProperty(exports, "isWithMetaHandler", {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
get: function () {
|
|
55
|
+
return _utils.isWithMetaHandler;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
Object.defineProperty(exports, "registerPaths", {
|
|
59
|
+
enumerable: true,
|
|
60
|
+
get: function () {
|
|
61
|
+
return _utils.registerPaths;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
29
64
|
|
|
30
65
|
var _api = require("./api");
|
|
31
66
|
|
|
@@ -59,20 +94,6 @@ Object.keys(_types).forEach(function (key) {
|
|
|
59
94
|
});
|
|
60
95
|
});
|
|
61
96
|
|
|
62
|
-
var _utils = require("./utils");
|
|
63
|
-
|
|
64
|
-
Object.keys(_utils).forEach(function (key) {
|
|
65
|
-
if (key === "default" || key === "__esModule") return;
|
|
66
|
-
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
67
|
-
if (key in exports && exports[key] === _utils[key]) return;
|
|
68
|
-
Object.defineProperty(exports, key, {
|
|
69
|
-
enumerable: true,
|
|
70
|
-
get: function () {
|
|
71
|
-
return _utils[key];
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
|
|
76
97
|
var _client = require("./client");
|
|
77
98
|
|
|
78
99
|
Object.keys(_client).forEach(function (key) {
|
|
@@ -99,4 +120,6 @@ Object.keys(_http2).forEach(function (key) {
|
|
|
99
120
|
return _http2[key];
|
|
100
121
|
}
|
|
101
122
|
});
|
|
102
|
-
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
var _utils = require("./utils");
|
|
@@ -62,6 +62,8 @@ class ApiRouter {
|
|
|
62
62
|
|
|
63
63
|
_defineProperty(this, "lambdaDir", void 0);
|
|
64
64
|
|
|
65
|
+
_defineProperty(this, "existLambda", void 0);
|
|
66
|
+
|
|
65
67
|
_defineProperty(this, "prefix", void 0);
|
|
66
68
|
|
|
67
69
|
_defineProperty(this, "apiFiles", void 0);
|
|
@@ -78,7 +80,7 @@ class ApiRouter {
|
|
|
78
80
|
|
|
79
81
|
_defineProperty(this, "createExistChecker", base => target => _utils.fs.pathExistsSync(_path.default.resolve(base, target)));
|
|
80
82
|
|
|
81
|
-
_defineProperty(this, "
|
|
83
|
+
_defineProperty(this, "getExactLambdaDir", apiDir => {
|
|
82
84
|
if (this.lambdaDir) {
|
|
83
85
|
return this.lambdaDir;
|
|
84
86
|
}
|
|
@@ -100,10 +102,23 @@ class ApiRouter {
|
|
|
100
102
|
|
|
101
103
|
this.prefix = this.initPrefix(prefix);
|
|
102
104
|
this.apiDir = _apiDir;
|
|
103
|
-
this.lambdaDir = _lambdaDir || this.
|
|
105
|
+
this.lambdaDir = _lambdaDir || this.getExactLambdaDir(this.apiDir);
|
|
106
|
+
this.existLambda = this.checkExistLambda(this.apiDir, this.lambdaDir);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
isExistLambda() {
|
|
110
|
+
return this.existLambda;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
getLambdaDir() {
|
|
114
|
+
return this.lambdaDir;
|
|
104
115
|
}
|
|
105
116
|
|
|
106
117
|
isApiFile(filename) {
|
|
118
|
+
if (this.existLambda) {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
|
|
107
122
|
if (!this.apiFiles.includes(filename)) {
|
|
108
123
|
return false;
|
|
109
124
|
}
|
|
@@ -213,12 +228,20 @@ class ApiRouter {
|
|
|
213
228
|
}
|
|
214
229
|
|
|
215
230
|
loadApiFiles() {
|
|
216
|
-
|
|
231
|
+
if (!this.existLambda) {
|
|
232
|
+
return [];
|
|
233
|
+
} // eslint-disable-next-line no-multi-assign
|
|
234
|
+
|
|
235
|
+
|
|
217
236
|
const apiFiles = this.apiFiles = (0, _utils3.getFiles)(this.lambdaDir, _constants.API_FILE_RULES);
|
|
218
237
|
return apiFiles;
|
|
219
238
|
}
|
|
220
239
|
|
|
221
240
|
getApiFiles() {
|
|
241
|
+
if (!this.existLambda) {
|
|
242
|
+
return [];
|
|
243
|
+
}
|
|
244
|
+
|
|
222
245
|
if (this.apiFiles.length > 0) {
|
|
223
246
|
return this.apiFiles;
|
|
224
247
|
}
|
|
@@ -247,6 +270,37 @@ class ApiRouter {
|
|
|
247
270
|
return prefix || '/api';
|
|
248
271
|
}
|
|
249
272
|
|
|
273
|
+
checkExistLambda(apiDir, lambdaDir) {
|
|
274
|
+
const isSame = apiDir === lambdaDir;
|
|
275
|
+
|
|
276
|
+
if (!isSame) {
|
|
277
|
+
return true;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const exts = ['.ts', '.js'];
|
|
281
|
+
|
|
282
|
+
const existAppDir = apiDir => {
|
|
283
|
+
return _utils.fs.existsSync(_path.default.join(apiDir, 'app'));
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
const existAppFile = apiDir => {
|
|
287
|
+
const exists = exts.some(ext => {
|
|
288
|
+
return _utils.fs.existsSync(_path.default.join(apiDir, `app${ext}`));
|
|
289
|
+
});
|
|
290
|
+
return exists;
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
if (isSame && existAppDir(apiDir)) {
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (isSame && existAppFile(apiDir)) {
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return true;
|
|
302
|
+
}
|
|
303
|
+
|
|
250
304
|
validateAbsolute(filename, paramsName) {
|
|
251
305
|
if (!_path.default.isAbsolute(filename)) {
|
|
252
306
|
throw new Error(`The ${paramsName} ${filename} is not a abolute path`);
|
|
@@ -90,7 +90,9 @@ const enableRegister = requireFn => {
|
|
|
90
90
|
const isFunction = input => input && {}.toString.call(input) === '[object Function]';
|
|
91
91
|
|
|
92
92
|
const requireHandlerModule = enableRegister(modulePath => {
|
|
93
|
-
|
|
93
|
+
// 测试环境不走缓存,因为缓存的 h andler 文件,会被 mockAPI 函数进行 mock,升级 jest28,setupFilesAfterEnv 能做异步操作的话,可解此问题
|
|
94
|
+
const originRequire = process.env.NODE_ENV === 'test' ? jest.requireActual : require;
|
|
95
|
+
const module = originRequire(modulePath);
|
|
94
96
|
|
|
95
97
|
if (isFunction(module)) {
|
|
96
98
|
return {
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.registerPaths = exports.getRelativeRuntimePath = exports.createMatchPath = void 0;
|
|
7
|
+
|
|
8
|
+
var path = _interopRequireWildcard(require("path"));
|
|
9
|
+
|
|
10
|
+
var os = _interopRequireWildcard(require("os"));
|
|
11
|
+
|
|
12
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
13
|
+
|
|
14
|
+
var _module = _interopRequireDefault(require("module"));
|
|
15
|
+
|
|
16
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
+
|
|
18
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
19
|
+
|
|
20
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
21
|
+
|
|
22
|
+
const getRelativeRuntimePath = (appDirectory, serverRuntimePath) => {
|
|
23
|
+
let relativeRuntimePath = '';
|
|
24
|
+
|
|
25
|
+
if (os.platform() === 'win32') {
|
|
26
|
+
// isRelative function in babel-plugin-resolver plugin can't handle windows relative path correctly, see babel-plugin-resolver's utils.
|
|
27
|
+
relativeRuntimePath = `../${path.relative(appDirectory, serverRuntimePath)}`;
|
|
28
|
+
} else {
|
|
29
|
+
// Look up one level, because the artifacts after build have dist directories
|
|
30
|
+
relativeRuntimePath = path.join('../', path.relative(appDirectory, serverRuntimePath));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
|
|
34
|
+
relativeRuntimePath = `./${path.relative(appDirectory, serverRuntimePath)}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return relativeRuntimePath;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
exports.getRelativeRuntimePath = getRelativeRuntimePath;
|
|
41
|
+
|
|
42
|
+
const sortByLongestPrefix = arr => {
|
|
43
|
+
return arr.concat().sort((a, b) => b.length - a.length);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const createMatchPath = paths => {
|
|
47
|
+
const sortedKeys = sortByLongestPrefix(Object.keys(paths));
|
|
48
|
+
const sortedPaths = {};
|
|
49
|
+
sortedKeys.forEach(key => {
|
|
50
|
+
sortedPaths[key] = paths[key];
|
|
51
|
+
});
|
|
52
|
+
return request => {
|
|
53
|
+
const found = Object.keys(sortedPaths).find(key => {
|
|
54
|
+
return request.startsWith(key);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
if (found) {
|
|
58
|
+
let foundPaths = sortedPaths[found];
|
|
59
|
+
|
|
60
|
+
if (!Array.isArray(foundPaths)) {
|
|
61
|
+
foundPaths = [foundPaths];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
foundPaths = foundPaths.filter(foundPath => path.isAbsolute(foundPath));
|
|
65
|
+
|
|
66
|
+
for (const p of foundPaths) {
|
|
67
|
+
const foundPath = request.replace(found, p);
|
|
68
|
+
|
|
69
|
+
if (_fs.default.existsSync(foundPath)) {
|
|
70
|
+
return foundPath;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return request.replace(found, foundPaths[0]);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return null;
|
|
78
|
+
};
|
|
79
|
+
}; // every path must be a absolute path;
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
exports.createMatchPath = createMatchPath;
|
|
83
|
+
|
|
84
|
+
const registerPaths = paths => {
|
|
85
|
+
const originalResolveFilename = _module.default._resolveFilename; // eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
86
|
+
|
|
87
|
+
const {
|
|
88
|
+
builtinModules
|
|
89
|
+
} = _module.default;
|
|
90
|
+
const matchPath = createMatchPath(paths);
|
|
91
|
+
|
|
92
|
+
_module.default._resolveFilename = function (request, _parent) {
|
|
93
|
+
const isCoreModule = builtinModules.includes(request);
|
|
94
|
+
|
|
95
|
+
if (!isCoreModule) {
|
|
96
|
+
const matched = matchPath(request);
|
|
97
|
+
|
|
98
|
+
if (matched) {
|
|
99
|
+
// eslint-disable-next-line prefer-rest-params
|
|
100
|
+
const modifiedArguments = [matched, ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above.
|
|
101
|
+
|
|
102
|
+
return originalResolveFilename.apply(this, modifiedArguments);
|
|
103
|
+
}
|
|
104
|
+
} // eslint-disable-next-line prefer-rest-params
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
return originalResolveFilename.apply(this, arguments);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
return () => {
|
|
111
|
+
_module.default._resolveFilename = originalResolveFilename;
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
exports.registerPaths = registerPaths;
|