@modern-js/bff-core 2.6.0 → 2.6.1-alpha.1

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.
@@ -25,12 +25,12 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
25
25
  mod
26
26
  ));
27
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
- var generate_client_exports = {};
29
- __export(generate_client_exports, {
28
+ var generateClient_exports = {};
29
+ __export(generateClient_exports, {
30
30
  DEFAULT_CLIENT_REQUEST_CREATOR: () => DEFAULT_CLIENT_REQUEST_CREATOR,
31
31
  generateClient: () => generateClient
32
32
  });
33
- module.exports = __toCommonJS(generate_client_exports);
33
+ module.exports = __toCommonJS(generateClient_exports);
34
34
  var path = __toESM(require("path"));
35
35
  var import_router = require("../router");
36
36
  var import_result = require("./result");
@@ -38,12 +38,14 @@ const DEFAULT_CLIENT_REQUEST_CREATOR = "@modern-js/create-request";
38
38
  const generateClient = async ({
39
39
  resourcePath,
40
40
  apiDir,
41
+ lambdaDir,
41
42
  prefix,
42
43
  port,
43
44
  target,
44
45
  requestCreator,
45
46
  fetcher,
46
- requireResolve = require.resolve
47
+ requireResolve = require.resolve,
48
+ httpMethodDecider
47
49
  }) => {
48
50
  if (!requestCreator) {
49
51
  requestCreator = requireResolve(
@@ -62,7 +64,9 @@ const generateClient = async ({
62
64
  }
63
65
  const apiRouter = new import_router.ApiRouter({
64
66
  apiDir,
65
- prefix
67
+ lambdaDir,
68
+ prefix,
69
+ httpMethodDecider
66
70
  });
67
71
  const handlerInfos = apiRouter.getSingleModuleHandlers(resourcePath);
68
72
  if (!handlerInfos) {
@@ -77,8 +81,17 @@ 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)}${fetcher ? `, fetch` : ""});
81
- `;
84
+ if (target === "client") {
85
+ handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', ${String(
86
+ port
87
+ )}, '${httpMethodDecider ? httpMethodDecider : "functionName"}' ${fetcher ? `, fetch` : ""});
88
+ `;
89
+ } else {
90
+ handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', process.env.PORT || ${String(
91
+ port
92
+ )}, '${httpMethodDecider ? httpMethodDecider : "functionName"}' ${fetcher ? `, fetch` : ""});
93
+ `;
94
+ }
82
95
  }
83
96
  const importCode = `import { createRequest } from '${requestCreator}';
84
97
  ${fetcher ? `import { fetch } from '${fetcher}';
@@ -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);
package/dist/cjs/index.js CHANGED
@@ -21,9 +21,11 @@ __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,
24
25
  ValidationError: () => import_http.ValidationError,
25
26
  createStorage: () => import_utils.createStorage,
26
27
  getRelativeRuntimePath: () => import_utils.getRelativeRuntimePath,
28
+ isInputParamsDeciderHandler: () => import_utils.isInputParamsDeciderHandler,
27
29
  isWithMetaHandler: () => import_utils.isWithMetaHandler,
28
30
  registerPaths: () => import_utils.registerPaths
29
31
  });
@@ -40,9 +42,11 @@ var import_utils = require("./utils");
40
42
  Api,
41
43
  HANDLER_WITH_META,
42
44
  HttpError,
45
+ INPUT_PARAMS_DECIDER,
43
46
  ValidationError,
44
47
  createStorage,
45
48
  getRelativeRuntimePath,
49
+ isInputParamsDeciderHandler,
46
50
  isWithMetaHandler,
47
51
  registerPaths
48
52
  });
@@ -1,6 +1,8 @@
1
+ var __create = Object.create;
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
4
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
7
  var __export = (target, all) => {
6
8
  for (var name in all)
@@ -14,6 +16,14 @@ var __copyProps = (to, from, except, desc) => {
14
16
  }
15
17
  return to;
16
18
  };
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.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
17
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
28
  var http_exports = {};
19
29
  __export(http_exports, {
@@ -42,7 +52,7 @@ const validateInput = async (schema, input) => {
42
52
  try {
43
53
  return await schema.parseAsync(input);
44
54
  } catch (error) {
45
- const { z: zod } = require("zod");
55
+ const { z: zod } = await Promise.resolve().then(() => __toESM(require("zod")));
46
56
  if (error instanceof zod.ZodError) {
47
57
  throw new import_http.ValidationError(400, error.message);
48
58
  }
@@ -44,12 +44,13 @@ class ApiRouter {
44
44
  constructor({
45
45
  apiDir,
46
46
  lambdaDir,
47
- prefix
47
+ prefix,
48
+ httpMethodDecider = "functionName"
48
49
  }) {
49
50
  this.apiFiles = [];
50
- this.getExactApiMode = (apiDir) => {
51
+ this.getExactApiMode = (apiDir, lambdaDir) => {
51
52
  const exist = this.createExistChecker(apiDir);
52
- 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);
53
54
  const existAppDir = exist(import_constants.FRAMEWORK_MODE_APP_DIR);
54
55
  const existAppFile = exist("app.ts") || exist("app.js");
55
56
  if (existLambdaDir || existAppDir || existAppFile) {
@@ -58,20 +59,21 @@ class ApiRouter {
58
59
  return import_constants.APIMode.FUNCTION;
59
60
  };
60
61
  this.createExistChecker = (base) => (target) => import_utils.fs.pathExistsSync(import_path.default.resolve(base, target));
61
- this.getExactLambdaDir = (apiDir) => {
62
- if (this.lambdaDir) {
63
- return this.lambdaDir;
62
+ this.getExactLambdaDir = (apiDir, originLambdaDir) => {
63
+ if (this.apiMode === import_constants.APIMode.FUNCTION) {
64
+ return apiDir;
64
65
  }
65
- const lambdaDir = this.apiMode === import_constants.APIMode.FARMEWORK ? import_path.default.join(apiDir, import_constants.FRAMEWORK_MODE_LAMBDA_DIR) : apiDir;
66
- return lambdaDir;
66
+ return originLambdaDir || import_path.default.join(apiDir, import_constants.FRAMEWORK_MODE_LAMBDA_DIR);
67
67
  };
68
68
  this.validateAbsolute(apiDir, "apiDir");
69
69
  this.validateAbsolute(lambdaDir, "lambdaDir");
70
70
  this.prefix = this.initPrefix(prefix);
71
71
  this.apiDir = apiDir;
72
- this.apiMode = this.getExactApiMode(apiDir);
73
- this.lambdaDir = lambdaDir || this.getExactLambdaDir(this.apiDir);
72
+ this.httpMethodDecider = httpMethodDecider;
73
+ this.apiMode = this.getExactApiMode(apiDir, lambdaDir);
74
+ this.lambdaDir = this.getExactLambdaDir(this.apiDir, lambdaDir);
74
75
  this.existLambdaDir = import_utils.fs.existsSync(this.lambdaDir);
76
+ (0, import_utils2.debug)(`apiDir:`, this.apiDir, `lambdaDir:`, this.lambdaDir);
75
77
  }
76
78
  isExistLambda() {
77
79
  return this.existLambdaDir;
@@ -83,13 +85,10 @@ class ApiRouter {
83
85
  return this.lambdaDir;
84
86
  }
85
87
  isApiFile(filename) {
86
- if (this.existLambdaDir) {
87
- return false;
88
+ if (this.apiFiles.includes(filename)) {
89
+ return true;
88
90
  }
89
- if (!this.apiFiles.includes(filename)) {
90
- return false;
91
- }
92
- return true;
91
+ return false;
93
92
  }
94
93
  getSingleModuleHandlers(filename) {
95
94
  const moduleInfo = this.getModuleInfo(filename);
@@ -131,7 +130,14 @@ class ApiRouter {
131
130
  return trigger.path;
132
131
  }
133
132
  }
134
- const routePath = (0, import_utils3.getPathFromFilename)(this.lambdaDir, filename);
133
+ let routePath = (0, import_utils3.getPathFromFilename)(this.lambdaDir, filename);
134
+ if (this.httpMethodDecider === "inputParams") {
135
+ if (routePath.endsWith("/")) {
136
+ routePath += `${handler == null ? void 0 : handler.name}`;
137
+ } else {
138
+ routePath += `/${handler == null ? void 0 : handler.name}`;
139
+ }
140
+ }
135
141
  return routePath;
136
142
  }
137
143
  getHttpMethod(originHandlerName, handler) {
@@ -141,33 +147,45 @@ class ApiRouter {
141
147
  return trigger.method;
142
148
  }
143
149
  }
144
- const upperName = originHandlerName.toUpperCase();
145
- switch (upperName) {
146
- case "GET":
147
- return import_types.HttpMethod.Get;
148
- case "POST":
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;
150
+ if (this.httpMethodDecider === "functionName") {
151
+ const upperName = originHandlerName.toUpperCase();
152
+ switch (upperName) {
153
+ case "GET":
154
+ return import_types.HttpMethod.Get;
155
+ case "POST":
156
+ return import_types.HttpMethod.Post;
157
+ case "PUT":
158
+ return import_types.HttpMethod.Put;
159
+ case "DELETE":
160
+ case "DEL":
161
+ return import_types.HttpMethod.Delete;
162
+ case "CONNECT":
163
+ return import_types.HttpMethod.Connect;
164
+ case "TRACE":
165
+ return import_types.HttpMethod.Trace;
166
+ case "PATCH":
167
+ return import_types.HttpMethod.Patch;
168
+ case "OPTION":
169
+ return import_types.HttpMethod.Option;
170
+ case "DEFAULT": {
171
+ return import_types.HttpMethod.Get;
172
+ }
173
+ default:
174
+ if (process.env.NODE_ENV !== "test") {
175
+ import_utils.logger.warn(
176
+ `Only api handlers are allowd to be exported, please remove the function ${originHandlerName} from exports`
177
+ );
178
+ }
179
+ return null;
165
180
  }
166
- default:
167
- import_utils.logger.warn(
168
- `Only api handlers are allowd to be exported, please remove the function ${originHandlerName} from exports`
169
- );
181
+ } else {
182
+ if (!handler) {
170
183
  return null;
184
+ }
185
+ if (typeof handler === "function" && handler.length > 0) {
186
+ return import_types.HttpMethod.Post;
187
+ }
188
+ return import_types.HttpMethod.Get;
171
189
  }
172
190
  }
173
191
  loadApiFiles() {
@@ -240,8 +258,14 @@ class ApiRouter {
240
258
  }
241
259
  getModuleHandlerInfos(moduleInfo) {
242
260
  const { module: module2, filename } = moduleInfo;
261
+ const { httpMethodDecider } = this;
243
262
  return Object.entries(module2).filter(([, handler]) => typeof handler === "function").map(([key]) => {
244
263
  const handler = module2[key];
264
+ if (httpMethodDecider === "inputParams") {
265
+ Object.assign(handler, {
266
+ [import_utils2.INPUT_PARAMS_DECIDER]: true
267
+ });
268
+ }
245
269
  const handlerInfo = this.getHandlerInfo(filename, key, handler);
246
270
  return handlerInfo;
247
271
  }).filter((handlerInfo) => Boolean(handlerInfo));
@@ -18,15 +18,23 @@ 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,
21
23
  isWithMetaHandler: () => isWithMetaHandler
22
24
  });
23
25
  module.exports = __toCommonJS(meta_exports);
24
26
  const HANDLER_WITH_META = "HANDLER_WITH_META";
27
+ const INPUT_PARAMS_DECIDER = "INPUT_PARAMS_DECIDER";
25
28
  const isWithMetaHandler = (handler) => {
26
29
  return typeof handler === "function" && handler[HANDLER_WITH_META];
27
30
  };
31
+ const isInputParamsDeciderHandler = (handler) => {
32
+ return typeof handler === "function" && handler[INPUT_PARAMS_DECIDER];
33
+ };
28
34
  // Annotate the CommonJS export names for ESM import in node:
29
35
  0 && (module.exports = {
30
36
  HANDLER_WITH_META,
37
+ INPUT_PARAMS_DECIDER,
38
+ isInputParamsDeciderHandler,
31
39
  isWithMetaHandler
32
40
  });
@@ -5,12 +5,14 @@ const DEFAULT_CLIENT_REQUEST_CREATOR = "@modern-js/create-request";
5
5
  const generateClient = async ({
6
6
  resourcePath,
7
7
  apiDir,
8
+ lambdaDir,
8
9
  prefix,
9
10
  port,
10
11
  target,
11
12
  requestCreator,
12
13
  fetcher,
13
- requireResolve = require.resolve
14
+ requireResolve = require.resolve,
15
+ httpMethodDecider
14
16
  }) => {
15
17
  if (!requestCreator) {
16
18
  requestCreator = requireResolve(
@@ -29,7 +31,9 @@ const generateClient = async ({
29
31
  }
30
32
  const apiRouter = new ApiRouter({
31
33
  apiDir,
32
- prefix
34
+ lambdaDir,
35
+ prefix,
36
+ httpMethodDecider
33
37
  });
34
38
  const handlerInfos = apiRouter.getSingleModuleHandlers(resourcePath);
35
39
  if (!handlerInfos) {
@@ -44,8 +48,17 @@ const generateClient = async ({
44
48
  }
45
49
  const upperHttpMethod = httpMethod.toUpperCase();
46
50
  const routeName = routePath;
47
- handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', ${process.env.PORT || String(port)}${fetcher ? `, fetch` : ""});
48
- `;
51
+ if (target === "client") {
52
+ handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', ${String(
53
+ port
54
+ )}, '${httpMethodDecider ? httpMethodDecider : "functionName"}' ${fetcher ? `, fetch` : ""});
55
+ `;
56
+ } else {
57
+ handlersCode += `export ${exportStatement} createRequest('${routeName}', '${upperHttpMethod}', process.env.PORT || ${String(
58
+ port
59
+ )}, '${httpMethodDecider ? httpMethodDecider : "functionName"}' ${fetcher ? `, fetch` : ""});
60
+ `;
61
+ }
49
62
  }
50
63
  const importCode = `import { createRequest } from '${requestCreator}';
