@modern-js/bff-core 1.15.0 → 1.17.1-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 +34 -0
- package/dist/js/modern/router/constants.js +3 -2
- package/dist/js/modern/router/index.js +44 -19
- package/dist/js/node/router/constants.js +5 -3
- package/dist/js/node/router/index.js +43 -18
- package/dist/types/router/constants.d.ts +3 -2
- package/dist/types/router/index.d.ts +8 -2
- package/package.json +7 -6
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
|
|
@@ -3,11 +3,12 @@ export const AllHttpMethods = Object.values(HttpMethod);
|
|
|
3
3
|
export let APIMode;
|
|
4
4
|
|
|
5
5
|
(function (APIMode) {
|
|
6
|
-
APIMode["FARMEWORK"] = "
|
|
7
|
-
APIMode["FUNCTION"] = "
|
|
6
|
+
APIMode["FARMEWORK"] = "framework";
|
|
7
|
+
APIMode["FUNCTION"] = "function";
|
|
8
8
|
})(APIMode || (APIMode = {}));
|
|
9
9
|
|
|
10
10
|
export const FRAMEWORK_MODE_LAMBDA_DIR = 'lambda';
|
|
11
|
+
export const FRAMEWORK_MODE_APP_DIR = 'app';
|
|
11
12
|
export const INDEX_SUFFIX = 'index';
|
|
12
13
|
export const API_DIR = 'api';
|
|
13
14
|
export const API_FILE_RULES = ['**/*.[tj]s', '!**/_*', '!**/_*/**/*.[tj]s', '!**/*.test.js', '!**/*.test.ts', '!**/*.d.ts', '!__test__/*.ts', '!__tests__/*.ts', '!node_modules/**', '!bootstrap.js'];
|
|
@@ -5,28 +5,36 @@ import { fs, logger } from '@modern-js/utils';
|
|
|
5
5
|
import 'reflect-metadata';
|
|
6
6
|
import { HttpMethod, httpMethods, OperatorType, TriggerType } from "../types";
|
|
7
7
|
import { debug } from "../utils";
|
|
8
|
-
import { APIMode, FRAMEWORK_MODE_LAMBDA_DIR, API_FILE_RULES } from "./constants";
|
|
8
|
+
import { APIMode, FRAMEWORK_MODE_LAMBDA_DIR, API_FILE_RULES, FRAMEWORK_MODE_APP_DIR } from "./constants";
|
|
9
9
|
import { getFiles, getPathFromFilename, requireHandlerModule, sortRoutes } from "./utils";
|
|
10
10
|
export * from "./types";
|
|
11
11
|
export * from "./constants";
|
|
12
12
|
export class ApiRouter {
|
|
13
|
+
// lambdaDir is the dir which equal to the apiDir in function mode, and equal to the api/lambda dir in framework mode
|
|
13
14
|
constructor({
|
|
14
15
|
apiDir: _apiDir,
|
|
15
16
|
lambdaDir: _lambdaDir,
|
|
16
17
|
prefix
|
|
17
18
|
}) {
|
|
19
|
+
_defineProperty(this, "apiMode", void 0);
|
|
20
|
+
|
|
18
21
|
_defineProperty(this, "apiDir", void 0);
|
|
19
22
|
|
|
23
|
+
_defineProperty(this, "existLambdaDir", void 0);
|
|
24
|
+
|
|
20
25
|
_defineProperty(this, "lambdaDir", void 0);
|
|
21
26
|
|
|
22
27
|
_defineProperty(this, "prefix", void 0);
|
|
23
28
|
|
|
24
|
-
_defineProperty(this, "apiFiles",
|
|
29
|
+
_defineProperty(this, "apiFiles", []);
|
|
25
30
|
|
|
26
|
-
_defineProperty(this, "
|
|
31
|
+
_defineProperty(this, "getExactApiMode", apiDir => {
|
|
27
32
|
const exist = this.createExistChecker(apiDir);
|
|
33
|
+
const existLambdaDir = exist(FRAMEWORK_MODE_LAMBDA_DIR);
|
|
34
|
+
const existAppDir = exist(FRAMEWORK_MODE_APP_DIR);
|
|
35
|
+
const existAppFile = exist('app.ts') || exist('app.js');
|
|
28
36
|
|
|
29
|
-
if (
|
|
37
|
+
if (existLambdaDir || existAppDir || existAppFile) {
|
|
30
38
|
return APIMode.FARMEWORK;
|
|
31
39
|
}
|
|
32
40
|
|
|
@@ -35,32 +43,41 @@ export class ApiRouter {
|
|
|
35
43
|
|
|
36
44
|
_defineProperty(this, "createExistChecker", base => target => fs.pathExistsSync(path.resolve(base, target)));
|
|
37
45
|
|
|
38
|
-
_defineProperty(this, "
|
|
46
|
+
_defineProperty(this, "getExactLambdaDir", apiDir => {
|
|
39
47
|
if (this.lambdaDir) {
|
|
40
48
|
return this.lambdaDir;
|
|
41
49
|
}
|
|
42
50
|
|
|
43
|
-
const
|
|
44
|
-
const lambdaDir = mode === APIMode.FARMEWORK ? path.join(apiDir, FRAMEWORK_MODE_LAMBDA_DIR) : apiDir;
|
|
51
|
+
const lambdaDir = this.apiMode === APIMode.FARMEWORK ? path.join(apiDir, FRAMEWORK_MODE_LAMBDA_DIR) : apiDir;
|
|
45
52
|
return lambdaDir;
|
|
46
53
|
});
|
|
47
54
|
|
|
48
|
-
this.
|
|
55
|
+
this.validateAbsolute(_apiDir, 'apiDir');
|
|
56
|
+
this.validateAbsolute(_lambdaDir, 'lambdaDir');
|
|
57
|
+
this.prefix = this.initPrefix(prefix);
|
|
58
|
+
this.apiDir = _apiDir;
|
|
59
|
+
this.apiMode = this.getExactApiMode(_apiDir);
|
|
60
|
+
this.lambdaDir = _lambdaDir || this.getExactLambdaDir(this.apiDir);
|
|
61
|
+
this.existLambdaDir = fs.existsSync(this.lambdaDir);
|
|
62
|
+
}
|
|
49
63
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
64
|
+
isExistLambda() {
|
|
65
|
+
return this.existLambdaDir;
|
|
66
|
+
}
|
|
53
67
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
68
|
+
getApiMode() {
|
|
69
|
+
return this.apiMode;
|
|
70
|
+
}
|
|
57
71
|
|
|
58
|
-
|
|
59
|
-
this.
|
|
60
|
-
this.lambdaDir = _lambdaDir || this.getLambdaDir(this.apiDir);
|
|
72
|
+
getLambdaDir() {
|
|
73
|
+
return this.lambdaDir;
|
|
61
74
|
}
|
|
62
75
|
|
|
63
76
|
isApiFile(filename) {
|
|
77
|
+
if (this.existLambdaDir) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
|
|
64
81
|
if (!this.apiFiles.includes(filename)) {
|
|
65
82
|
return false;
|
|
66
83
|
}
|
|
@@ -169,12 +186,20 @@ export class ApiRouter {
|
|
|
169
186
|
}
|
|
170
187
|
|
|
171
188
|
loadApiFiles() {
|
|
172
|
-
|
|
189
|
+
if (!this.existLambdaDir) {
|
|
190
|
+
return [];
|
|
191
|
+
} // eslint-disable-next-line no-multi-assign
|
|
192
|
+
|
|
193
|
+
|
|
173
194
|
const apiFiles = this.apiFiles = getFiles(this.lambdaDir, API_FILE_RULES);
|
|
174
195
|
return apiFiles;
|
|
175
196
|
}
|
|
176
197
|
|
|
177
198
|
getApiFiles() {
|
|
199
|
+
if (!this.existLambdaDir) {
|
|
200
|
+
return [];
|
|
201
|
+
}
|
|
202
|
+
|
|
178
203
|
if (this.apiFiles.length > 0) {
|
|
179
204
|
return this.apiFiles;
|
|
180
205
|
}
|
|
@@ -204,7 +229,7 @@ export class ApiRouter {
|
|
|
204
229
|
}
|
|
205
230
|
|
|
206
231
|
validateAbsolute(filename, paramsName) {
|
|
207
|
-
if (!path.isAbsolute(filename)) {
|
|
232
|
+
if (typeof filename === 'string' && !path.isAbsolute(filename)) {
|
|
208
233
|
throw new Error(`The ${paramsName} ${filename} is not a abolute path`);
|
|
209
234
|
}
|
|
210
235
|
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.INDEX_SUFFIX = exports.FRAMEWORK_MODE_LAMBDA_DIR = exports.AllHttpMethods = exports.API_FILE_RULES = exports.API_DIR = exports.APIMode = void 0;
|
|
6
|
+
exports.INDEX_SUFFIX = exports.FRAMEWORK_MODE_LAMBDA_DIR = exports.FRAMEWORK_MODE_APP_DIR = exports.AllHttpMethods = exports.API_FILE_RULES = exports.API_DIR = exports.APIMode = void 0;
|
|
7
7
|
|
|
8
8
|
var _types = require("../types");
|
|
9
9
|
|
|
@@ -13,12 +13,14 @@ let APIMode;
|
|
|
13
13
|
exports.APIMode = APIMode;
|
|
14
14
|
|
|
15
15
|
(function (APIMode) {
|
|
16
|
-
APIMode["FARMEWORK"] = "
|
|
17
|
-
APIMode["FUNCTION"] = "
|
|
16
|
+
APIMode["FARMEWORK"] = "framework";
|
|
17
|
+
APIMode["FUNCTION"] = "function";
|
|
18
18
|
})(APIMode || (exports.APIMode = APIMode = {}));
|
|
19
19
|
|
|
20
20
|
const FRAMEWORK_MODE_LAMBDA_DIR = 'lambda';
|
|
21
21
|
exports.FRAMEWORK_MODE_LAMBDA_DIR = FRAMEWORK_MODE_LAMBDA_DIR;
|
|
22
|
+
const FRAMEWORK_MODE_APP_DIR = 'app';
|
|
23
|
+
exports.FRAMEWORK_MODE_APP_DIR = FRAMEWORK_MODE_APP_DIR;
|
|
22
24
|
const INDEX_SUFFIX = 'index';
|
|
23
25
|
exports.INDEX_SUFFIX = INDEX_SUFFIX;
|
|
24
26
|
const API_DIR = 'api';
|
|
@@ -53,23 +53,31 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
53
53
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
54
54
|
|
|
55
55
|
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
|
|
56
57
|
constructor({
|
|
57
58
|
apiDir: _apiDir,
|
|
58
59
|
lambdaDir: _lambdaDir,
|
|
59
60
|
prefix
|
|
60
61
|
}) {
|
|
62
|
+
_defineProperty(this, "apiMode", void 0);
|
|
63
|
+
|
|
61
64
|
_defineProperty(this, "apiDir", void 0);
|
|
62
65
|
|
|
66
|
+
_defineProperty(this, "existLambdaDir", void 0);
|
|
67
|
+
|
|
63
68
|
_defineProperty(this, "lambdaDir", void 0);
|
|
64
69
|
|
|
65
70
|
_defineProperty(this, "prefix", void 0);
|
|
66
71
|
|
|
67
|
-
_defineProperty(this, "apiFiles",
|
|
72
|
+
_defineProperty(this, "apiFiles", []);
|
|
68
73
|
|
|
69
|
-
_defineProperty(this, "
|
|
74
|
+
_defineProperty(this, "getExactApiMode", apiDir => {
|
|
70
75
|
const exist = this.createExistChecker(apiDir);
|
|
76
|
+
const existLambdaDir = exist(_constants.FRAMEWORK_MODE_LAMBDA_DIR);
|
|
77
|
+
const existAppDir = exist(_constants.FRAMEWORK_MODE_APP_DIR);
|
|
78
|
+
const existAppFile = exist('app.ts') || exist('app.js');
|
|
71
79
|
|
|
72
|
-
if (
|
|
80
|
+
if (existLambdaDir || existAppDir || existAppFile) {
|
|
73
81
|
return _constants.APIMode.FARMEWORK;
|
|
74
82
|
}
|
|
75
83
|
|
|
@@ -78,32 +86,41 @@ class ApiRouter {
|
|
|
78
86
|
|
|
79
87
|
_defineProperty(this, "createExistChecker", base => target => _utils.fs.pathExistsSync(_path.default.resolve(base, target)));
|
|
80
88
|
|
|
81
|
-
_defineProperty(this, "
|
|
89
|
+
_defineProperty(this, "getExactLambdaDir", apiDir => {
|
|
82
90
|
if (this.lambdaDir) {
|
|
83
91
|
return this.lambdaDir;
|
|
84
92
|
}
|
|
85
93
|
|
|
86
|
-
const
|
|
87
|
-
const lambdaDir = mode === _constants.APIMode.FARMEWORK ? _path.default.join(apiDir, _constants.FRAMEWORK_MODE_LAMBDA_DIR) : apiDir;
|
|
94
|
+
const lambdaDir = this.apiMode === _constants.APIMode.FARMEWORK ? _path.default.join(apiDir, _constants.FRAMEWORK_MODE_LAMBDA_DIR) : apiDir;
|
|
88
95
|
return lambdaDir;
|
|
89
96
|
});
|
|
90
97
|
|
|
91
|
-
this.
|
|
98
|
+
this.validateAbsolute(_apiDir, 'apiDir');
|
|
99
|
+
this.validateAbsolute(_lambdaDir, 'lambdaDir');
|
|
100
|
+
this.prefix = this.initPrefix(prefix);
|
|
101
|
+
this.apiDir = _apiDir;
|
|
102
|
+
this.apiMode = this.getExactApiMode(_apiDir);
|
|
103
|
+
this.lambdaDir = _lambdaDir || this.getExactLambdaDir(this.apiDir);
|
|
104
|
+
this.existLambdaDir = _utils.fs.existsSync(this.lambdaDir);
|
|
105
|
+
}
|
|
92
106
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
107
|
+
isExistLambda() {
|
|
108
|
+
return this.existLambdaDir;
|
|
109
|
+
}
|
|
96
110
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
111
|
+
getApiMode() {
|
|
112
|
+
return this.apiMode;
|
|
113
|
+
}
|
|
100
114
|
|
|
101
|
-
|
|
102
|
-
this.
|
|
103
|
-
this.lambdaDir = _lambdaDir || this.getLambdaDir(this.apiDir);
|
|
115
|
+
getLambdaDir() {
|
|
116
|
+
return this.lambdaDir;
|
|
104
117
|
}
|
|
105
118
|
|
|
106
119
|
isApiFile(filename) {
|
|
120
|
+
if (this.existLambdaDir) {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
|
|
107
124
|
if (!this.apiFiles.includes(filename)) {
|
|
108
125
|
return false;
|
|
109
126
|
}
|
|
@@ -213,12 +230,20 @@ class ApiRouter {
|
|
|
213
230
|
}
|
|
214
231
|
|
|
215
232
|
loadApiFiles() {
|
|
216
|
-
|
|
233
|
+
if (!this.existLambdaDir) {
|
|
234
|
+
return [];
|
|
235
|
+
} // eslint-disable-next-line no-multi-assign
|
|
236
|
+
|
|
237
|
+
|
|
217
238
|
const apiFiles = this.apiFiles = (0, _utils3.getFiles)(this.lambdaDir, _constants.API_FILE_RULES);
|
|
218
239
|
return apiFiles;
|
|
219
240
|
}
|
|
220
241
|
|
|
221
242
|
getApiFiles() {
|
|
243
|
+
if (!this.existLambdaDir) {
|
|
244
|
+
return [];
|
|
245
|
+
}
|
|
246
|
+
|
|
222
247
|
if (this.apiFiles.length > 0) {
|
|
223
248
|
return this.apiFiles;
|
|
224
249
|
}
|
|
@@ -248,7 +273,7 @@ class ApiRouter {
|
|
|
248
273
|
}
|
|
249
274
|
|
|
250
275
|
validateAbsolute(filename, paramsName) {
|
|
251
|
-
if (!_path.default.isAbsolute(filename)) {
|
|
276
|
+
if (typeof filename === 'string' && !_path.default.isAbsolute(filename)) {
|
|
252
277
|
throw new Error(`The ${paramsName} ${filename} is not a abolute path`);
|
|
253
278
|
}
|
|
254
279
|
}
|
|
@@ -3,14 +3,15 @@ export declare enum APIMode {
|
|
|
3
3
|
/**
|
|
4
4
|
* 框架模式
|
|
5
5
|
*/
|
|
6
|
-
FARMEWORK = "
|
|
6
|
+
FARMEWORK = "framework",
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* 函数模式
|
|
10
10
|
*/
|
|
11
|
-
FUNCTION = "
|
|
11
|
+
FUNCTION = "function",
|
|
12
12
|
}
|
|
13
13
|
export declare const FRAMEWORK_MODE_LAMBDA_DIR = "lambda";
|
|
14
|
+
export declare const FRAMEWORK_MODE_APP_DIR = "app";
|
|
14
15
|
export declare const INDEX_SUFFIX = "index";
|
|
15
16
|
export declare const API_DIR = "api";
|
|
16
17
|
export declare const API_FILE_RULES: string[];
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
2
|
import { HttpMethod } from '../types';
|
|
3
|
+
import { APIMode } from './constants';
|
|
3
4
|
import { ApiHandler, APIHandlerInfo } from './types';
|
|
4
5
|
export * from './types';
|
|
5
6
|
export * from './constants';
|
|
6
7
|
export declare class ApiRouter {
|
|
8
|
+
private apiMode;
|
|
7
9
|
private apiDir;
|
|
10
|
+
private existLambdaDir;
|
|
8
11
|
private lambdaDir;
|
|
9
12
|
private prefix;
|
|
10
13
|
private apiFiles;
|
|
@@ -17,6 +20,9 @@ export declare class ApiRouter {
|
|
|
17
20
|
lambdaDir?: string;
|
|
18
21
|
prefix?: string;
|
|
19
22
|
});
|
|
23
|
+
isExistLambda(): boolean;
|
|
24
|
+
getApiMode(): APIMode;
|
|
25
|
+
getLambdaDir(): string;
|
|
20
26
|
isApiFile(filename: string): boolean;
|
|
21
27
|
getSingleModuleHandlers(filename: string): APIHandlerInfo[] | null;
|
|
22
28
|
getHandlerInfo(filename: string, originFuncName: string, handler: ApiHandler): APIHandlerInfo | null;
|
|
@@ -33,9 +39,9 @@ export declare class ApiRouter {
|
|
|
33
39
|
|
|
34
40
|
private initPrefix;
|
|
35
41
|
private validateAbsolute;
|
|
36
|
-
private
|
|
42
|
+
private getExactApiMode;
|
|
37
43
|
private createExistChecker;
|
|
38
|
-
private
|
|
44
|
+
private getExactLambdaDir;
|
|
39
45
|
private getModuleInfos;
|
|
40
46
|
private getModuleInfo;
|
|
41
47
|
private getHandlerInfos;
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.
|
|
14
|
+
"version": "1.17.1-beta.0",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -25,16 +25,16 @@
|
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@babel/runtime": "^7.18.0",
|
|
28
|
-
"@modern-js/bff-runtime": "1.
|
|
29
|
-
"@modern-js/utils": "1.
|
|
28
|
+
"@modern-js/bff-runtime": "1.17.0",
|
|
29
|
+
"@modern-js/utils": "1.17.0",
|
|
30
30
|
"esbuild": "^0.14.38",
|
|
31
31
|
"esbuild-register": "^3.3.3",
|
|
32
32
|
"koa-compose": "^4.1.0",
|
|
33
33
|
"reflect-metadata": "^0.1.13"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@scripts/build": "1.
|
|
37
|
-
"@scripts/jest-config": "1.
|
|
36
|
+
"@scripts/build": "1.17.0",
|
|
37
|
+
"@scripts/jest-config": "1.17.0",
|
|
38
38
|
"@types/jest": "^27",
|
|
39
39
|
"@types/koa-compose": "^3.2.5",
|
|
40
40
|
"@types/node": "^14",
|
|
@@ -58,7 +58,8 @@
|
|
|
58
58
|
},
|
|
59
59
|
"publishConfig": {
|
|
60
60
|
"registry": "https://registry.npmjs.org/",
|
|
61
|
-
"access": "public"
|
|
61
|
+
"access": "public",
|
|
62
|
+
"types": "./dist/types/index.d.ts"
|
|
62
63
|
},
|
|
63
64
|
"wireit": {
|
|
64
65
|
"build": {
|