@modern-js/bff-core 2.41.0 → 2.42.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.
@@ -45,7 +45,7 @@ __reExport(router_exports, require("./constants"), module.exports);
45
45
  class ApiRouter {
46
46
  enableRegister() {
47
47
  const existTsLoader = Boolean(require.extensions[".ts"]);
48
- if (!existTsLoader && (process.env.NODE_ENV !== "production" || this.isBuild)) {
48
+ if (!existTsLoader) {
49
49
  try {
50
50
  const projectSearchDir = this.appDir || this.apiDir;
51
51
  const tsNode = require("ts-node");
@@ -223,6 +223,7 @@ class ApiRouter {
223
223
  return filenames.map((filename) => this.getModuleInfo(filename)).filter((moduleInfo) => Boolean(moduleInfo));
224
224
  }
225
225
  getModuleInfo(filename) {
226
+ this.enableRegister();
226
227
  try {
227
228
  const module2 = (0, import_utils3.requireHandlerModule)(filename);
228
229
  return {
@@ -313,7 +314,6 @@ class ApiRouter {
313
314
  this.lambdaDir = this.getExactLambdaDir(this.apiDir, lambdaDir);
314
315
  this.existLambdaDir = import_utils.fs.existsSync(this.lambdaDir);
315
316
  (0, import_utils2.debug)(`apiDir:`, this.apiDir, `lambdaDir:`, this.lambdaDir);
316
- this.enableRegister();
317
317
  }
318
318
  }
319
319
  // Annotate the CommonJS export names for ESM import in node:
@@ -11,7 +11,7 @@ export * from "./constants";
11
11
  class ApiRouter {
12
12
  enableRegister() {
13
13
  const existTsLoader = Boolean(require.extensions[".ts"]);
14
- if (!existTsLoader && (process.env.NODE_ENV !== "production" || this.isBuild)) {
14
+ if (!existTsLoader) {
15
15
  try {
16
16
  const projectSearchDir = this.appDir || this.apiDir;
17
17
  const tsNode = require("ts-node");
@@ -189,6 +189,7 @@ class ApiRouter {
189
189
  return filenames.map((filename) => this.getModuleInfo(filename)).filter((moduleInfo) => Boolean(moduleInfo));
190
190
  }
191
191
  getModuleInfo(filename) {
192
+ this.enableRegister();
192
193
  try {
193
194
  const module = requireHandlerModule(filename);
194
195
  return {
@@ -279,7 +280,6 @@ class ApiRouter {
279
280
  this.lambdaDir = this.getExactLambdaDir(this.apiDir, lambdaDir);
280
281
  this.existLambdaDir = fs.existsSync(this.lambdaDir);
281
282
  debug(`apiDir:`, this.apiDir, `lambdaDir:`, this.lambdaDir);
282
- this.enableRegister();
283
283
  }
284
284
  }
285
285
  export {
@@ -1,3 +1,6 @@
1
1
  import 'reflect-metadata';
2
2
  import type { ApiRunner, ArrayToObject, ExtractInputType, ExtractOuputType, Operator, MaybeAsync } from './types';
3
- export declare function Api<Operators extends Operator<any, any>[], Res extends MaybeAsync<any>>(...args: [...operators: Operators, handler: (arg: ArrayToObject<ExtractOuputType<Operators>>) => Res]): ApiRunner<ExtractInputType<Operators> extends void[] ? void : ArrayToObject<ExtractInputType<Operators>>, Res>;
3
+ export declare function Api<Operators extends Operator<any, any>[], Res extends MaybeAsync<any>>(...args: [
4
+ ...operators: Operators,
5
+ handler: (arg: ArrayToObject<ExtractOuputType<Operators>>) => Res
6
+ ]): ApiRunner<ExtractInputType<Operators> extends void[] ? void : ArrayToObject<ExtractInputType<Operators>>, Res>;
@@ -2,30 +2,18 @@ import type { HttpMethodDecider } from '@modern-js/types';
2
2
  import { Result } from './result';
3
3
  export type GenClientResult = Result<string>;
4
4
  export type GenClientOptions = {
5
- resourcePath: string;
6
- source: string;
7
- appDir: string;
8
- apiDir: string;
9
- lambdaDir: string;
10
- prefix: string;
11
- port: number;
12
- requestCreator?: string;
13
- fetcher?: string;
14
- target?: string;
15
- requireResolve?: typeof require.resolve;
16
- httpMethodDecider?: HttpMethodDecider;
5
+ resourcePath: string;
6
+ source: string;
7
+ appDir: string;
8
+ apiDir: string;
9
+ lambdaDir: string;
10
+ prefix: string;
11
+ port: number;
12
+ requestCreator?: string;
13
+ fetcher?: string;
14
+ target?: string;
15
+ requireResolve?: typeof require.resolve;
16
+ httpMethodDecider?: HttpMethodDecider;
17
17
  };
18
18
  export declare const DEFAULT_CLIENT_REQUEST_CREATOR = "@modern-js/create-request";
19
- export declare const generateClient: ({
20
- appDir,
21
- resourcePath,
22
- apiDir,
23
- lambdaDir,
24
- prefix,
25
- port,
26
- target,
27
- requestCreator,
28
- fetcher,
29
- requireResolve,
30
- httpMethodDecider
31
- }: GenClientOptions) => Promise<GenClientResult>;
19
+ export declare const generateClient: ({ appDir, resourcePath, apiDir, lambdaDir, prefix, port, target, requestCreator, fetcher, requireResolve, httpMethodDecider, }: GenClientOptions) => Promise<GenClientResult>;
@@ -1 +1 @@
1
- export * from './generateClient';
1
+ export * from './generateClient';
@@ -1,15 +1,15 @@
1
1
  export type Err<T = unknown> = {
2
- kind: 'Err';
3
- value: T;
4
- isErr: true;
5
- isOk: false;
2
+ kind: 'Err';
3
+ value: T;
4
+ isErr: true;
5
+ isOk: false;
6
6
  };
7
7
  export type Ok<T = unknown> = {
8
- kind: 'Ok';
9
- value: T;
10
- isErr: false;
11
- isOk: true;
8
+ kind: 'Ok';
9
+ value: T;
10
+ isErr: false;
11
+ isOk: true;
12
12
  };
13
13
  export type Result<T = unknown, E = string> = Err<E> | Ok<T>;
14
14
  export declare const Err: <E = string>(value: E) => Err<E>;
15
- export declare const Ok: <T, E = string>(value: T) => Result<T, E>;
15
+ export declare const Ok: <T, E = string>(value: T) => Result<T, E>;
@@ -1,8 +1,8 @@
1
1
  export declare class HttpError extends Error {
2
- status: number;
3
- constructor(status: number, message: string);
2
+ status: number;
3
+ constructor(status: number, message: string);
4
4
  }
5
5
  export declare class ValidationError extends HttpError {
6
- private code;
7
- constructor(status: number, message: string);
8
- }
6
+ private code;
7
+ constructor(status: number, message: string);
8
+ }
@@ -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, INPUT_PARAMS_DECIDER, isInputParamsDeciderHandler, createStorage, registerPaths, } from './utils';
@@ -1,8 +1,8 @@
1
1
  import type { z } from 'zod';
2
2
  import { Operator, HttpMethod, ResponseMetaType } from '../types';
3
3
  export interface ResponseMeta {
4
- type: ResponseMetaType;
5
- value: unknown;
4
+ type: ResponseMetaType;
5
+ value: unknown;
6
6
  }
7
7
  export declare const createHttpOperator: (method: HttpMethod) => (urlPath: string) => Operator<void>;
8
8
  export declare const Get: (urlPath: string) => Operator<void>;
@@ -15,25 +15,25 @@ export declare const Patch: (urlPath: string) => Operator<void>;
15
15
  export declare const Options: (urlPath: string) => Operator<void>;
16
16
  export declare const Head: (urlPath: string) => Operator<void>;
17
17
  export declare const Data: <Schema extends z.ZodType<any, z.ZodTypeDef, any>>(schema: Schema) => Operator<{
18
- data: z.input<Schema>;
18
+ data: z.input<Schema>;
19
19
  }, {
20
- data: z.output<Schema>;
20
+ data: z.output<Schema>;
21
21
  }>;
22
22
  export declare const Query: <Schema extends z.ZodType<any, z.ZodTypeDef, any>>(schema: Schema) => Operator<{
23
- query: z.input<Schema>;
23
+ query: z.input<Schema>;
24
24
  }, {
25
- query: z.output<Schema>;
25
+ query: z.output<Schema>;
26
26
  }>;
27
27
  export declare const Params: <Schema extends z.ZodType<any, z.ZodTypeDef, any>>(schema: Schema) => Operator<{
28
- params: z.input<Schema>;
28
+ params: z.input<Schema>;
29
29
  }, {
30
- params: z.output<Schema>;
30
+ params: z.output<Schema>;
31
31
  }>;
32
32
  export declare const Headers: <Schema extends z.ZodType<any, z.ZodTypeDef, any>>(schema: Schema) => Operator<{
33
- headers: z.input<Schema>;
33
+ headers: z.input<Schema>;
34
34
  }, {
35
- headers: z.output<Schema>;
35
+ headers: z.output<Schema>;
36
36
  }>;
37
37
  export declare const HttpCode: (statusCode: number) => Operator<void>;
38
38
  export declare const SetHeaders: (headers: Record<string, string>) => Operator<void>;
39
- export declare const Redirect: (url: string) => Operator<void>;
39
+ export declare const Redirect: (url: string) => Operator<void>;
@@ -1,16 +1,16 @@
1
1
  export declare const AllHttpMethods: string[];
2
2
  export declare enum APIMode {
3
- /**
4
- * 框架模式
5
- */
6
- FARMEWORK = "framework",
7
- /**
8
- * 函数模式
9
- */
10
- FUNCTION = "function",
3
+ /**
4
+ * 框架模式
5
+ */
6
+ FARMEWORK = "framework",
7
+ /**
8
+ * 函数模式
9
+ */
10
+ FUNCTION = "function"
11
11
  }
12
12
  export declare const FRAMEWORK_MODE_LAMBDA_DIR = "lambda";
13
13
  export declare const FRAMEWORK_MODE_APP_DIR = "app";
14
14
  export declare const INDEX_SUFFIX = "index";
15
15
  export declare const API_DIR = "api";
16
- export declare const API_FILE_RULES: string[];
16
+ export declare const API_FILE_RULES: string[];
@@ -6,56 +6,49 @@ import { ApiHandler, APIHandlerInfo } from './types';
6
6
  export * from './types';
7
7
  export * from './constants';
8
8
  export declare class ApiRouter {
9
- private apiMode;
10
- private appDir?;
11
- private apiDir;
12
- private existLambdaDir;
13
- private httpMethodDecider;
14
- private lambdaDir;
15
- private prefix;
16
- private apiFiles;
17
- private isBuild?;
18
- constructor({
19
- appDir,
20
- apiDir,
21
- lambdaDir,
22
- prefix,
23
- isBuild,
24
- httpMethodDecider
25
- }: {
26
- appDir?: string;
27
- apiDir: string;
28
- lambdaDir?: string;
29
- prefix?: string;
30
- isBuild?: boolean;
31
- httpMethodDecider?: HttpMethodDecider;
32
- });
33
- private enableRegister;
34
- isExistLambda(): boolean;
35
- getApiMode(): APIMode;
36
- getLambdaDir(): string;
37
- isApiFile(filename: string): boolean;
38
- getSingleModuleHandlers(filename: string): APIHandlerInfo[] | null;
39
- getHandlerInfo(filename: string, originFuncName: string, handler: ApiHandler): APIHandlerInfo | null;
40
- getSafeRoutePath(filename: string, handler?: ApiHandler): string;
41
- getRouteName(filename: string, handler?: ApiHandler): string;
42
- getHttpMethod(originHandlerName: string, handler?: ApiHandler): HttpMethod | null;
43
- loadApiFiles(): string[];
44
- getApiFiles(): string[];
45
- getApiHandlers(): APIHandlerInfo[];
46
- /**
47
- * 如果用户未传入或传入空串,默认为 /api
48
- * 如果传入 /,则 prefix 为 /
49
- */
50
- private initPrefix;
51
- private validateAbsolute;
52
- private getExactApiMode;
53
- private createExistChecker;
54
- private getExactLambdaDir;
55
- private getModuleInfos;
56
- private getModuleInfo;
57
- private getHandlerInfos;
58
- private getModuleHandlerInfos;
59
- private validateValidApifile;
60
- private getRoutePath;
61
- }
9
+ private apiMode;
10
+ private appDir?;
11
+ private apiDir;
12
+ private existLambdaDir;
13
+ private httpMethodDecider;
14
+ private lambdaDir;
15
+ private prefix;
16
+ private apiFiles;
17
+ private isBuild?;
18
+ constructor({ appDir, apiDir, lambdaDir, prefix, isBuild, httpMethodDecider, }: {
19
+ appDir?: string;
20
+ apiDir: string;
21
+ lambdaDir?: string;
22
+ prefix?: string;
23
+ isBuild?: boolean;
24
+ httpMethodDecider?: HttpMethodDecider;
25
+ });
26
+ private enableRegister;
27
+ isExistLambda(): boolean;
28
+ getApiMode(): APIMode;
29
+ getLambdaDir(): string;
30
+ isApiFile(filename: string): boolean;
31
+ getSingleModuleHandlers(filename: string): APIHandlerInfo[] | null;
32
+ getHandlerInfo(filename: string, originFuncName: string, handler: ApiHandler): APIHandlerInfo | null;
33
+ getSafeRoutePath(filename: string, handler?: ApiHandler): string;
34
+ getRouteName(filename: string, handler?: ApiHandler): string;
35
+ getHttpMethod(originHandlerName: string, handler?: ApiHandler): HttpMethod | null;
36
+ loadApiFiles(): string[];
37
+ getApiFiles(): string[];
38
+ getApiHandlers(): APIHandlerInfo[];
39
+ /**
40
+ * 如果用户未传入或传入空串,默认为 /api
41
+ * 如果传入 /,则 prefix 为 /
42
+ */
43
+ private initPrefix;
44
+ private validateAbsolute;
45
+ private getExactApiMode;
46
+ private createExistChecker;
47
+ private getExactLambdaDir;
48
+ private getModuleInfos;
49
+ private getModuleInfo;
50
+ private getHandlerInfos;
51
+ private getModuleHandlerInfos;
52
+ private validateValidApifile;
53
+ private getRoutePath;
54
+ }
@@ -1,17 +1,17 @@
1
1
  import { HttpMethod } from '../types';
2
2
  export type ModuleInfo = {
3
- filename: string;
4
- module: HandlerModule;
3
+ filename: string;
4
+ module: HandlerModule;
5
5
  };
6
6
  type Handler = (...args: any) => any | Promise<any>;
7
7
  export type ApiHandler = Handler;
8
8
  export type HandlerModule = Record<string, ApiHandler>;
9
9
  export type APIHandlerInfo = {
10
- handler: ApiHandler;
11
- name: string;
12
- httpMethod: HttpMethod;
13
- filename: string;
14
- routeName: string;
15
- routePath: string;
10
+ handler: ApiHandler;
11
+ name: string;
12
+ httpMethod: HttpMethod;
13
+ filename: string;
14
+ routeName: string;
15
+ routePath: string;
16
16
  };
17
- export {};
17
+ export {};
@@ -7,4 +7,4 @@ export declare const getPathFromFilename: (baseDir: string, filename: string) =>
7
7
  export declare const isHandler: (input: any) => input is Handler<any, any>;
8
8
  export declare const requireHandlerModule: (modulePath: string) => any;
9
9
  export declare const sortRoutes: (apiHandlers: APIHandlerInfo[]) => APIHandlerInfo[];
10
- export {};
10
+ export {};
@@ -1,58 +1,62 @@
1
1
  import { Merge } from 'type-fest';
2
2
  export declare enum OperatorType {
3
- Trigger = 0,
4
- Middleware = 1,
3
+ Trigger = 0,
4
+ Middleware = 1
5
5
  }
6
6
  export declare enum TriggerType {
7
- Http = 0,
7
+ Http = 0
8
8
  }
9
9
  export declare enum HttpMetadata {
10
- Method = "METHOD",
11
- Data = "DATA",
12
- Query = "QUERY",
13
- Params = "PARAMS",
14
- Headers = "HEADERS",
15
- Response = "RESPONSE",
10
+ Method = "METHOD",
11
+ Data = "DATA",
12
+ Query = "QUERY",
13
+ Params = "PARAMS",
14
+ Headers = "HEADERS",
15
+ Response = "RESPONSE"
16
16
  }
17
17
  export declare enum ResponseMetaType {
18
- StatusCode = 0,
19
- Redirect = 1,
20
- Headers = 2,
18
+ StatusCode = 0,
19
+ Redirect = 1,
20
+ Headers = 2
21
21
  }
22
22
  export declare enum HttpMethod {
23
- Get = "GET",
24
- Post = "POST",
25
- Put = "PUT",
26
- Delete = "DELETE",
27
- Connect = "CONNECT",
28
- Trace = "TRACE",
29
- Patch = "PATCH",
30
- Options = "OPTIONS",
31
- Head = "HEAD",
23
+ Get = "GET",
24
+ Post = "POST",
25
+ Put = "PUT",
26
+ Delete = "DELETE",
27
+ Connect = "CONNECT",
28
+ Trace = "TRACE",
29
+ Patch = "PATCH",
30
+ Options = "OPTIONS",
31
+ Head = "HEAD"
32
32
  }
33
33
  export type InputSchemaMeata = Extract<HttpMetadata, HttpMetadata.Data | HttpMetadata.Query | HttpMetadata.Headers | HttpMetadata.Params>;
34
34
  export type ExecuteFunc<Outputs> = (helper: ExecuteHelper<Outputs>, next: () => Promise<any>) => Promise<any>;
35
35
  export type ExecuteHelper<Outputs> = {
36
- result?: any;
37
- inputs: Outputs;
36
+ result?: any;
37
+ inputs: Outputs;
38
38
  };
39
39
  export type MetadataHelper = {
40
- setMetadata: <T = any>(key: any, value: T) => void;
41
- getMetadata: <T = any>(key: any) => T;
40
+ setMetadata: <T = any>(key: any, value: T) => void;
41
+ getMetadata: <T = any>(key: any) => T;
42
42
  };
43
43
  export type Operator<Input = any, Output = Input> = {
44
- name: string;
45
- inputType?: Input;
46
- outputType?: Output;
47
- metadata?: (helper: MetadataHelper) => void;
48
- validate?: ExecuteFunc<Output>;
49
- execute?: ExecuteFunc<Output>;
44
+ name: string;
45
+ inputType?: Input;
46
+ outputType?: Output;
47
+ metadata?: (helper: MetadataHelper) => void;
48
+ validate?: ExecuteFunc<Output>;
49
+ execute?: ExecuteFunc<Output>;
50
50
  };
51
51
  export type MaybeAsync<T> = Promise<T> | T;
52
52
  export type ApiRunner<Input extends object | void | unknown, Output extends MaybeAsync<any>> = (...args: Input extends void ? [] : [input: Input]) => Output;
53
53
  export type NonNullable<T> = Exclude<T, null | undefined>;
54
- export type ExtractInputType<T> = { [key in keyof T]: T[key] extends Operator<any, any> ? NonNullable<T[key]['inputType']> : void };
55
- export type ExtractOuputType<T> = { [key in keyof T]: T[key] extends Operator<any, any> ? NonNullable<T[key]['outputType']> : void };
54
+ export type ExtractInputType<T> = {
55
+ [key in keyof T]: T[key] extends Operator<any, any> ? NonNullable<T[key]['inputType']> : void;
56
+ };
57
+ export type ExtractOuputType<T> = {
58
+ [key in keyof T]: T[key] extends Operator<any, any> ? NonNullable<T[key]['outputType']> : void;
59
+ };
56
60
  export type ArrayToObject<T, R = {}> = T extends [infer First, ...infer Rest] ? First extends PromiseLike<infer PromiseValue> ? PromiseValue : First extends object ? Merge<First, ArrayToObject<Rest, R>> : ArrayToObject<Rest, R> : R;
57
61
  export type AsyncFunction = (...args: any[]) => Promise<any>;
58
- export declare const httpMethods: HttpMethod[];
62
+ export declare const httpMethods: HttpMethod[];
@@ -1,7 +1,7 @@
1
1
  interface Paths {
2
- [key: string]: string[] | string;
2
+ [key: string]: string[] | string;
3
3
  }
4
4
  export declare const getRelativeRuntimePath: (appDirectory: string, serverRuntimePath: string) => string;
5
5
  export declare const createMatchPath: (paths: Paths) => (request: string) => string | null;
6
6
  export declare const registerPaths: (paths: Paths) => () => void;
7
- export {};
7
+ export {};
@@ -1 +1 @@
1
- export declare const debug: import("@modern-js/utils/compiled/debug").Debugger;
1
+ export declare const debug: import("@modern-js/utils/compiled/debug").Debugger;
@@ -2,4 +2,4 @@ export * from './storage';
2
2
  export * from './alias';
3
3
  export { debug } from './debug';
4
4
  export * from './meta';
5
- export * from './validate';
5
+ export * from './validate';
@@ -1,4 +1,4 @@
1
1
  export declare const HANDLER_WITH_META = "HANDLER_WITH_META";
2
2
  export declare const INPUT_PARAMS_DECIDER = "INPUT_PARAMS_DECIDER";
3
3
  export declare const isWithMetaHandler: (handler: any) => any;
4
- export declare const isInputParamsDeciderHandler: (handler: any) => any;
4
+ export declare const isInputParamsDeciderHandler: (handler: any) => any;
@@ -1,5 +1,5 @@
1
1
  declare const createStorage: <T>() => {
2
- run: <O>(context: T, cb: () => O | Promise<O>) => Promise<O>;
3
- useContext: () => T;
2
+ run: <O>(context: T, cb: () => O | Promise<O>) => Promise<O>;
3
+ useContext: () => T;
4
4
  };
5
- export { createStorage };
5
+ export { createStorage };
@@ -1,5 +1,5 @@
1
1
  export declare const getTypeErrorMessage: (actual: unknown) => string;
2
2
  export declare class ERR_INVALID_ARG_TYPE extends Error {
3
- constructor(funcName: string, expectedType: string, actual: unknown);
3
+ constructor(funcName: string, expectedType: string, actual: unknown);
4
4
  }
5
- export declare const validateFunction: (maybeFunc: unknown, name: string) => boolean;
5
+ export declare const validateFunction: (maybeFunc: unknown, name: string) => boolean;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.41.0",
18
+ "version": "2.42.0",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
@@ -31,7 +31,7 @@
31
31
  "koa-compose": "^4.1.0",
32
32
  "reflect-metadata": "^0.1.13",
33
33
  "@swc/helpers": "0.5.3",
34
- "@modern-js/utils": "2.41.0"
34
+ "@modern-js/utils": "2.42.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/jest": "^29",
@@ -43,9 +43,9 @@
43
43
  "type-fest": "2.15.0",
44
44
  "typescript": "^5",
45
45
  "zod": "^3.22.3",
46
- "@scripts/build": "2.41.0",
47
- "@scripts/jest-config": "2.41.0",
48
- "@modern-js/types": "2.41.0"
46
+ "@modern-js/types": "2.42.0",
47
+ "@scripts/build": "2.42.0",
48
+ "@scripts/jest-config": "2.42.0"
49
49
  },
50
50
  "peerDependencies": {
51
51
  "zod": "^3.22.3",