51
64
  ${fetcher ? `import { fetch } from '${fetcher}';
@@ -1 +1 @@
1
- export * from "./generate-client";
1
+ export * from "./generateClient";
package/dist/esm/index.js CHANGED
@@ -8,6 +8,8 @@ import {
8
8
  getRelativeRuntimePath,
9
9
  HANDLER_WITH_META,
10
10
  isWithMetaHandler,
11
+ INPUT_PARAMS_DECIDER,
12
+ isInputParamsDeciderHandler,
11
13
  createStorage,
12
14
  registerPaths
13
15
  } from "./utils";
@@ -15,9 +17,11 @@ export {
15
17
  Api,
16
18
  HANDLER_WITH_META,
17
19
  HttpError,
20
+ INPUT_PARAMS_DECIDER,
18
21
  ValidationError,
19
22
  createStorage,
20
23
  getRelativeRuntimePath,
24
+ isInputParamsDeciderHandler,
21
25
  isWithMetaHandler,
22
26
  registerPaths
23
27
  };
@@ -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 } = require("zod");
13
+ const { z: zod } = await import("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 } from "../utils";
5
+ import { debug, INPUT_PARAMS_DECIDER } from "../utils";
6
6
  import {
7
7
  APIMode,
8
8
  FRAMEWORK_MODE_LAMBDA_DIR,
@@ -21,12 +21,13 @@ class ApiRouter {
21
21
  constructor({
22
22
  apiDir,
23
23
  lambdaDir,
24
- prefix
24
+ prefix,
25
+ httpMethodDecider = "functionName"
25
26
  }) {
26
27
  this.apiFiles = [];
27
- this.getExactApiMode = (apiDir) => {
28
+ this.getExactApiMode = (apiDir, lambdaDir) => {
28
29
  const exist = this.createExistChecker(apiDir);
29
- const existLambdaDir = exist(FRAMEWORK_MODE_LAMBDA_DIR);
30
+ const existLambdaDir = lambdaDir && fs.pathExistsSync(lambdaDir) || exist(FRAMEWORK_MODE_LAMBDA_DIR);
30
31
  const existAppDir = exist(FRAMEWORK_MODE_APP_DIR);
31
32
  const existAppFile = exist("app.ts") || exist("app.js");
32
33
  if (existLambdaDir || existAppDir || existAppFile) {
@@ -35,20 +36,21 @@ class ApiRouter {
35
36
  return APIMode.FUNCTION;
36
37
  };
37
38
  this.createExistChecker = (base) => (target) => fs.pathExistsSync(path.resolve(base, target));
38
- this.getExactLambdaDir = (apiDir) => {
39
- if (this.lambdaDir) {
40
- return this.lambdaDir;
39
+ this.getExactLambdaDir = (apiDir, originLambdaDir) => {
40
+ if (this.apiMode === APIMode.FUNCTION) {
41
+ return apiDir;
41
42
  }
42
- const lambdaDir = this.apiMode === APIMode.FARMEWORK ? path.join(apiDir, FRAMEWORK_MODE_LAMBDA_DIR) : apiDir;
43
- return lambdaDir;
43
+ return originLambdaDir || path.join(apiDir, FRAMEWORK_MODE_LAMBDA_DIR);
44
44
  };
45
45
  this.validateAbsolute(apiDir, "apiDir");
46
46
  this.validateAbsolute(lambdaDir, "lambdaDir");
47
47
  this.prefix = this.initPrefix(prefix);
48
48
  this.apiDir = apiDir;
49
- this.apiMode = this.getExactApiMode(apiDir);
50
- this.lambdaDir = lambdaDir || this.getExactLambdaDir(this.apiDir);
49
+ this.httpMethodDecider = httpMethodDecider;
50
+ this.apiMode = this.getExactApiMode(apiDir, lambdaDir);
51
+ this.lambdaDir = this.getExactLambdaDir(this.apiDir, lambdaDir);
51
52
  this.existLambdaDir = fs.existsSync(this.lambdaDir);
53
+ debug(`apiDir:`, this.apiDir, `lambdaDir:`, this.lambdaDir);
52
54
  }
53
55
  isExistLambda() {
54
56
  return this.existLambdaDir;
@@ -60,13 +62,10 @@ class ApiRouter {
60
62
  return this.lambdaDir;
61
63
  }
62
64
  isApiFile(filename) {
63
- if (this.existLambdaDir) {
64
- return false;
65
+ if (this.apiFiles.includes(filename)) {
66
+ return true;
65
67
  }
66
- if (!this.apiFiles.includes(filename)) {
67
- return false;
68
- }
69
- return true;
68
+ return false;
70
69
  }
71
70
  getSingleModuleHandlers(filename) {
72
71
  const moduleInfo = this.getModuleInfo(filename);
@@ -108,7 +107,14 @@ class ApiRouter {
108
107
  return trigger.path;
109
108
  }
110
109
  }
111
- const routePath = getPathFromFilename(this.lambdaDir, filename);
110
+ let routePath = getPathFromFilename(this.lambdaDir, filename);
111
+ if (this.httpMethodDecider === "inputParams") {
112
+ if (routePath.endsWith("/")) {
113
+ routePath += `${handler == null ? void 0 : handler.name}`;
114
+ } else {
115
+ routePath += `/${handler == null ? void 0 : handler.name}`;
116
+ }
117
+ }
112
118
  return routePath;
113
119
  }
114
120
  getHttpMethod(originHandlerName, handler) {
@@ -118,33 +124,45 @@ class ApiRouter {
118
124
  return trigger.method;
119
125
  }
120
126
  }
121
- const upperName = originHandlerName.toUpperCase();
122
- switch (upperName) {
123
- case "GET":
124
- return HttpMethod.Get;
125
- case "POST":
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;
127
+ if (this.httpMethodDecider === "functionName") {
128
+ const upperName = originHandlerName.toUpperCase();
129
+ switch (upperName) {
130
+ case "GET":
131
+ return HttpMethod.Get;
132
+ case "POST":
133
+ return HttpMethod.Post;
134
+ case "PUT":
135
+ return HttpMethod.Put;
136
+ case "DELETE":
137
+ case "DEL":
138
+ return HttpMethod.Delete;
139
+ case "CONNECT":
140
+ return HttpMethod.Connect;
141
+ case "TRACE":
142
+ return HttpMethod.Trace;
143
+ case "PATCH":
144
+ return HttpMethod.Patch;
145
+ case "OPTION":
146
+ return HttpMethod.Option;
147
+ case "DEFAULT": {
148
+ return HttpMethod.Get;
149
+ }
150
+ default:
151
+ if (process.env.NODE_ENV !== "test") {
152
+ logger.warn(
153
+ `Only api handlers are allowd to be exported, please remove the function ${originHandlerName} from exports`
154
+ );
155
+ }
156
+ return null;
142
157
  }
143
- default:
144
- logger.warn(
145
- `Only api handlers are allowd to be exported, please remove the function ${originHandlerName} from exports`
146
- );
158
+ } else {
159
+ if (!handler) {
147
160
  return null;
161
+ }
162
+ if (typeof handler === "function" && handler.length > 0) {
163
+ return HttpMethod.Post;
164
+ }
165
+ return HttpMethod.Get;
148
166
  }
149
167
  }
150
168
  loadApiFiles() {
@@ -217,8 +235,14 @@ class ApiRouter {
217
235
  }
218
236
  getModuleHandlerInfos(moduleInfo) {
219
237
  const { module, filename } = moduleInfo;
238
+ const { httpMethodDecider } = this;
220
239
  return Object.entries(module).filter(([, handler]) => typeof handler === "function").map(([key]) => {
221
240
  const handler = module[key];
241
+ if (httpMethodDecider === "inputParams") {
242
+ Object.assign(handler, {
243
+ [INPUT_PARAMS_DECIDER]: true
244
+ });
245
+ }
222
246
  const handlerInfo = this.getHandlerInfo(filename, key, handler);
223
247
  return handlerInfo;
224
248
  }).filter((handlerInfo) => Boolean(handlerInfo));
@@ -1,8 +1,14 @@
1
1
  const HANDLER_WITH_META = "HANDLER_WITH_META";
2
+ const INPUT_PARAMS_DECIDER = "INPUT_PARAMS_DECIDER";
2
3
  const isWithMetaHandler = (handler) => {
3
4
  return typeof handler === "function" && handler[HANDLER_WITH_META];
4
5
  };
6
+ const isInputParamsDeciderHandler = (handler) => {
7
+ return typeof handler === "function" && handler[INPUT_PARAMS_DECIDER];
8
+ };
5
9
  export {
6
10
  HANDLER_WITH_META,
11
+ INPUT_PARAMS_DECIDER,
12
+ isInputParamsDeciderHandler,
7
13
  isWithMetaHandler
8
14
  };
@@ -1,24 +1,29 @@
1
+ import type { HttpMethodDecider } from '@modern-js/types';
1
2
  import { Result } from './result';
2
3
  export type GenClientResult = Result<string>;
3
4
  export type GenClientOptions = {
4
5
  resourcePath: string;
5
6
  source: string;
6
7
  apiDir: string;
8
+ lambdaDir: string;
7
9
  prefix: string;
8
10
  port: number;
9
11
  requestCreator?: string;
10
12
  fetcher?: string;
11
13
  target?: string;
12
14
  requireResolve?: typeof require.resolve;
15
+ httpMethodDecider?: HttpMethodDecider;
13
16
  };
14
17
  export declare const DEFAULT_CLIENT_REQUEST_CREATOR = "@modern-js/create-request";
15
18
  export declare const generateClient: ({
16
19
  resourcePath,
17
20
  apiDir,
21
+ lambdaDir,
18
22
  prefix,
19
23
  port,
20
24
  target,
21
25
  requestCreator,
22
26
  fetcher,
23
- requireResolve
27
+ requireResolve,
28
+ httpMethodDecider
24
29
  }: GenClientOptions) => Promise<GenClientResult>;
@@ -1 +1 @@
1
- export * from './generate-client';
1
+ export * from './generateClient';
@@ -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, createStorage, registerPaths } from './utils';
7
+ export { getRelativeRuntimePath, HANDLER_WITH_META, isWithMetaHandler, INPUT_PARAMS_DECIDER, isInputParamsDeciderHandler, createStorage, registerPaths } from './utils';
@@ -1,4 +1,5 @@
1
1
  import 'reflect-metadata';
2
+ import type { HttpMethodDecider } from '@modern-js/types';
2
3
  import { HttpMethod } from '../types';
3
4
  import { APIMode } from './constants';
4
5
  import { ApiHandler, APIHandlerInfo } from './types';
@@ -8,17 +9,20 @@ export declare class ApiRouter {
8
9
  private apiMode;
9
10
  private apiDir;
10
11
  private existLambdaDir;
12
+ private httpMethodDecider;
11
13
  private lambdaDir;
12
14
  private prefix;
13
15
  private apiFiles;
14
16
  constructor({
15
17
  apiDir,
16
18
  lambdaDir,
17
- prefix
19
+ prefix,
20
+ httpMethodDecider
18
21
  }: {
19
22
  apiDir: string;
20
23
  lambdaDir?: string;
21
24
  prefix?: string;
25
+ httpMethodDecider?: HttpMethodDecider;
22
26
  });
23
27
  isExistLambda(): boolean;
24
28
  getApiMode(): APIMode;
@@ -1,2 +1,4 @@
1
1
  export declare const HANDLER_WITH_META = "HANDLER_WITH_META";
2
- export declare const isWithMetaHandler: (handler: any) => any;
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;
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "2.6.0",
14
+ "version": "2.6.1-alpha.1",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/cjs/index.js",
@@ -25,23 +25,24 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "@babel/runtime": "^7.18.0",
28
+ "@modern-js/bff-runtime": "2.6.0",
29
+ "@modern-js/utils": "2.6.0",
28
30
  "esbuild": "0.15.7",
29
31
  "esbuild-register": "^3.3.3",
30
32
  "koa-compose": "^4.1.0",
31
- "reflect-metadata": "^0.1.13",
32
- "@modern-js/bff-runtime": "2.6.0",
33
- "@modern-js/utils": "2.6.0"
33
+ "reflect-metadata": "^0.1.13"
34
34
  },
35
35
  "devDependencies": {
36
+ "@modern-js/types": "2.6.0",
37
+ "@scripts/build": "2.6.0",
38
+ "@scripts/jest-config": "2.6.0",
36
39
  "@types/jest": "^27",
37
40
  "@types/koa-compose": "^3.2.5",
38
41
  "@types/node": "^14",
39
42
  "jest": "^27",
40
43
  "type-fest": "2.15.0",
41
44
  "typescript": "^4",
42
- "zod": "^3.17.3",
43
- "@scripts/build": "2.6.0",
44
- "@scripts/jest-config": "2.6.0"
45
+ "zod": "^3.17.3"
45
46
  },
46
47
  "peerDependencies": {
47
48
  "zod": "^3.17.3"