@modern-js/bff-core 2.5.1-alpha.5 → 2.6.1-alpha.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 CHANGED
@@ -1,5 +1,15 @@
1
1
  # @modern-js/bff-core
2
2
 
3
+ ## 2.6.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [e1f799e]
8
+ - Updated dependencies [7915ab3]
9
+ - Updated dependencies [0fe658a]
10
+ - @modern-js/utils@2.6.0
11
+ - @modern-js/bff-runtime@2.6.0
12
+
3
13
  ## 2.5.0
4
14
 
5
15
  ### Patch Changes
package/dist/cjs/api.js CHANGED
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -17,16 +17,20 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
23
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
- var generate_client_exports = {};
25
- __export(generate_client_exports, {
28
+ var generateClient_exports = {};
29
+ __export(generateClient_exports, {
26
30
  DEFAULT_CLIENT_REQUEST_CREATOR: () => DEFAULT_CLIENT_REQUEST_CREATOR,
27
31
  generateClient: () => generateClient
28
32
  });
29
- module.exports = __toCommonJS(generate_client_exports);
33
+ module.exports = __toCommonJS(generateClient_exports);
30
34
  var path = __toESM(require("path"));
31
35
  var import_router = require("../router");
32
36
  var import_result = require("./result");
@@ -77,7 +81,7 @@ const generateClient = async ({
77
81
  }
78
82
  const upperHttpMethod = httpMethod.toUpperCase();
79
83
  const routeName = routePath;
80
- handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', ${process.env.PORT || String(port)}, '${httpMethodDecider}', ${fetcher ? `, fetch` : ""});
84
+ handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', ${process.env.PORT || String(port)}, '${httpMethodDecider ? httpMethodDecider : "functionName"}' ${fetcher ? `, fetch` : ""});
81
85
  `;
82
86
  }
83
87
  const importCode = `import { createRequest } from '${requestCreator}';
@@ -14,4 +14,4 @@ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "defau
14
14
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
15
  var client_exports = {};
16
16
  module.exports = __toCommonJS(client_exports);
17
- __reExport(client_exports, require("./generate-client"), module.exports);
17
+ __reExport(client_exports, require("./generateClient"), module.exports);
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
18
18
  };
19
19
  var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
20
20
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
21
25
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
26
  mod
23
27
  ));
