@modern-js/bff-core 2.58.0 → 2.58.2

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.
@@ -54,7 +54,7 @@ const generateClient = async ({ appDir, resourcePath, apiDir, lambdaDir, prefix,
54
54
  prefix,
55
55
  httpMethodDecider
56
56
  });
57
- const handlerInfos = apiRouter.getSingleModuleHandlers(resourcePath);
57
+ const handlerInfos = await apiRouter.getSingleModuleHandlers(resourcePath);
58
58
  if (!handlerInfos) {
59
59
  return (0, import_result.Err)(`generate client error: Cannot require module ${resourcePath}`);
60
60
  }
@@ -53,7 +53,7 @@ const validateInput = async (schema, input) => {
53
53
  try {
54
54
  return await schema.parseAsync(input);
55
55
  } catch (error) {
56
- const { z: zod } = await Promise.resolve().then(() => __toESM(require("zod")));
56
+ const { z: zod } = await import("zod");
57
57
  if (error instanceof zod.ZodError) {
58
58
  throw new import_http.ValidationError(400, error.message);
59
59
  }
@@ -57,8 +57,8 @@ class ApiRouter {
57
57
  }
58
58
  return false;
59
59
  }
60
- getSingleModuleHandlers(filename) {
61
- const moduleInfo = this.getModuleInfo(filename);
60
+ async getSingleModuleHandlers(filename) {
61
+ const moduleInfo = await this.getModuleInfo(filename);
62
62
  if (moduleInfo) {
63
63
  return this.getModuleHandlerInfos(moduleInfo);
64
64
  }
@@ -167,9 +167,9 @@ class ApiRouter {
167
167
  }
168
168
  return this.loadApiFiles();
169
169
  }
170
- getApiHandlers() {
170
+ async getApiHandlers() {
171
171
  const filenames = this.getApiFiles();
172
- const moduleInfos = this.getModuleInfos(filenames);
172
+ const moduleInfos = await this.getModuleInfos(filenames);
173
173
  const apiHandlers = this.getHandlerInfos(moduleInfos);
174
174
  (0, import_utils2.debug)("apiHandlers", apiHandlers.length, apiHandlers);
175
175
  return apiHandlers;
@@ -189,12 +189,12 @@ class ApiRouter {
189
189
  throw new Error(`The ${paramsName} ${filename} is not a abolute path`);
190
190
  }
191
191
  }
192
- getModuleInfos(filenames) {
193
- return filenames.map((filename) => this.getModuleInfo(filename)).filter((moduleInfo) => Boolean(moduleInfo));
192
+ async getModuleInfos(filenames) {
193
+ return Promise.all(filenames.map((filename) => this.getModuleInfo(filename)).filter((moduleInfo) => Boolean(moduleInfo)));
194
194
  }
195
- getModuleInfo(filename) {
195
+ async getModuleInfo(filename) {
196
196
  try {
197
- const module2 = (0, import_utils3.requireHandlerModule)(filename);
197
+ const module2 = await (0, import_utils3.requireHandlerModule)(filename);
198
198
  return {
199
199
  filename,
200
200
  module: module2
@@ -69,9 +69,9 @@ const clearRouteName = (routeName) => {
69
69
  };
70
70
  const isHandler = (input) => input && typeof input === "function";
71
71
  const isFunction = (input) => input && {}.toString.call(input) === "[object Function]";
72
- const requireHandlerModule = (modulePath) => {
73
- const originRequire = process.env.NODE_ENV === "test" ? jest.requireActual : require;
74
- const module2 = originRequire(modulePath);
72
+ const requireHandlerModule = async (modulePath) => {
73
+ const originRequire = process.env.NODE_ENV === "test" ? jest.requireActual : import_utils.compatibleRequire;
74
+ const module2 = await originRequire(modulePath, false);
75
75
  if (isFunction(module2)) {
76
76
  return {
77
77
  default: module2
@@ -20,7 +20,7 @@ const generateClient = async ({ appDir, resourcePath, apiDir, lambdaDir, prefix,
20
20
  prefix,
21
21
  httpMethodDecider
22
22
  });
23
- const handlerInfos = apiRouter.getSingleModuleHandlers(resourcePath);
23
+ const handlerInfos = await apiRouter.getSingleModuleHandlers(resourcePath);
24
24
  if (!handlerInfos) {
25
25
  return Err(`generate client error: Cannot require module ${resourcePath}`);
26
26
  }
@@ -23,8 +23,8 @@ class ApiRouter {
23
23
  }
24
24
  return false;
25
25
  }
26
- getSingleModuleHandlers(filename) {
27
- const moduleInfo = this.getModuleInfo(filename);
26
+ async getSingleModuleHandlers(filename) {
27
+ const moduleInfo = await this.getModuleInfo(filename);
28
28
  if (moduleInfo) {
29
29
  return this.getModuleHandlerInfos(moduleInfo);
30
30
  }
@@ -133,9 +133,9 @@ class ApiRouter {
133
133
  }
134
134
  return this.loadApiFiles();
135
135
  }
136
- getApiHandlers() {
136
+ async getApiHandlers() {
137
137
  const filenames = this.getApiFiles();
138
- const moduleInfos = this.getModuleInfos(filenames);
138
+ const moduleInfos = await this.getModuleInfos(filenames);
139
139
  const apiHandlers = this.getHandlerInfos(moduleInfos);
140
140
  debug("apiHandlers", apiHandlers.length, apiHandlers);
141
141
  return apiHandlers;
@@ -155,12 +155,12 @@ class ApiRouter {
155
155
  throw new Error(`The ${paramsName} ${filename} is not a abolute path`);
156
156
  }
157
157
  }
158
- getModuleInfos(filenames) {
159
- return filenames.map((filename) => this.getModuleInfo(filename)).filter((moduleInfo) => Boolean(moduleInfo));
158
+ async getModuleInfos(filenames) {
159
+ return Promise.all(filenames.map((filename) => this.getModuleInfo(filename)).filter((moduleInfo) => Boolean(moduleInfo)));
160
160
  }
161
- getModuleInfo(filename) {
161
+ async getModuleInfo(filename) {
162
162
  try {
163
- const module = requireHandlerModule(filename);
163
+ const module = await requireHandlerModule(filename);
164
164
  return {
165
165
  filename,
166
166
  module
@@ -1,5 +1,5 @@
1
1
  import path from "path";
2
- import { globby } from "@modern-js/utils";
2
+ import { globby, compatibleRequire } from "@modern-js/utils";
3
3
  import { INDEX_SUFFIX } from "./constants";
4
4
  const getFiles = (lambdaDir, rules) => globby.sync(rules, {
5
5
  cwd: lambdaDir,
@@ -32,9 +32,9 @@ const clearRouteName = (routeName) => {
32
32
  };
33
33
  const isHandler = (input) => input && typeof input === "function";
34
34
  const isFunction = (input) => input && {}.toString.call(input) === "[object Function]";
35
- const requireHandlerModule = (modulePath) => {
36
- const originRequire = process.env.NODE_ENV === "test" ? jest.requireActual : require;
37
- const module = originRequire(modulePath);
35
+ const requireHandlerModule = async (modulePath) => {
36
+ const originRequire = process.env.NODE_ENV === "test" ? jest.requireActual : compatibleRequire;
37
+ const module = await originRequire(modulePath, false);
38
38
  if (isFunction(module)) {
39
39
  return {
40
40
  default: module
@@ -1,5 +1,5 @@
1
1
  import type { HttpMethodDecider } from '@modern-js/types';
2
- import { Result } from './result';
2
+ import { type Result } from './result';
3
3
  export type GenClientResult = Result<string>;
4
4
  export type GenClientOptions = {
5
5
  resourcePath: string;
@@ -1,5 +1,5 @@
1
1
  import type { z } from 'zod';
2
- import { Operator, HttpMethod, ResponseMetaType } from '../types';
2
+ import { type Operator, HttpMethod, ResponseMetaType } from '../types';
3
3
  export interface ResponseMeta {
4
4
  type: ResponseMetaType;
5
5
  value: unknown;
@@ -2,7 +2,7 @@ import 'reflect-metadata';
2
2
  import type { HttpMethodDecider } from '@modern-js/types';
3
3
  import { HttpMethod } from '../types';
4
4
  import { APIMode } from './constants';
5
- import { ApiHandler, APIHandlerInfo } from './types';
5
+ import type { ApiHandler, APIHandlerInfo } from './types';
6
6
  export * from './types';
7
7
  export * from './constants';
8
8
  export declare class ApiRouter {
@@ -27,14 +27,14 @@ export declare class ApiRouter {
27
27
  getApiMode(): APIMode;
28
28
  getLambdaDir(): string;
29
29
  isApiFile(filename: string): boolean;
30
- getSingleModuleHandlers(filename: string): APIHandlerInfo[] | null;
30
+ getSingleModuleHandlers(filename: string): Promise<APIHandlerInfo[] | null>;
31
31
  getHandlerInfo(filename: string, originFuncName: string, handler: ApiHandler): APIHandlerInfo | null;
32
32
  getSafeRoutePath(filename: string, handler?: ApiHandler): string;
33
33
  getRouteName(filename: string, handler?: ApiHandler): string;
34
34
  getHttpMethod(originHandlerName: string, handler?: ApiHandler): HttpMethod | null;
35
35
  loadApiFiles(): string[];
36
36
  getApiFiles(): string[];
37
- getApiHandlers(): APIHandlerInfo[];
37
+ getApiHandlers(): Promise<APIHandlerInfo[]>;
38
38
  /**
39
39
  * 如果用户未传入或传入空串,默认为 /api
40
40
  * 如果传入 /,则 prefix 为 /
@@ -1,4 +1,4 @@
1
- import { HttpMethod } from '../types';
1
+ import type { HttpMethod } from '../types';
2
2
  export type ModuleInfo = {
3
3
  filename: string;
4
4
  module: HandlerModule;
@@ -1,10 +1,10 @@
1
- import { APIHandlerInfo } from './types';
1
+ import type { APIHandlerInfo } from './types';
2
2
  type MaybeAsync<I> = I | Promise<I>;
3
3
  export type NormalHandler = (...args: any[]) => any;
4
4
  export type Handler<I, O> = (input: I) => MaybeAsync<O>;
5
5
  export declare const getFiles: (lambdaDir: string, rules: string | string[]) => string[];
6
6
  export declare const getPathFromFilename: (baseDir: string, filename: string) => string;
7
7
  export declare const isHandler: (input: any) => input is Handler<any, any>;
8
- export declare const requireHandlerModule: (modulePath: string) => any;
8
+ export declare const requireHandlerModule: (modulePath: string) => Promise<any>;
9
9
  export declare const sortRoutes: (apiHandlers: APIHandlerInfo[]) => APIHandlerInfo[];
10
10
  export {};
@@ -1,4 +1,4 @@
1
- import { Merge } from 'type-fest';
1
+ import type { Merge } from 'type-fest';
2
2
  export declare enum OperatorType {
3
3
  Trigger = 0,
4
4
  Middleware = 1
@@ -1 +1 @@
1
- export declare const debug: import("@modern-js/utils/compiled/debug").Debugger;
1
+ export declare const debug: (...args: unknown[]) => void;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.58.0",
18
+ "version": "2.58.2",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
@@ -28,11 +28,11 @@
28
28
  }
29
29
  },
30
30
  "dependencies": {
31
+ "@swc/helpers": "0.5.3",
31
32
  "koa-compose": "^4.1.0",
32
33
  "reflect-metadata": "^0.1.13",
33
34
  "type-fest": "2.15.0",
34
- "@swc/helpers": "0.5.3",
35
- "@modern-js/utils": "2.58.0"
35
+ "@modern-js/utils": "2.58.2"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/jest": "^29",
@@ -43,14 +43,14 @@
43
43
  "tsconfig-paths": "^4.1.2",
44
44
  "typescript": "^5",
45
45
  "zod": "^3.22.3",
46
- "@scripts/build": "2.58.0",
47
- "@scripts/jest-config": "2.58.0",
48
- "@modern-js/types": "2.58.0"
46
+ "@modern-js/types": "2.58.2",
47
+ "@scripts/build": "2.58.2",
48
+ "@scripts/jest-config": "2.58.2"
49
49
  },
50
50
  "peerDependencies": {
51
- "zod": "^3.22.3",
52
51
  "ts-node": "^10.9.1",
53
- "tsconfig-paths": "^4.1.2"
52
+ "tsconfig-paths": "^4.1.2",
53
+ "zod": "^3.22.3"
54
54
  },
55
55
  "peerDependenciesMeta": {
56
56
  "zod": {