@modern-js/bff-core 2.5.1-alpha.2 → 2.6.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.
Files changed (57) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/cjs/api.js +4 -0
  3. package/dist/cjs/client/generate-client.js +7 -7
  4. package/dist/cjs/index.js +0 -4
  5. package/dist/cjs/operators/http.js +1 -7
  6. package/dist/cjs/router/index.js +36 -54
  7. package/dist/cjs/router/utils.js +4 -0
  8. package/dist/cjs/utils/alias.js +4 -0
  9. package/dist/cjs/utils/meta.js +0 -8
  10. package/dist/cjs/utils/storage.js +4 -0
  11. package/dist/cjs/utils/validate.js +4 -0
  12. package/dist/esm/client/generate-client.js +3 -7
  13. package/dist/esm/index.js +0 -4
  14. package/dist/esm/operators/http.js +1 -1
  15. package/dist/esm/router/index.js +33 -55
  16. package/dist/esm/utils/meta.js +0 -6
  17. package/dist/types/client/generate-client.d.ts +1 -6
  18. package/dist/types/index.d.ts +1 -1
  19. package/dist/types/router/index.d.ts +1 -5
  20. package/dist/types/utils/meta.d.ts +1 -3
  21. package/package.json +7 -8
  22. package/dist/js/modern/api.js +0 -69
  23. package/dist/js/modern/client/generate-client.js +0 -79
  24. package/dist/js/modern/client/index.js +0 -1
  25. package/dist/js/modern/client/result.js +0 -22
  26. package/dist/js/modern/errors/http.js +0 -16
  27. package/dist/js/modern/index.js +0 -23
  28. package/dist/js/modern/operators/http.js +0 -210
  29. package/dist/js/modern/router/constants.js +0 -32
  30. package/dist/js/modern/router/index.js +0 -236
  31. package/dist/js/modern/router/types.js +0 -0
  32. package/dist/js/modern/router/utils.js +0 -83
  33. package/dist/js/modern/types.js +0 -45
  34. package/dist/js/modern/utils/alias.js +0 -76
  35. package/dist/js/modern/utils/debug.js +0 -5
  36. package/dist/js/modern/utils/index.js +0 -8
  37. package/dist/js/modern/utils/meta.js +0 -8
  38. package/dist/js/modern/utils/storage.js +0 -42
  39. package/dist/js/modern/utils/validate.js +0 -43
  40. package/dist/js/node/api.js +0 -98
  41. package/dist/js/node/client/generate-client.js +0 -109
  42. package/dist/js/node/client/index.js +0 -17
  43. package/dist/js/node/client/result.js +0 -46
  44. package/dist/js/node/errors/http.js +0 -40
  45. package/dist/js/node/index.js +0 -48
  46. package/dist/js/node/operators/http.js +0 -241
  47. package/dist/js/node/router/constants.js +0 -61
  48. package/dist/js/node/router/index.js +0 -256
  49. package/dist/js/node/router/types.js +0 -15
  50. package/dist/js/node/router/utils.js +0 -116
  51. package/dist/js/node/types.js +0 -73
  52. package/dist/js/node/utils/alias.js +0 -107
  53. package/dist/js/node/utils/debug.js +0 -28
  54. package/dist/js/node/utils/index.js +0 -32
  55. package/dist/js/node/utils/meta.js +0 -32
  56. package/dist/js/node/utils/storage.js +0 -71
  57. package/dist/js/node/utils/validate.js +0 -74
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,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
  ));