@@ -44,9 +48,9 @@ class ApiRouter {
44
48
  httpMethodDecider = "functionName"
45
49
  }) {
46
50
  this.apiFiles = [];
47
- this.getExactApiMode = (apiDir) => {
51
+ this.getExactApiMode = (apiDir, lambdaDir) => {
48
52
  const exist = this.createExistChecker(apiDir);
49
- const existLambdaDir = exist(import_constants.FRAMEWORK_MODE_LAMBDA_DIR);
53
+ const existLambdaDir = lambdaDir && import_utils.fs.pathExistsSync(lambdaDir) || exist(import_constants.FRAMEWORK_MODE_LAMBDA_DIR);
50
54
  const existAppDir = exist(import_constants.FRAMEWORK_MODE_APP_DIR);
51
55
  const existAppFile = exist("app.ts") || exist("app.js");
52
56
  if (existLambdaDir || existAppDir || existAppFile) {
@@ -55,21 +59,21 @@ class ApiRouter {
55
59
  return import_constants.APIMode.FUNCTION;
56
60
  };
57
61
  this.createExistChecker = (base) => (target) => import_utils.fs.pathExistsSync(import_path.default.resolve(base, target));
58
- this.getExactLambdaDir = (apiDir) => {
59
- if (this.lambdaDir) {
60
- return this.lambdaDir;
62
+ this.getExactLambdaDir = (apiDir, originLambdaDir) => {
63
+ if (this.apiMode === import_constants.APIMode.FUNCTION) {
64
+ return apiDir;
61
65
  }
62
- const lambdaDir = this.apiMode === import_constants.APIMode.FARMEWORK ? import_path.default.join(apiDir, import_constants.FRAMEWORK_MODE_LAMBDA_DIR) : apiDir;
63
- return lambdaDir;
66
+ return originLambdaDir || import_path.default.join(apiDir, import_constants.FRAMEWORK_MODE_LAMBDA_DIR);
64
67
  };
65
68
  this.validateAbsolute(apiDir, "apiDir");
66
69
  this.validateAbsolute(lambdaDir, "lambdaDir");
67
70
  this.prefix = this.initPrefix(prefix);
68
71
  this.apiDir = apiDir;
69
- this.apiMode = this.getExactApiMode(apiDir);
70
72
  this.httpMethodDecider = httpMethodDecider;
71
- this.lambdaDir = lambdaDir || this.getExactLambdaDir(this.apiDir);
73
+ this.apiMode = this.getExactApiMode(apiDir, lambdaDir);
74
+ this.lambdaDir = this.getExactLambdaDir(this.apiDir, lambdaDir);
72
75
  this.existLambdaDir = import_utils.fs.existsSync(this.lambdaDir);
76
+ (0, import_utils2.debug)(`apiDir:`, this.apiDir, `lambdaDir:`, this.lambdaDir);
73
77
  }
74
78
  isExistLambda() {
75
79
  return this.existLambdaDir;
@@ -81,13 +85,10 @@ class ApiRouter {
81
85
  return this.lambdaDir;
82
86
  }
83
87
  isApiFile(filename) {
84
- if (this.existLambdaDir) {
85
- return false;
88
+ if (this.apiFiles.includes(filename)) {
89
+ return true;
86
90
  }
87
- if (!this.apiFiles.includes(filename)) {
88
- return false;
89
- }
90
- return true;
91
+ return false;
91
92
  }
92
93
  getSingleModuleHandlers(filename) {
93
94
  const moduleInfo = this.getModuleInfo(filename);
@@ -111,6 +112,7 @@ class ApiRouter {
111
112
  }
112
113
  return null;
113
114
  }
115
+ // TODO: 性能提升,开发环境,判断下 lambda 目录修改时间
114
116
  getSafeRoutePath(filename, handler) {
115
117
  this.loadApiFiles();
116
118
  this.validateValidApifile(filename);
@@ -209,6 +211,10 @@ class ApiRouter {
209
211
  (0, import_utils2.debug)("apiHandlers", apiHandlers.length, apiHandlers);
210
212
  return apiHandlers;
211
213
  }
214
+ /**
215
+ * 如果用户未传入或传入空串,默认为 /api
216
+ * 如果传入 /,则 prefix 为 /
217
+ */
212
218
  initPrefix(prefix) {
213
219
  if (prefix === "/") {
214
220
  return "";
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -48,7 +48,7 @@ const generateClient = async ({
48
48
  }
49
49
  const upperHttpMethod = httpMethod.toUpperCase();
50
50
  const routeName = routePath;
51
- handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', ${process.env.PORT || String(port)}, '${httpMethodDecider}', ${fetcher ? `, fetch` : ""});
51
+ handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', ${process.env.PORT || String(port)}, '${httpMethodDecider ? httpMethodDecider : "functionName"}' ${fetcher ? `, fetch` : ""});
52
52
  `;
53
53
  }
54
54
  const importCode = `import { createRequest } from '${requestCreator}';
@@ -1 +1 @@
1
- export * from "./generate-client";
1
+ export * from "./generateClient";
@@ -25,9 +25,9 @@ class ApiRouter {
25
25
  httpMethodDecider = "functionName"
26
26
  }) {
27
27
  this.apiFiles = [];
28
- this.getExactApiMode = (apiDir) => {
28
+ this.getExactApiMode = (apiDir, lambdaDir) => {
29
29
  const exist = this.createExistChecker(apiDir);
30
- const existLambdaDir = exist(FRAMEWORK_MODE_LAMBDA_DIR);
30
+ const existLambdaDir = lambdaDir && fs.pathExistsSync(lambdaDir) || exist(FRAMEWORK_MODE_LAMBDA_DIR);
31
31
  const existAppDir = exist(FRAMEWORK_MODE_APP_DIR);
32
32
  const existAppFile = exist("app.ts") || exist("app.js");
33
33
  if (existLambdaDir || existAppDir || existAppFile) {
@@ -36,21 +36,21 @@ class ApiRouter {
36
36
  return APIMode.FUNCTION;
37
37
  };
38
38
  this.createExistChecker = (base) => (target) => fs.pathExistsSync(path.resolve(base, target));
39
- this.getExactLambdaDir = (apiDir) => {
40
- if (this.lambdaDir) {
41
- return this.lambdaDir;
39
+ this.getExactLambdaDir = (apiDir, originLambdaDir) => {
40
+ if (this.apiMode === APIMode.FUNCTION) {
41
+ return apiDir;
42
42
  }
43
- const lambdaDir = this.apiMode === APIMode.FARMEWORK ? path.join(apiDir, FRAMEWORK_MODE_LAMBDA_DIR) : apiDir;
44
- return lambdaDir;
43
+ return originLambdaDir || path.join(apiDir, FRAMEWORK_MODE_LAMBDA_DIR);
45
44
  };
46
45
  this.validateAbsolute(apiDir, "apiDir");
47
46
  this.validateAbsolute(lambdaDir, "lambdaDir");
48
47
  this.prefix = this.initPrefix(prefix);
49
48
  this.apiDir = apiDir;
50
- this.apiMode = this.getExactApiMode(apiDir);
51
49
  this.httpMethodDecider = httpMethodDecider;
52
- this.lambdaDir = lambdaDir || this.getExactLambdaDir(this.apiDir);
50
+ this.apiMode = this.getExactApiMode(apiDir, lambdaDir);
51
+ this.lambdaDir = this.getExactLambdaDir(this.apiDir, lambdaDir);
53
52
  this.existLambdaDir = fs.existsSync(this.lambdaDir);
53
+ debug(`apiDir:`, this.apiDir, `lambdaDir:`, this.lambdaDir);
54
54
  }
55
55
  isExistLambda() {
56
56
  return this.existLambdaDir;
@@ -62,13 +62,10 @@ class ApiRouter {
62
62
  return this.lambdaDir;
63
63
  }
64
64
  isApiFile(filename) {
65
- if (this.existLambdaDir) {
66
- return false;
65
+ if (this.apiFiles.includes(filename)) {
66
+ return true;
67
67
  }
68
- if (!this.apiFiles.includes(filename)) {
69
- return false;
70
- }
71
- return true;
68
+ return false;
72
69
  }
73
70
  getSingleModuleHandlers(filename) {
74
71
  const moduleInfo = this.getModuleInfo(filename);
@@ -92,6 +89,7 @@ class ApiRouter {
92
89
  }
93
90
  return null;
94
91
  }
92
+ // TODO: 性能提升,开发环境,判断下 lambda 目录修改时间
95
93
  getSafeRoutePath(filename, handler) {
96
94
  this.loadApiFiles();
97
95
  this.validateValidApifile(filename);
@@ -190,6 +188,10 @@ class ApiRouter {
190
188
  debug("apiHandlers", apiHandlers.length, apiHandlers);
191
189
  return apiHandlers;
192
190
  }
191
+ /**
192
+ * 如果用户未传入或传入空串,默认为 /api
193
+ * 如果传入 /,则 prefix 为 /
194
+ */
193
195
  initPrefix(prefix) {
194
196
  if (prefix === "/") {
195
197
  return "";
@@ -12,7 +12,7 @@ export type GenClientOptions = {
12
12
  fetcher?: string;
13
13
  target?: string;
14
14
  requireResolve?: typeof require.resolve;
15
- httpMethodDecider: HttpMethodDecider;
15
+ httpMethodDecider?: HttpMethodDecider;
16
16
  };
17
17
  export declare const DEFAULT_CLIENT_REQUEST_CREATOR = "@modern-js/create-request";
18
18
  export declare const generateClient: ({
@@ -1 +1 @@
1
- export * from './generate-client';
1
+ export * from './generateClient';
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "2.5.1-alpha.5",
14
+ "version": "2.6.1-alpha.0",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/cjs/index.js",
@@ -25,17 +25,17 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "@babel/runtime": "^7.18.0",
28
- "@modern-js/bff-runtime": "2.5.0",
29
- "@modern-js/utils": "2.5.0",
28
+ "@modern-js/bff-runtime": "2.6.0",
29
+ "@modern-js/utils": "2.6.0",
30
30
  "esbuild": "0.15.7",
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
- "@modern-js/types": "^2.5.0",
37
- "@scripts/build": "2.5.0",
38
- "@scripts/jest-config": "2.5.0",
36
+ "@modern-js/types": "2.6.0",
37
+ "@scripts/build": "2.6.0",
38
+ "@scripts/jest-config": "2.6.0",
39
39
  "@types/jest": "^27",
40
40
  "@types/koa-compose": "^3.2.5",
41
41
  "@types/node": "^14",