@modern-js/bff-core 1.17.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.
@@ -3,11 +3,12 @@ export const AllHttpMethods = Object.values(HttpMethod);
3
3
  export let APIMode;
4
4
 
5
5
  (function (APIMode) {
6
- APIMode["FARMEWORK"] = "FARMEWORK";
7
- APIMode["FUNCTION"] = "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,30 +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
 
20
- _defineProperty(this, "lambdaDir", void 0);
23
+ _defineProperty(this, "existLambdaDir", void 0);
21
24
 
22
- _defineProperty(this, "existLambda", void 0);
25
+ _defineProperty(this, "lambdaDir", void 0);
23
26
 
24
27
  _defineProperty(this, "prefix", void 0);
25
28
 
26
- _defineProperty(this, "apiFiles", void 0);
29
+ _defineProperty(this, "apiFiles", []);
27
30
 
28
- _defineProperty(this, "getAPIMode", apiDir => {
31
+ _defineProperty(this, "getExactApiMode", apiDir => {
29
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');
30
36
 
31
- if (exist(FRAMEWORK_MODE_LAMBDA_DIR)) {
37
+ if (existLambdaDir || existAppDir || existAppFile) {
32
38
  return APIMode.FARMEWORK;
33
39
  }
34
40
 
@@ -42,29 +48,25 @@ export class ApiRouter {
42
48
  return this.lambdaDir;
43
49
  }
44
50
 
45
- const mode = this.getAPIMode(apiDir);
46
- 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;
47
52
  return lambdaDir;
48
53
  });
49
54
 
50
- this.apiFiles = [];
51
-
52
- if (_apiDir) {
53
- this.validateAbsolute(_apiDir, 'apiDir');
54
- }
55
-
56
- if (_lambdaDir) {
57
- this.validateAbsolute(_lambdaDir, 'lambdaDir');
58
- }
59
-
55
+ this.validateAbsolute(_apiDir, 'apiDir');
56
+ this.validateAbsolute(_lambdaDir, 'lambdaDir');
60
57
  this.prefix = this.initPrefix(prefix);
61
58
  this.apiDir = _apiDir;
59
+ this.apiMode = this.getExactApiMode(_apiDir);
62
60
  this.lambdaDir = _lambdaDir || this.getExactLambdaDir(this.apiDir);
63
- this.existLambda = this.checkExistLambda(this.apiDir, this.lambdaDir);
61
+ this.existLambdaDir = fs.existsSync(this.lambdaDir);
64
62
  }
65
63
 
66
64
  isExistLambda() {
67
- return this.existLambda;
65
+ return this.existLambdaDir;
66
+ }
67
+
68
+ getApiMode() {
69
+ return this.apiMode;
68
70
  }
69
71
 
70
72
  getLambdaDir() {
@@ -72,7 +74,7 @@ export class ApiRouter {
72
74
  }
73
75
 
74
76
  isApiFile(filename) {
75
- if (this.existLambda) {
77
+ if (this.existLambdaDir) {
76
78
  return false;
77
79
  }
78
80
 
@@ -184,7 +186,7 @@ export class ApiRouter {
184
186
  }
185
187
 
186
188
  loadApiFiles() {
187
- if (!this.existLambda) {
189
+ if (!this.existLambdaDir) {
188
190
  return [];
189
191
  } // eslint-disable-next-line no-multi-assign
190
192
 
@@ -194,7 +196,7 @@ export class ApiRouter {
194
196
  }
195
197
 
196
198
  getApiFiles() {
197
- if (!this.existLambda) {
199
+ if (!this.existLambdaDir) {
198
200
  return [];
199
201
  }
200
202
 
@@ -226,39 +228,8 @@ export class ApiRouter {
226
228
  return prefix || '/api';
227
229
  }
228
230
 
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
-
260
231
  validateAbsolute(filename, paramsName) {
261
- if (!path.isAbsolute(filename)) {
232
+ if (typeof filename === 'string' && !path.isAbsolute(filename)) {
262
233
  throw new Error(`The ${paramsName} ${filename} is not a abolute path`);
263
234
  }
264
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"] = "FARMEWORK";
17
- APIMode["FUNCTION"] = "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,25 +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
 
63
- _defineProperty(this, "lambdaDir", void 0);
66
+ _defineProperty(this, "existLambdaDir", void 0);
64
67
 
65
- _defineProperty(this, "existLambda", void 0);
68
+ _defineProperty(this, "lambdaDir", void 0);
66
69
 
67
70
  _defineProperty(this, "prefix", void 0);
68
71
 
69
- _defineProperty(this, "apiFiles", void 0);
72
+ _defineProperty(this, "apiFiles", []);
70
73
 
71
- _defineProperty(this, "getAPIMode", apiDir => {
74
+ _defineProperty(this, "getExactApiMode", apiDir => {
72
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');
73
79
 
74
- if (exist(_constants.FRAMEWORK_MODE_LAMBDA_DIR)) {
80
+ if (existLambdaDir || existAppDir || existAppFile) {
75
81
  return _constants.APIMode.FARMEWORK;
76
82
  }
77
83
 
@@ -85,29 +91,25 @@ class ApiRouter {
85
91
  return this.lambdaDir;
86
92
  }
87
93
 
88
- const mode = this.getAPIMode(apiDir);
89
- 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;
90
95
  return lambdaDir;
91
96
  });
92
97
 
93
- this.apiFiles = [];
94
-
95
- if (_apiDir) {
96
- this.validateAbsolute(_apiDir, 'apiDir');
97
- }
98
-
99
- if (_lambdaDir) {
100
- this.validateAbsolute(_lambdaDir, 'lambdaDir');
101
- }
102
-
98
+ this.validateAbsolute(_apiDir, 'apiDir');
99
+ this.validateAbsolute(_lambdaDir, 'lambdaDir');
103
100
  this.prefix = this.initPrefix(prefix);
104
101
  this.apiDir = _apiDir;
102
+ this.apiMode = this.getExactApiMode(_apiDir);
105
103
  this.lambdaDir = _lambdaDir || this.getExactLambdaDir(this.apiDir);
106
- this.existLambda = this.checkExistLambda(this.apiDir, this.lambdaDir);
104
+ this.existLambdaDir = _utils.fs.existsSync(this.lambdaDir);
107
105
  }
108
106
 
109
107
  isExistLambda() {
110
- return this.existLambda;
108
+ return this.existLambdaDir;
109
+ }
110
+
111
+ getApiMode() {
112
+ return this.apiMode;
111
113
  }
112
114
 
113
115
  getLambdaDir() {
@@ -115,7 +117,7 @@ class ApiRouter {
115
117
  }
116
118
 
117
119
  isApiFile(filename) {
118
- if (this.existLambda) {
120
+ if (this.existLambdaDir) {
119
121
  return false;
120
122
  }
121
123
 
@@ -228,7 +230,7 @@ class ApiRouter {
228
230
  }
229
231
 
230
232
  loadApiFiles() {
231
- if (!this.existLambda) {
233
+ if (!this.existLambdaDir) {
232
234
  return [];
233
235
  } // eslint-disable-next-line no-multi-assign
234
236
 
@@ -238,7 +240,7 @@ class ApiRouter {
238
240
  }
239
241
 
240
242
  getApiFiles() {
241
- if (!this.existLambda) {
243
+ if (!this.existLambdaDir) {
242
244
  return [];
243
245
  }
244
246
 
@@ -270,39 +272,8 @@ class ApiRouter {
270
272
  return prefix || '/api';
271
273
  }
272
274
 
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
-
304
275
  validateAbsolute(filename, paramsName) {
305
- if (!_path.default.isAbsolute(filename)) {
276
+ if (typeof filename === 'string' && !_path.default.isAbsolute(filename)) {
306
277
  throw new Error(`The ${paramsName} ${filename} is not a abolute path`);
307
278
  }
308
279
  }
@@ -3,14 +3,15 @@ export declare enum APIMode {
3
3
  /**
4
4
  * 框架模式
5
5
  */
6
- FARMEWORK = "FARMEWORK",
6
+ FARMEWORK = "framework",
7
7
 
8
8
  /**
9
9
  * 函数模式
10
10
  */
11
- FUNCTION = "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,12 +1,14 @@
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
- private existLambda;
10
12
  private prefix;
11
13
  private apiFiles;
12
14
  constructor({
@@ -19,6 +21,7 @@ export declare class ApiRouter {
19
21
  prefix?: string;
20
22
  });
21
23
  isExistLambda(): boolean;
24
+ getApiMode(): APIMode;
22
25
  getLambdaDir(): string;
23
26
  isApiFile(filename: string): boolean;
24
27
  getSingleModuleHandlers(filename: string): APIHandlerInfo[] | null;
@@ -35,9 +38,8 @@ export declare class ApiRouter {
35
38
  */
36
39
 
37
40
  private initPrefix;
38
- private checkExistLambda;
39
41
  private validateAbsolute;
40
- private getAPIMode;
42
+ private getExactApiMode;
41
43
  private createExistChecker;
42
44
  private getExactLambdaDir;
43
45
  private getModuleInfos;
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.17.0",
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",
@@ -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": {