@@ -34,14 +38,12 @@ const DEFAULT_CLIENT_REQUEST_CREATOR = "@modern-js/create-request";
34
38
  const generateClient = async ({
35
39
  resourcePath,
36
40
  apiDir,
37
- lambdaDir,
38
41
  prefix,
39
42
  port,
40
43
  target,
41
44
  requestCreator,
42
45
  fetcher,
43
- requireResolve = require.resolve,
44
- httpMethodDecider
46
+ requireResolve = require.resolve
45
47
  }) => {
46
48
  if (!requestCreator) {
47
49
  requestCreator = requireResolve(
@@ -60,9 +62,7 @@ const generateClient = async ({
60
62
  }
61
63
  const apiRouter = new import_router.ApiRouter({
62
64
  apiDir,
63
- lambdaDir,
64
- prefix,
65
- httpMethodDecider
65
+ prefix
66
66
  });
67
67
  const handlerInfos = apiRouter.getSingleModuleHandlers(resourcePath);
68
68
  if (!handlerInfos) {
@@ -77,7 +77,7 @@ const generateClient = async ({
77
77
  }
78
78
  const upperHttpMethod = httpMethod.toUpperCase();
79
79
  const routeName = routePath;
80
- handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', '${httpMethodDecider}', ${process.env.PORT || String(port)}${fetcher ? `, fetch` : ""});
80
+ handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', ${process.env.PORT || String(port)}${fetcher ? `, fetch` : ""});
81
81
  `;
82
82
  }
83
83
  const importCode = `import { createRequest } from '${requestCreator}';
package/dist/cjs/index.js CHANGED
@@ -21,11 +21,9 @@ __export(src_exports, {
21
21
  Api: () => import_api.Api,
22
22
  HANDLER_WITH_META: () => import_utils.HANDLER_WITH_META,
23
23
  HttpError: () => import_http.HttpError,
24
- INPUT_PARAMS_DECIDER: () => import_utils.INPUT_PARAMS_DECIDER,
25
24
  ValidationError: () => import_http.ValidationError,
26
25
  createStorage: () => import_utils.createStorage,
27
26
  getRelativeRuntimePath: () => import_utils.getRelativeRuntimePath,
28
- isInputParamsDeciderHandler: () => import_utils.isInputParamsDeciderHandler,
29
27
  isWithMetaHandler: () => import_utils.isWithMetaHandler,
30
28
  registerPaths: () => import_utils.registerPaths
31
29
  });
@@ -42,11 +40,9 @@ var import_utils = require("./utils");
42
40
  Api,
43
41
  HANDLER_WITH_META,
44
42
  HttpError,
45
- INPUT_PARAMS_DECIDER,
46
43
  ValidationError,
47
44
  createStorage,
48
45
  getRelativeRuntimePath,
49
- isInputParamsDeciderHandler,
50
46
  isWithMetaHandler,
51
47
  registerPaths
52
48
  });
@@ -1,8 +1,6 @@
1
- var __create = Object.create;
2
1
  var __defProp = Object.defineProperty;
3
2
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
3
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
4
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
5
  var __export = (target, all) => {
8
6
  for (var name in all)
@@ -16,10 +14,6 @@ var __copyProps = (to, from, except, desc) => {
16
14
  }
17
15
  return to;
18
16
  };
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
17
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
18
  var http_exports = {};
25
19
  __export(http_exports, {
@@ -48,7 +42,7 @@ const validateInput = async (schema, input) => {
48
42
  try {
49
43
  return await schema.parseAsync(input);
50
44
  } catch (error) {
51
- const { z: zod } = await Promise.resolve().then(() => __toESM(require("zod")));
45
+ const { z: zod } = require("zod");
52
46
  if (error instanceof zod.ZodError) {
53
47
  throw new import_http.ValidationError(400, error.message);
54
48
  }
@@ -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
  ));
@@ -40,8 +44,7 @@ class ApiRouter {
40
44
  constructor({
41
45
  apiDir,
42
46
  lambdaDir,
43
- prefix,
44
- httpMethodDecider = "functionName"
47
+ prefix
45
48
  }) {
46
49
  this.apiFiles = [];
47
50
  this.getExactApiMode = (apiDir) => {
@@ -67,7 +70,6 @@ class ApiRouter {
67
70
  this.prefix = this.initPrefix(prefix);
68
71
  this.apiDir = apiDir;
69
72
  this.apiMode = this.getExactApiMode(apiDir);
70
- this.httpMethodDecider = httpMethodDecider;
71
73
  this.lambdaDir = lambdaDir || this.getExactLambdaDir(this.apiDir);
72
74
  this.existLambdaDir = import_utils.fs.existsSync(this.lambdaDir);
73
75
  }
@@ -111,6 +113,7 @@ class ApiRouter {
111
113
  }
112
114
  return null;
113
115
  }
116
+ // TODO: 性能提升,开发环境,判断下 lambda 目录修改时间
114
117
  getSafeRoutePath(filename, handler) {
115
118
  this.loadApiFiles();
116
119
  this.validateValidApifile(filename);
@@ -128,14 +131,7 @@ class ApiRouter {
128
131
  return trigger.path;
129
132
  }
130
133
  }
131
- let routePath = (0, import_utils3.getPathFromFilename)(this.lambdaDir, filename);
132
- if (this.httpMethodDecider === "inputParams") {
133
- if (routePath.endsWith("/")) {
134
- routePath += `${handler == null ? void 0 : handler.name}`;
135
- } else {
136
- routePath += `/${handler == null ? void 0 : handler.name}`;
137
- }
138
- }
134
+ const routePath = (0, import_utils3.getPathFromFilename)(this.lambdaDir, filename);
139
135
  return routePath;
140
136
  }
141
137
  getHttpMethod(originHandlerName, handler) {
@@ -145,45 +141,33 @@ class ApiRouter {
145
141
  return trigger.method;
146
142
  }
147
143
  }
148
- if (this.httpMethodDecider === "functionName") {
149
- const upperName = originHandlerName.toUpperCase();
150
- switch (upperName) {
151
- case "GET":
152
- return import_types.HttpMethod.Get;
153
- case "POST":
154
- return import_types.HttpMethod.Post;
155
- case "PUT":
156
- return import_types.HttpMethod.Put;
157
- case "DELETE":
158
- case "DEL":
159
- return import_types.HttpMethod.Delete;
160
- case "CONNECT":
161
- return import_types.HttpMethod.Connect;
162
- case "TRACE":
163
- return import_types.HttpMethod.Trace;
164
- case "PATCH":
165
- return import_types.HttpMethod.Patch;
166
- case "OPTION":
167
- return import_types.HttpMethod.Option;
168
- case "DEFAULT": {
169
- return import_types.HttpMethod.Get;
170
- }
171
- default:
172
- if (process.env.NODE_ENV !== "test") {
173
- import_utils.logger.warn(
174
- `Only api handlers are allowd to be exported, please remove the function ${originHandlerName} from exports`
175
- );
176
- }
177
- return null;
178
- }
179
- } else {
180
- if (!handler) {
181
- return null;
182
- }
183
- if (typeof handler === "function" && handler.length > 0) {
144
+ const upperName = originHandlerName.toUpperCase();
145
+ switch (upperName) {
146
+ case "GET":
147
+ return import_types.HttpMethod.Get;
148
+ case "POST":
184
149
  return import_types.HttpMethod.Post;
150
+ case "PUT":
151
+ return import_types.HttpMethod.Put;
152
+ case "DELETE":
153
+ case "DEL":
154
+ return import_types.HttpMethod.Delete;
155
+ case "CONNECT":
156
+ return import_types.HttpMethod.Connect;
157
+ case "TRACE":
158
+ return import_types.HttpMethod.Trace;
159
+ case "PATCH":
160
+ return import_types.HttpMethod.Patch;
161
+ case "OPTION":
162
+ return import_types.HttpMethod.Option;
163
+ case "DEFAULT": {
164
+ return import_types.HttpMethod.Get;
185
165
  }
186
- return import_types.HttpMethod.Get;
166
+ default:
167
+ import_utils.logger.warn(
168
+ `Only api handlers are allowd to be exported, please remove the function ${originHandlerName} from exports`
169
+ );
170
+ return null;
187
171
  }
188
172
  }
189
173
  loadApiFiles() {
@@ -209,6 +193,10 @@ class ApiRouter {
209
193
  (0, import_utils2.debug)("apiHandlers", apiHandlers.length, apiHandlers);
210
194
  return apiHandlers;
211
195
  }
196
+ /**
197
+ * 如果用户未传入或传入空串,默认为 /api
198
+ * 如果传入 /,则 prefix 为 /
199
+ */
212
200
  initPrefix(prefix) {
213
201
  if (prefix === "/") {
214
202
  return "";
@@ -252,14 +240,8 @@ class ApiRouter {
252
240
  }
253
241
  getModuleHandlerInfos(moduleInfo) {
254
242
  const { module: module2, filename } = moduleInfo;
255
- const { httpMethodDecider } = this;
256
243
  return Object.entries(module2).filter(([, handler]) => typeof handler === "function").map(([key]) => {
257
244
  const handler = module2[key];
258
- if (httpMethodDecider === "inputParams") {
259
- Object.assign(handler, {
260
- [import_utils2.INPUT_PARAMS_DECIDER]: true
261
- });
262
- }
263
245
  const handlerInfo = this.getHandlerInfo(filename, key, handler);
264
246
  return handlerInfo;
265
247
  }).filter((handlerInfo) => Boolean(handlerInfo));
@@ -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
  ));
@@ -18,23 +18,15 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
18
18
  var meta_exports = {};
19
19
  __export(meta_exports, {
20
20
  HANDLER_WITH_META: () => HANDLER_WITH_META,
21
- INPUT_PARAMS_DECIDER: () => INPUT_PARAMS_DECIDER,
22
- isInputParamsDeciderHandler: () => isInputParamsDeciderHandler,
23
21
  isWithMetaHandler: () => isWithMetaHandler
24
22
  });
25
23
  module.exports = __toCommonJS(meta_exports);
26
24
  const HANDLER_WITH_META = "HANDLER_WITH_META";
27
- const INPUT_PARAMS_DECIDER = "INPUT_PARAMS_DECIDER";
28
25
  const isWithMetaHandler = (handler) => {
29
26
  return typeof handler === "function" && handler[HANDLER_WITH_META];
30
27
  };
31
- const isInputParamsDeciderHandler = (handler) => {
32
- return typeof handler === "function" && handler[INPUT_PARAMS_DECIDER];
33
- };
34
28
  // Annotate the CommonJS export names for ESM import in node:
35
29
  0 && (module.exports = {
36
30
  HANDLER_WITH_META,
37
- INPUT_PARAMS_DECIDER,
38
- isInputParamsDeciderHandler,
39
31
  isWithMetaHandler
40
32
  });
@@ -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
  ));
@@ -5,14 +5,12 @@ const DEFAULT_CLIENT_REQUEST_CREATOR = "@modern-js/create-request";
5
5
  const generateClient = async ({
6
6
  resourcePath,
7
7
  apiDir,
8
- lambdaDir,
9
8
  prefix,
10
9
  port,
11
10
  target,
12
11
  requestCreator,
13
12
  fetcher,
14
- requireResolve = require.resolve,
15
- httpMethodDecider
13
+ requireResolve = require.resolve
16
14
  }) => {
17
15
  if (!requestCreator) {
18
16
  requestCreator = requireResolve(
@@ -31,9 +29,7 @@ const generateClient = async ({
31
29
  }
32
30
  const apiRouter = new ApiRouter({
33
31
  apiDir,
34
- lambdaDir,
35
- prefix,
36
- httpMethodDecider
32
+ prefix
37
33
  });
38
34
  const handlerInfos = apiRouter.getSingleModuleHandlers(resourcePath);
39
35
  if (!handlerInfos) {
@@ -48,7 +44,7 @@ const generateClient = async ({
48
44
  }
49
45
  const upperHttpMethod = httpMethod.toUpperCase();
50
46
  const routeName = routePath;
51
- handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', '${httpMethodDecider}', ${process.env.PORT || String(port)}${fetcher ? `, fetch` : ""});
47
+ handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', ${process.env.PORT || String(port)}${fetcher ? `, fetch` : ""});
52
48
  `;
53
49
  }
54
50
  const importCode = `import { createRequest } from '${requestCreator}';
package/dist/esm/index.js CHANGED
@@ -8,8 +8,6 @@ import {
8
8
  getRelativeRuntimePath,
9
9
  HANDLER_WITH_META,
10
10
  isWithMetaHandler,
11
- INPUT_PARAMS_DECIDER,
12
- isInputParamsDeciderHandler,
13
11
  createStorage,
14
12
  registerPaths
15
13
  } from "./utils";
@@ -17,11 +15,9 @@ export {
17
15
  Api,
18
16
  HANDLER_WITH_META,
19
17
  HttpError,
20
- INPUT_PARAMS_DECIDER,
21
18
  ValidationError,
22
19
  createStorage,
23
20
  getRelativeRuntimePath,
24
- isInputParamsDeciderHandler,
25
21
  isWithMetaHandler,
26
22
  registerPaths
27
23
  };
@@ -10,7 +10,7 @@ const validateInput = async (schema, input) => {
10
10
  try {
11
11
  return await schema.parseAsync(input);
12
12
  } catch (error) {
13
- const { z: zod } = await import("zod");
13
+ const { z: zod } = require("zod");
14
14
  if (error instanceof zod.ZodError) {
15
15
  throw new ValidationError(400, error.message);
16
16
  }
@@ -2,7 +2,7 @@ import path from "path";
2
2
  import { fs, logger } from "@modern-js/utils";
3
3
  import "reflect-metadata";
4
4
  import { HttpMethod, httpMethods, OperatorType, TriggerType } from "../types";
5
- import { debug, INPUT_PARAMS_DECIDER } from "../utils";
5
+ import { debug } from "../utils";
6
6
  import {
7
7
  APIMode,
8
8
  FRAMEWORK_MODE_LAMBDA_DIR,
@@ -21,8 +21,7 @@ class ApiRouter {
21
21
  constructor({
22
22
  apiDir,
23
23
  lambdaDir,
24
- prefix,
25
- httpMethodDecider = "functionName"
24
+ prefix
26
25
  }) {
27
26
  this.apiFiles = [];
28
27
  this.getExactApiMode = (apiDir) => {
@@ -48,7 +47,6 @@ class ApiRouter {
48
47
  this.prefix = this.initPrefix(prefix);
49
48
  this.apiDir = apiDir;
50
49
  this.apiMode = this.getExactApiMode(apiDir);
51
- this.httpMethodDecider = httpMethodDecider;
52
50
  this.lambdaDir = lambdaDir || this.getExactLambdaDir(this.apiDir);
53
51
  this.existLambdaDir = fs.existsSync(this.lambdaDir);
54
52
  }
@@ -92,6 +90,7 @@ class ApiRouter {
92
90
  }
93
91
  return null;
94
92
  }
93
+ // TODO: 性能提升,开发环境,判断下 lambda 目录修改时间
95
94
  getSafeRoutePath(filename, handler) {
96
95
  this.loadApiFiles();
97
96
  this.validateValidApifile(filename);
@@ -109,14 +108,7 @@ class ApiRouter {
109
108
  return trigger.path;
110
109
  }
111
110
  }
112
- let routePath = getPathFromFilename(this.lambdaDir, filename);
113
- if (this.httpMethodDecider === "inputParams") {
114
- if (routePath.endsWith("/")) {
115
- routePath += `${handler == null ? void 0 : handler.name}`;
116
- } else {
117
- routePath += `/${handler == null ? void 0 : handler.name}`;
118
- }
119
- }
111
+ const routePath = getPathFromFilename(this.lambdaDir, filename);
120
112
  return routePath;
121
113
  }
122
114
  getHttpMethod(originHandlerName, handler) {
@@ -126,45 +118,33 @@ class ApiRouter {
126
118
  return trigger.method;
127
119
  }
128
120
  }
129
- if (this.httpMethodDecider === "functionName") {
130
- const upperName = originHandlerName.toUpperCase();
131
- switch (upperName) {
132
- case "GET":
133
- return HttpMethod.Get;
134
- case "POST":
135
- return HttpMethod.Post;
136
- case "PUT":
137
- return HttpMethod.Put;
138
- case "DELETE":
139
- case "DEL":
140
- return HttpMethod.Delete;
141
- case "CONNECT":
142
- return HttpMethod.Connect;
143
- case "TRACE":
144
- return HttpMethod.Trace;
145
- case "PATCH":
146
- return HttpMethod.Patch;
147
- case "OPTION":
148
- return HttpMethod.Option;
149
- case "DEFAULT": {
150
- return HttpMethod.Get;
151
- }
152
- default:
153
- if (process.env.NODE_ENV !== "test") {
154
- logger.warn(
155
- `Only api handlers are allowd to be exported, please remove the function ${originHandlerName} from exports`
156
- );
157
- }
158
- return null;
159
- }
160
- } else {
161
- if (!handler) {
162
- return null;
163
- }
164
- if (typeof handler === "function" && handler.length > 0) {
121
+ const upperName = originHandlerName.toUpperCase();
122
+ switch (upperName) {
123
+ case "GET":
124
+ return HttpMethod.Get;
125
+ case "POST":
165
126
  return HttpMethod.Post;
127
+ case "PUT":
128
+ return HttpMethod.Put;
129
+ case "DELETE":
130
+ case "DEL":
131
+ return HttpMethod.Delete;
132
+ case "CONNECT":
133
+ return HttpMethod.Connect;
134
+ case "TRACE":
135
+ return HttpMethod.Trace;
136
+ case "PATCH":
137
+ return HttpMethod.Patch;
138
+ case "OPTION":
139
+ return HttpMethod.Option;
140
+ case "DEFAULT": {
141
+ return HttpMethod.Get;
166
142
  }
167
- return HttpMethod.Get;
143
+ default:
144
+ logger.warn(
145
+ `Only api handlers are allowd to be exported, please remove the function ${originHandlerName} from exports`
146
+ );
147
+ return null;
168
148
  }
169
149
  }
170
150
  loadApiFiles() {
@@ -190,6 +170,10 @@ class ApiRouter {
190
170
  debug("apiHandlers", apiHandlers.length, apiHandlers);
191
171
  return apiHandlers;
192
172
  }
173
+ /**
174
+ * 如果用户未传入或传入空串,默认为 /api
175
+ * 如果传入 /,则 prefix 为 /
176
+ */
193
177
  initPrefix(prefix) {
194
178
  if (prefix === "/") {
195
179
  return "";
@@ -233,14 +217,8 @@ class ApiRouter {
233
217
  }
234
218
  getModuleHandlerInfos(moduleInfo) {
235
219
  const { module, filename } = moduleInfo;
236
- const { httpMethodDecider } = this;
237
220
  return Object.entries(module).filter(([, handler]) => typeof handler === "function").map(([key]) => {
238
221
  const handler = module[key];
239
- if (httpMethodDecider === "inputParams") {
240
- Object.assign(handler, {
241
- [INPUT_PARAMS_DECIDER]: true
242
- });
243
- }
244
222
  const handlerInfo = this.getHandlerInfo(filename, key, handler);
245
223
  return handlerInfo;
246
224
  }).filter((handlerInfo) => Boolean(handlerInfo));
@@ -1,14 +1,8 @@
1
1
  const HANDLER_WITH_META = "HANDLER_WITH_META";
2
- const INPUT_PARAMS_DECIDER = "INPUT_PARAMS_DECIDER";
3
2
  const isWithMetaHandler = (handler) => {
4
3
  return typeof handler === "function" && handler[HANDLER_WITH_META];
5
4
  };
6
- const isInputParamsDeciderHandler = (handler) => {
7
- return typeof handler === "function" && handler[INPUT_PARAMS_DECIDER];
8
- };
9
5
  export {
10
6
  HANDLER_WITH_META,
11
- INPUT_PARAMS_DECIDER,
12
- isInputParamsDeciderHandler,
13
7
  isWithMetaHandler
14
8
  };
@@ -1,29 +1,24 @@
1
- import type { HttpMethodDecider } from '@modern-js/types';
2
1
  import { Result } from './result';
3
2
  export type GenClientResult = Result<string>;
4
3
  export type GenClientOptions = {
5
4
  resourcePath: string;
6
5
  source: string;
7
6
  apiDir: string;
8
- lambdaDir: string;
9
7
  prefix: string;
10
8
  port: number;
11
9
  requestCreator?: string;
12
10
  fetcher?: string;
13
11
  target?: string;
14
12
  requireResolve?: typeof require.resolve;
15
- httpMethodDecider: HttpMethodDecider;
16
13
  };
17
14
  export declare const DEFAULT_CLIENT_REQUEST_CREATOR = "@modern-js/create-request";
18
15
  export declare const generateClient: ({
19
16
  resourcePath,
20
17
  apiDir,
21
- lambdaDir,
22
18
  prefix,
23
19
  port,
24
20
  target,
25
21
  requestCreator,
26
22
  fetcher,
27
- requireResolve,
28
- httpMethodDecider
23
+ requireResolve
29
24
  }: GenClientOptions) => Promise<GenClientResult>;
@@ -4,4 +4,4 @@ export * from './router';
4
4
  export * from './types';
5
5
  export * from './client';
6
6
  export * from './operators/http';
7
- export { getRelativeRuntimePath, HANDLER_WITH_META, isWithMetaHandler, INPUT_PARAMS_DECIDER, isInputParamsDeciderHandler, createStorage, registerPaths } from './utils';
7
+ export { getRelativeRuntimePath, HANDLER_WITH_META, isWithMetaHandler, createStorage, registerPaths } from './utils';
@@ -1,5 +1,4 @@
1
1
  import 'reflect-metadata';
2
- import type { HttpMethodDecider } from '@modern-js/types';
3
2
  import { HttpMethod } from '../types';
4
3
  import { APIMode } from './constants';
5
4
  import { ApiHandler, APIHandlerInfo } from './types';
@@ -9,20 +8,17 @@ export declare class ApiRouter {
9
8
  private apiMode;
10
9
  private apiDir;
11
10
  private existLambdaDir;
12
- private httpMethodDecider;
13
11
  private lambdaDir;
14
12
  private prefix;
15
13
  private apiFiles;
16
14
  constructor({
17
15
  apiDir,
18
16
  lambdaDir,
19
- prefix,
20
- httpMethodDecider
17
+ prefix
21
18
  }: {
22
19
  apiDir: string;
23
20
  lambdaDir?: string;
24
21
  prefix?: string;
25
- httpMethodDecider?: HttpMethodDecider;
26
22
  });
27
23
  isExistLambda(): boolean;
28
24
  getApiMode(): APIMode;
@@ -1,4 +1,2 @@
1
1
  export declare const HANDLER_WITH_META = "HANDLER_WITH_META";
2
- export declare const INPUT_PARAMS_DECIDER = "INPUT_PARAMS_DECIDER";
3
- export declare const isWithMetaHandler: (handler: any) => any;
4
- export declare const isInputParamsDeciderHandler: (handler: any) => any;
2
+ export declare const isWithMetaHandler: (handler: any) => any;