@faapi/faapi 0.0.0-canary.4ab8d97 → 0.0.0-canary.a3f7014
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/README.md +7 -1
- package/dist/cli/index.js +5887 -5340
- package/dist/cli/index.js.map +1 -1
- package/dist/index.d.ts +379 -66
- package/dist/index.js +2765 -163
- package/dist/index.js.map +1 -1
- package/package.json +6 -7
package/dist/index.d.ts
CHANGED
|
@@ -99,6 +99,14 @@ interface FaapiContext {
|
|
|
99
99
|
headers: Headers;
|
|
100
100
|
method: string;
|
|
101
101
|
path: string;
|
|
102
|
+
/**
|
|
103
|
+
* 客户端 IP
|
|
104
|
+
*
|
|
105
|
+
* 优先 `x-forwarded-for` 第一个 IP(反向代理场景),回退到 socket.remoteAddress。
|
|
106
|
+
* IPv6 形式 `::ffff:1.2.3.4` 会被规整为 IPv4 形式 `1.2.3.4`。
|
|
107
|
+
* 无法获取时为空字符串。
|
|
108
|
+
*/
|
|
109
|
+
ip: string;
|
|
102
110
|
/** 解析后的所有 cookie 键值对 */
|
|
103
111
|
cookies: Record<string, string>;
|
|
104
112
|
/** 配置文件中的自定义业务配置(类型可通过 declare module '@faapi/faapi' 增强 FaapiContextConfig) */
|
|
@@ -111,6 +119,19 @@ interface FaapiContext {
|
|
|
111
119
|
* 设置响应头
|
|
112
120
|
*/
|
|
113
121
|
setHeader(key: string, value: string): void;
|
|
122
|
+
/**
|
|
123
|
+
* 设置 ETag 响应头
|
|
124
|
+
*
|
|
125
|
+
* handler 中基于业务数据(如 updatedAt / version / contentHash)设置 ETag:
|
|
126
|
+
* ```ts
|
|
127
|
+
* export function GET(ctx) {
|
|
128
|
+
* const data = await fetchData();
|
|
129
|
+
* ctx.setETag(`"${data.version}-${data.updatedAt}"`);
|
|
130
|
+
* return data;
|
|
131
|
+
* }
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
setETag(value: string): void;
|
|
114
135
|
/**
|
|
115
136
|
* 返回 JSON 响应(handler 直接 return)
|
|
116
137
|
*
|
|
@@ -264,15 +285,24 @@ interface CorsOptions {
|
|
|
264
285
|
*/
|
|
265
286
|
declare function cors(options?: CorsOptions): FaapiMiddleware;
|
|
266
287
|
|
|
288
|
+
type LoggerFn = (messageOrObj: string | Record<string, unknown>, message?: string) => void;
|
|
267
289
|
interface LoggerOptions {
|
|
268
|
-
/**
|
|
269
|
-
|
|
290
|
+
/**
|
|
291
|
+
* 自定义日志函数
|
|
292
|
+
*
|
|
293
|
+
* - 传入 `console.log`(默认):纯文本格式 `GET /api/users 200 12ms`
|
|
294
|
+
* - 传入 pino logger:结构化日志 `logger.info({ method, path, status, durationMs }, 'request completed')`
|
|
295
|
+
* - 传入 winston logger:`logger.info('GET /api/users 200 12ms', { method, path })`
|
|
296
|
+
*/
|
|
297
|
+
log?: LoggerFn;
|
|
270
298
|
}
|
|
271
299
|
/**
|
|
272
300
|
* 创建请求日志中间件(洋葱模型)
|
|
273
301
|
*
|
|
274
|
-
*
|
|
275
|
-
*
|
|
302
|
+
* 日志格式(文本模式):GET /api/users 200 12ms
|
|
303
|
+
* 错误格式(文本模式):POST /api/users 400 45ms - Error: ...
|
|
304
|
+
*
|
|
305
|
+
* 结构化模式:传入 pino/winston 等 logger 实例时,会自动传递结构化字段。
|
|
276
306
|
*
|
|
277
307
|
* before/after 一体,闭包变量共享开始时间,无需污染 ctx。
|
|
278
308
|
* 错误用 try/catch 捕获,记录后重新抛出(让上层处理)。
|
|
@@ -280,6 +310,23 @@ interface LoggerOptions {
|
|
|
280
310
|
*/
|
|
281
311
|
declare function logger(options?: LoggerOptions): FaapiMiddleware;
|
|
282
312
|
|
|
313
|
+
interface HelmetOptions {
|
|
314
|
+
contentSecurityPolicy?: string | false;
|
|
315
|
+
xFrameOptions?: 'DENY' | 'SAMEORIGIN' | false;
|
|
316
|
+
xContentTypeOptions?: boolean;
|
|
317
|
+
referrerPolicy?: string | false;
|
|
318
|
+
strictTransportSecurity?: string | false;
|
|
319
|
+
xDnsPrefetchControl?: boolean;
|
|
320
|
+
xDownloadOptions?: boolean;
|
|
321
|
+
xPermittedCrossDomainPolicies?: string | false;
|
|
322
|
+
crossOriginOpenerPolicy?: string | false;
|
|
323
|
+
crossOriginResourcePolicy?: string | false;
|
|
324
|
+
crossOriginEmbedderPolicy?: string | false;
|
|
325
|
+
originAgentCluster?: boolean;
|
|
326
|
+
xPoweredBy?: boolean;
|
|
327
|
+
}
|
|
328
|
+
declare function helmet(options?: HelmetOptions): FaapiMiddleware;
|
|
329
|
+
|
|
283
330
|
declare const HTTP_METHODS: readonly ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"];
|
|
284
331
|
type HttpMethod = (typeof HTTP_METHODS)[number];
|
|
285
332
|
|
|
@@ -296,7 +343,26 @@ interface RouteRecord {
|
|
|
296
343
|
/** 路由对应的注入器映射表(从根到路由目录合并,构建时加载) */
|
|
297
344
|
injectors?: InjectorMap;
|
|
298
345
|
}
|
|
346
|
+
/**
|
|
347
|
+
* WebSocket 路由记录
|
|
348
|
+
*
|
|
349
|
+
* 与 HTTP RouteRecord 类似,但不绑定 HTTP 方法(WS 是协议升级,不区分 GET/POST)。
|
|
350
|
+
* 一个 handler.ts 中导出 WS 即生成一条 WS 路由记录。
|
|
351
|
+
*/
|
|
352
|
+
interface WsRouteRecord {
|
|
353
|
+
urlPath: string;
|
|
354
|
+
filePath: string;
|
|
355
|
+
paramNames: string[];
|
|
356
|
+
isDynamic: boolean;
|
|
357
|
+
/** 是否为 catch-all 路由([...slug]) */
|
|
358
|
+
isCatchAll?: boolean;
|
|
359
|
+
/** 路由对应的中间件集合(握手阶段执行,复用鉴权/CORS/日志) */
|
|
360
|
+
middlewares?: FaapiMiddleware[];
|
|
361
|
+
/** 路由对应的注入器映射表 */
|
|
362
|
+
injectors?: InjectorMap;
|
|
363
|
+
}
|
|
299
364
|
type RouteManifest = RouteRecord[];
|
|
365
|
+
type WsRouteManifest = WsRouteRecord[];
|
|
300
366
|
/**
|
|
301
367
|
* 路由单个参数的 schema 描述
|
|
302
368
|
*
|
|
@@ -315,6 +381,18 @@ interface RouteInputSchema {
|
|
|
315
381
|
schemaName: string | null;
|
|
316
382
|
properties: RouteParamSchema[];
|
|
317
383
|
}
|
|
384
|
+
/**
|
|
385
|
+
* 路由响应类型的 schema 描述
|
|
386
|
+
*
|
|
387
|
+
* 由 @faapi/schema 扩展包的 buildRouteSchemas 生成。
|
|
388
|
+
* output 为 null 表示无显式返回类型注解、void/Promise<void>、或解析失败降级。
|
|
389
|
+
*/
|
|
390
|
+
interface RouteOutputSchema {
|
|
391
|
+
/** 命名类型名(如 'UserResponse'),内联类型为 null */
|
|
392
|
+
schemaName: string | null;
|
|
393
|
+
/** 顶层属性列表 */
|
|
394
|
+
properties: RouteParamSchema[];
|
|
395
|
+
}
|
|
318
396
|
/**
|
|
319
397
|
* 路由的完整 schema 描述
|
|
320
398
|
*
|
|
@@ -327,6 +405,8 @@ interface RouteInfo {
|
|
|
327
405
|
filePath: string;
|
|
328
406
|
isDynamic: boolean;
|
|
329
407
|
inputs: RouteInputSchema[];
|
|
408
|
+
/** 响应类型描述(null 表示无返回类型注解/void/解析失败) */
|
|
409
|
+
output: RouteOutputSchema | null;
|
|
330
410
|
}
|
|
331
411
|
|
|
332
412
|
/** HTTP 请求 handler 类型 */
|
|
@@ -344,8 +424,10 @@ type UpgradeHandler = (req: IncomingMessage, socket: Socket, head: Buffer) => vo
|
|
|
344
424
|
interface PluginContext {
|
|
345
425
|
/** 项目根目录 */
|
|
346
426
|
rootDir: string;
|
|
347
|
-
/**
|
|
427
|
+
/** 当前路由清单(setup 时的快照,reloadRoutes 后不会更新;需最新路由用 getRoutes()) */
|
|
348
428
|
routes: RouteManifest;
|
|
429
|
+
/** 获取最新路由清单(reloadRoutes 后返回更新后的数组) */
|
|
430
|
+
getRoutes: () => RouteManifest;
|
|
349
431
|
/** HTTP 服务器实例(未 listen) */
|
|
350
432
|
server: Server;
|
|
351
433
|
/** 自定义业务配置(faapi.config.ts 中的自定义 key) */
|
|
@@ -416,40 +498,31 @@ type PluginDeclaration = string | [string, unknown] | {
|
|
|
416
498
|
options?: unknown;
|
|
417
499
|
};
|
|
418
500
|
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
*/
|
|
425
|
-
type ResponseFormatFn = (data: unknown, ctx: FaapiContext) => unknown;
|
|
426
|
-
/**
|
|
427
|
-
* 错误响应格式化函数
|
|
428
|
-
*
|
|
429
|
-
* 优先于内置 formatErrorResponse 处理错误。返回 Response 表示已处理;
|
|
430
|
-
* 返回 null/undefined 表示不处理,由内置 formatErrorResponse 兜底。
|
|
431
|
-
*/
|
|
432
|
-
type ErrorFormatFn = (error: unknown, ctx?: FaapiContext) => Response | null | undefined;
|
|
501
|
+
interface Http2Options {
|
|
502
|
+
key?: string;
|
|
503
|
+
cert?: string;
|
|
504
|
+
}
|
|
505
|
+
|
|
433
506
|
/**
|
|
434
507
|
* 生命周期钩子
|
|
435
508
|
*/
|
|
436
509
|
interface LifecycleHooks {
|
|
437
|
-
/**
|
|
510
|
+
/** 服务器启动后调用(适合初始化数据库连接等) */
|
|
438
511
|
onReady?: (ctx: LifecycleContext) => Promise<void> | void;
|
|
439
512
|
/** 服务器关闭时调用(适合清理资源、优雅关闭) */
|
|
440
513
|
onClose?: (ctx: LifecycleContext) => Promise<void> | void;
|
|
441
514
|
/**
|
|
442
|
-
*
|
|
515
|
+
* 请求错误已被处理为响应后调用(参考 Fastify onError 语义)
|
|
443
516
|
*
|
|
444
|
-
*
|
|
517
|
+
* 时机:handler 抛错 → 全局中间件 try/catch(若有) → 框架内置 formatErrorResponse 兜底
|
|
445
518
|
* → 响应发出后 → onError 触发副作用
|
|
446
519
|
*
|
|
447
|
-
*
|
|
448
|
-
*
|
|
520
|
+
* 职责:日志上报、告警、链路追踪等副作用。**不修改、不替换已生成的响应**。
|
|
521
|
+
* 自身抛错会被捕获并忽略,不影响响应已发送的事实。
|
|
449
522
|
*
|
|
450
|
-
*
|
|
451
|
-
* -
|
|
452
|
-
* - onError
|
|
523
|
+
* 与全局错误中间件的区别:
|
|
524
|
+
* - 全局错误中间件:把 error 翻译成 Response(主入口,决定响应内容)
|
|
525
|
+
* - onError:响应发出后的副作用(不能改响应)
|
|
453
526
|
*/
|
|
454
527
|
onError?: (error: unknown, ctx: FaapiContext) => Promise<void> | void;
|
|
455
528
|
}
|
|
@@ -471,7 +544,6 @@ interface LifecycleContext {
|
|
|
471
544
|
* ```ts
|
|
472
545
|
* import type { FaapiConfig } from '@faapi/faapi';
|
|
473
546
|
* export default {
|
|
474
|
-
* port: 3000,
|
|
475
547
|
* cors: { origin: '*' },
|
|
476
548
|
* } satisfies FaapiConfig;
|
|
477
549
|
* ```
|
|
@@ -480,7 +552,6 @@ interface LifecycleContext {
|
|
|
480
552
|
* ```ts
|
|
481
553
|
* import type { FaapiConfig } from '@faapi/faapi';
|
|
482
554
|
* export default {
|
|
483
|
-
* port: 3000,
|
|
484
555
|
* cors: { origin: '*' },
|
|
485
556
|
* // 自定义业务配置(任意 key)
|
|
486
557
|
* db: { host: 'localhost', port: 5432 },
|
|
@@ -488,20 +559,25 @@ interface LifecycleContext {
|
|
|
488
559
|
* ```
|
|
489
560
|
*
|
|
490
561
|
* 环境覆盖通过 faapi.config.{NODE_ENV}.ts 实现(如 faapi.config.production.ts)
|
|
562
|
+
*
|
|
563
|
+
* 框架元信息通过环境变量配置(不放在 config 内):
|
|
564
|
+
* - `FAAPI_APP_DIR`:源码目录前缀,默认 'src',设为 '.' 表示源码在项目根目录
|
|
565
|
+
* - `PORT`:服务端口,默认 3000
|
|
566
|
+
* - `FAAPI_OUT_DIR`:产物输出目录,dev 固定为 '.faapi/dev',prod 默认 'dist'
|
|
491
567
|
*/
|
|
492
568
|
interface FaapiConfig {
|
|
493
|
-
/** 服务端口,默认 3000(可被 --port / PORT 环境变量覆盖) */
|
|
494
|
-
port?: number;
|
|
495
|
-
/** 静态文件目录 */
|
|
496
|
-
staticDir?: string;
|
|
497
569
|
/** CORS 配置,false 禁用 */
|
|
498
570
|
cors?: CorsOptions | boolean;
|
|
499
|
-
/** 统一响应格式化函数 */
|
|
500
|
-
responseFormat?: ResponseFormatFn;
|
|
501
|
-
/** 错误响应格式化函数 */
|
|
502
|
-
errorFormat?: ErrorFormatFn;
|
|
503
571
|
/** 生命周期钩子 */
|
|
504
572
|
lifecycle?: LifecycleHooks;
|
|
573
|
+
/** 安全头配置,false 禁用 */
|
|
574
|
+
helmet?: HelmetOptions | boolean;
|
|
575
|
+
/** 请求体大小限制(字节),默认 10MB(10 * 1024 * 1024) */
|
|
576
|
+
bodyLimit?: number;
|
|
577
|
+
/** 日志中间件配置 */
|
|
578
|
+
logger?: LoggerOptions | boolean;
|
|
579
|
+
/** HTTP/2 配置,false 禁用(默认 http/1.1) */
|
|
580
|
+
http2?: Http2Options | boolean;
|
|
505
581
|
/**
|
|
506
582
|
* 全局中间件:对所有路由(HTTP + WebSocket 握手)生效
|
|
507
583
|
*
|
|
@@ -664,6 +740,15 @@ interface WsContext {
|
|
|
664
740
|
*/
|
|
665
741
|
type WsHandler = (ctx: WsContext) => WsEventHandlers | void;
|
|
666
742
|
|
|
743
|
+
/**
|
|
744
|
+
* 清理所有 Program 缓存(watch 模式下文件变化时调用)
|
|
745
|
+
*
|
|
746
|
+
* 全量清理而非增量清理,理由:
|
|
747
|
+
* - 简单可靠,无状态一致性问题
|
|
748
|
+
* - 跨文件类型引用需要所有文件的 Program 同步更新
|
|
749
|
+
* - dev 模式文件量有限,全量重建在百毫秒级
|
|
750
|
+
*/
|
|
751
|
+
declare function invalidateProgramCache(): void;
|
|
667
752
|
/**
|
|
668
753
|
* 为指定文件创建 TypeScript Program(带缓存)
|
|
669
754
|
*
|
|
@@ -744,7 +829,79 @@ interface PropertyType {
|
|
|
744
829
|
name: string;
|
|
745
830
|
type: RuntimeType;
|
|
746
831
|
optional: boolean;
|
|
832
|
+
/**
|
|
833
|
+
* 字段级 JSDoc 约束标签(@max/@min/@maxLength 等)
|
|
834
|
+
*
|
|
835
|
+
* 来自字段 JSDoc 注释,由 generateZodSchema 转为 zod 链式调用。
|
|
836
|
+
* 约束与字段类型不匹配时在提取阶段抛 SchemaExtractionError。
|
|
837
|
+
*/
|
|
838
|
+
constraints?: TypeConstraint[];
|
|
747
839
|
}
|
|
840
|
+
/**
|
|
841
|
+
* JSDoc 约束标签的运行时描述
|
|
842
|
+
*
|
|
843
|
+
* 由字段 JSDoc 注释提取,对应 zod schema 的链式约束方法。
|
|
844
|
+
* 仅在 PropertyType.constraints 中出现,不挂在嵌套类型(array 元素、tuple 元素等)上。
|
|
845
|
+
*/
|
|
846
|
+
type TypeConstraint = {
|
|
847
|
+
kind: 'max';
|
|
848
|
+
value: number;
|
|
849
|
+
} | {
|
|
850
|
+
kind: 'min';
|
|
851
|
+
value: number;
|
|
852
|
+
} | {
|
|
853
|
+
kind: 'int';
|
|
854
|
+
} | {
|
|
855
|
+
kind: 'positive';
|
|
856
|
+
} | {
|
|
857
|
+
kind: 'negative';
|
|
858
|
+
} | {
|
|
859
|
+
kind: 'nonnegative';
|
|
860
|
+
} | {
|
|
861
|
+
kind: 'nonpositive';
|
|
862
|
+
} | {
|
|
863
|
+
kind: 'maxLength';
|
|
864
|
+
value: number;
|
|
865
|
+
} | {
|
|
866
|
+
kind: 'minLength';
|
|
867
|
+
value: number;
|
|
868
|
+
} | {
|
|
869
|
+
kind: 'length';
|
|
870
|
+
value: number;
|
|
871
|
+
} | {
|
|
872
|
+
kind: 'regex';
|
|
873
|
+
pattern: string;
|
|
874
|
+
flags?: string;
|
|
875
|
+
} | {
|
|
876
|
+
kind: 'email';
|
|
877
|
+
} | {
|
|
878
|
+
kind: 'url';
|
|
879
|
+
} | {
|
|
880
|
+
kind: 'uuid';
|
|
881
|
+
};
|
|
882
|
+
/**
|
|
883
|
+
* 将 TypeScript 类型节点解析为运行时类型描述
|
|
884
|
+
*
|
|
885
|
+
* 支持的类型:
|
|
886
|
+
* - 基础类型:string / number / boolean / null / undefined / any / unknown / void
|
|
887
|
+
* - bigint:不支持(HTTP/JSON 不能传输),AST 提取阶段抛 SchemaExtractionError
|
|
888
|
+
* - 字面量类型:'foo' / 42 / true
|
|
889
|
+
* - 数组类型:T[] / Array<T> / ReadonlyArray<T> / readonly T[]
|
|
890
|
+
* - 元组类型:[string, number] / [string, number?] / [string, ...number[]] / readonly [T, U](按位置校验)
|
|
891
|
+
* - 对象类型:{ name: string; age?: number }(含 readonly 字段修饰符,忽略 readonly)
|
|
892
|
+
* - 联合类型:string | null
|
|
893
|
+
* - 交叉类型:A & B(按对象合并处理)
|
|
894
|
+
* - 引用类型:Date / 其他 interface(递归解析)
|
|
895
|
+
* - 工具类型:Record<K, V> / Partial<T> / Readonly<T>(best effort)
|
|
896
|
+
* - Pick<T, K> / Omit<T, K>:K 支持字面量联合、类型别名、keyof T
|
|
897
|
+
*
|
|
898
|
+
* readonly 是编译期约束,运行时不产生校验语义,所有 readonly 修饰符统一忽略。
|
|
899
|
+
*
|
|
900
|
+
* @param typeNode TypeScript 类型节点
|
|
901
|
+
* @param checker 类型 checker(用于解析引用类型)
|
|
902
|
+
* @param visited 防止递归循环
|
|
903
|
+
*/
|
|
904
|
+
declare function resolveTypeNode(typeNode: ts.TypeNode, checker?: ts.TypeChecker, visited?: Set<string>): RuntimeType;
|
|
748
905
|
|
|
749
906
|
interface HandlerTypeInfo {
|
|
750
907
|
name: string;
|
|
@@ -780,48 +937,204 @@ declare function extractTypeInfo(program: ts.Program, filePath: string, typeName
|
|
|
780
937
|
declare function getInputTypeForMethod(method: string): 'query' | 'body';
|
|
781
938
|
|
|
782
939
|
/**
|
|
783
|
-
*
|
|
940
|
+
* 单个路由的 schema 提取结果
|
|
941
|
+
*
|
|
942
|
+
* key 使用 urlPath(如 '/api/hello')而非 filePath,因为 urlPath 在 dev/prod 完全一致,
|
|
943
|
+
* 无需 remapManifestKeys 桥接 .ts/.js 路径差异。
|
|
784
944
|
*/
|
|
785
|
-
interface
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
945
|
+
interface RouteSchemaSource {
|
|
946
|
+
/** 路由 URL 路径(如 '/api/hello'),作为 schema key */
|
|
947
|
+
urlPath: string;
|
|
948
|
+
/** 源文件绝对路径(用于 generateSchemaFiles 按文件分组生成 zod.js) */
|
|
949
|
+
filePath: string;
|
|
950
|
+
schemaName: string;
|
|
951
|
+
typeInfo: HandlerTypeInfo | null;
|
|
789
952
|
}
|
|
790
953
|
/**
|
|
791
|
-
*
|
|
954
|
+
* 从路由清单收集 schema 提取所需的原始数据
|
|
955
|
+
*
|
|
956
|
+
* dev 和 prd 共享的核心提取流程:
|
|
957
|
+
* 1. 按文件分组遍历路由
|
|
958
|
+
* 2. 对每个文件 createProgram + extractAllTypes 收集所有类型
|
|
959
|
+
* 3. 用 analyzeInjection + extractTypeInfo 提取每个路由的 schema 类型
|
|
960
|
+
* 4. 同时返回按文件分组的 allTypesMap 和合并后的全局 allTypes
|
|
961
|
+
*
|
|
962
|
+
* 调用方基于返回的 sources 和 allTypes 各自做最终转换:
|
|
963
|
+
* - dev:生成 JS 模块文件 → import 加载(用 allTypesByFile)
|
|
964
|
+
* - prd:生成 JS 模块代码 → SchemaModuleEntry[](用 allTypesByFile)
|
|
792
965
|
*/
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
966
|
+
declare function collectRouteSchemaSources(routes: RouteManifest, rootDir?: string): {
|
|
967
|
+
sources: RouteSchemaSource[];
|
|
968
|
+
/** 按文件分组的类型映射(prd writeSchemaModule 用) */
|
|
969
|
+
allTypesByFile: Map<string, Map<string, HandlerTypeInfo>>;
|
|
970
|
+
/** 合并后的全局类型映射(兼容旧调用方保留,新路径使用 allTypesByFile) */
|
|
971
|
+
mergedAllTypes: Map<string, HandlerTypeInfo>;
|
|
972
|
+
};
|
|
973
|
+
|
|
974
|
+
/**
|
|
975
|
+
* 加载 faapi 配置文件
|
|
976
|
+
*
|
|
977
|
+
* 统一读取 `<outDir>/faapi-config.js` 产物:
|
|
978
|
+
* - dev 模式:`faapi dev` 启动时由 `compileConfig` 生成 `.faapi/dev/faapi-config.js`
|
|
979
|
+
* - prod 模式:`faapi build` 时由 `compileConfig` 生成 `dist/faapi-config.js`
|
|
980
|
+
*
|
|
981
|
+
* 产物由 `compileConfig` 在构建阶段合并 env 后固化,运行时不读源码、不现场编译、不按 env 合并。
|
|
982
|
+
*
|
|
983
|
+
* - 产物存在 → import 并返回 default
|
|
984
|
+
* - 产物不存在但源码有配置文件 → 抛错(强制 rebuild)
|
|
985
|
+
* - 源码也无配置文件 → 返回 `null`(配置可选)
|
|
986
|
+
*
|
|
987
|
+
* @param rootDir 项目根目录
|
|
988
|
+
* @param outDir 产物目录(如 'dist' 或 '.faapi/dev')
|
|
989
|
+
* @returns 配置对象,无配置文件时返回 null
|
|
990
|
+
*/
|
|
991
|
+
declare function loadConfig(rootDir: string, outDir: string): Promise<Partial<FaapiConfig> | null>;
|
|
992
|
+
|
|
993
|
+
declare const VALIDATION_ERROR = "VALIDATION_ERROR";
|
|
994
|
+
declare const ROUTE_NOT_FOUND = "ROUTE_NOT_FOUND";
|
|
995
|
+
declare const METHOD_NOT_ALLOWED = "METHOD_NOT_ALLOWED";
|
|
996
|
+
declare const INTERNAL_ERROR = "INTERNAL_ERROR";
|
|
997
|
+
declare const MODULE_LOAD_ERROR = "MODULE_LOAD_ERROR";
|
|
998
|
+
type ErrorCode = typeof VALIDATION_ERROR | typeof ROUTE_NOT_FOUND | typeof METHOD_NOT_ALLOWED | typeof INTERNAL_ERROR | typeof MODULE_LOAD_ERROR;
|
|
999
|
+
|
|
1000
|
+
declare class FaapiError extends Error {
|
|
1001
|
+
readonly code: ErrorCode;
|
|
1002
|
+
readonly statusCode: number;
|
|
1003
|
+
constructor(code: ErrorCode, message: string, statusCode: number);
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
declare class ValidationError extends FaapiError {
|
|
1007
|
+
readonly issues: ValidationIssue[];
|
|
1008
|
+
constructor(message: string, issues: ValidationIssue[]);
|
|
1009
|
+
}
|
|
1010
|
+
declare class RouteNotFoundError extends FaapiError {
|
|
1011
|
+
constructor(path: string);
|
|
1012
|
+
}
|
|
1013
|
+
declare class MethodNotAllowedError extends FaapiError {
|
|
1014
|
+
readonly allowedMethods: string[];
|
|
1015
|
+
constructor(method: string, path: string, allowedMethods: string[]);
|
|
1016
|
+
}
|
|
1017
|
+
declare class InternalError extends FaapiError {
|
|
1018
|
+
constructor(message: string);
|
|
1019
|
+
}
|
|
1020
|
+
declare class ModuleLoadError extends FaapiError {
|
|
1021
|
+
constructor(filePath: string, reason: string);
|
|
798
1022
|
}
|
|
799
1023
|
/**
|
|
800
|
-
*
|
|
1024
|
+
* 校验问题类型
|
|
801
1025
|
*
|
|
802
|
-
*
|
|
803
|
-
*
|
|
1026
|
+
* 结构化错误信息,便于上层(全局错误中间件/前端)按 code 做不同处理,
|
|
1027
|
+
* 不依赖字符串解析。message 仅为人类可读的兜底描述。
|
|
804
1028
|
*
|
|
805
|
-
*
|
|
806
|
-
*
|
|
807
|
-
*
|
|
1029
|
+
* code 与 HTTP 状态码的映射(由 ValidationError 推导):
|
|
1030
|
+
* - INVALID_FORMAT / MISSING_FIELD → 400 Bad Request
|
|
1031
|
+
* - TYPE_MISMATCH / INVALID_VALUE / COERCE_FAILED → 422 Unprocessable Entity
|
|
808
1032
|
*/
|
|
809
|
-
|
|
1033
|
+
interface ValidationIssue {
|
|
1034
|
+
/** 字段路径,如 'user.address.city' */
|
|
1035
|
+
path: string;
|
|
1036
|
+
/** 错误码,机器可读的契约 */
|
|
1037
|
+
code: ValidationErrorCode;
|
|
1038
|
+
/** 期望类型/值,如 'number' / '"admin" | "user"' */
|
|
1039
|
+
expected: string;
|
|
1040
|
+
/** 实际类型/值,如 'string' / 'undefined' */
|
|
1041
|
+
received: string;
|
|
1042
|
+
/** 人类可读的本地化消息(兜底,不保证稳定) */
|
|
1043
|
+
message: string;
|
|
1044
|
+
}
|
|
1045
|
+
type ValidationErrorCode = 'TYPE_MISMATCH' | 'MISSING_FIELD' | 'INVALID_FORMAT' | 'INVALID_VALUE' | 'COERCE_FAILED';
|
|
1046
|
+
|
|
1047
|
+
interface InjectOptions {
|
|
1048
|
+
method?: string;
|
|
1049
|
+
path?: string;
|
|
1050
|
+
headers?: Record<string, string>;
|
|
1051
|
+
query?: Record<string, string>;
|
|
1052
|
+
body?: unknown;
|
|
1053
|
+
}
|
|
1054
|
+
interface InjectResponse {
|
|
1055
|
+
status: number;
|
|
1056
|
+
headers: Headers;
|
|
1057
|
+
body: unknown;
|
|
1058
|
+
}
|
|
1059
|
+
interface CreateAppOptions {
|
|
1060
|
+
/** 项目根目录,默认 process.cwd() */
|
|
1061
|
+
rootDir?: string;
|
|
1062
|
+
/** 源码目录前缀,覆盖环境变量 FAAPI_APP_DIR,默认 'src' */
|
|
1063
|
+
appDir?: string;
|
|
1064
|
+
/** 端口号,也可在 listen() 时传入;默认环境变量 PORT 或 3000 */
|
|
1065
|
+
port?: number;
|
|
1066
|
+
}
|
|
1067
|
+
/** 应用基础接口(dev/prod 共用,不含 reloadRoutes) */
|
|
1068
|
+
interface AppBase {
|
|
1069
|
+
/** Node.js Server 实例(listen 后可用,close 后置 null) */
|
|
1070
|
+
server: Server | null;
|
|
1071
|
+
/** 排序后的路由清单 */
|
|
1072
|
+
routes: RouteManifest;
|
|
1073
|
+
/** WebSocket 路由清单 */
|
|
1074
|
+
wsRoutes: WsRouteManifest;
|
|
1075
|
+
/** 项目根目录 */
|
|
1076
|
+
rootDir: string;
|
|
1077
|
+
/** 启动 HTTP server,打印路由表,执行 onReady 钩子 */
|
|
1078
|
+
listen(port?: number): Promise<Server>;
|
|
1079
|
+
/** 关闭 server,执行 onClose 钩子 */
|
|
1080
|
+
close(): Promise<void>;
|
|
1081
|
+
/**
|
|
1082
|
+
* 无服务器测试注入
|
|
1083
|
+
*
|
|
1084
|
+
* 构建一个模拟请求直接走完整请求链路,不绑定端口。
|
|
1085
|
+
* 需要在 listen() 之前调用(server 未启动时)。
|
|
1086
|
+
*/
|
|
1087
|
+
inject(options?: InjectOptions): Promise<InjectResponse>;
|
|
1088
|
+
}
|
|
810
1089
|
|
|
1090
|
+
/** dev 应用接口(AppBase + reloadRoutes 热替换) */
|
|
1091
|
+
interface DevApp extends AppBase {
|
|
1092
|
+
/** 重新水合路由清单 + 清 schema 缓存 + 更新 server 路由引用(dev 热替换用) */
|
|
1093
|
+
reloadRoutes(): Promise<void>;
|
|
1094
|
+
}
|
|
811
1095
|
/**
|
|
812
|
-
*
|
|
1096
|
+
* dev 模式应用启动 API
|
|
813
1097
|
*
|
|
814
|
-
*
|
|
815
|
-
* 1. 指定的 configPath
|
|
816
|
-
* 2. faapi.config.ts / faapi.config.js(基础配置)
|
|
817
|
-
* 3. faapi.config.{env}.ts / faapi.config.{env}.js(环境覆盖,深度合并)
|
|
1098
|
+
* 在 createAppBase(共享逻辑)基础上增加 `reloadRoutes` 热替换能力,供 `faapi dev` watcher 调用。
|
|
818
1099
|
*
|
|
819
|
-
*
|
|
1100
|
+
* 与 createProdApp 的区别:
|
|
1101
|
+
* - dev:含 reloadRoutes(重新扫描路由 + 重新生成 schema + 清缓存 + 更新 server 路由引用)
|
|
1102
|
+
* - prod:精简,无 reloadRoutes(产物已固化,运行时不重建)
|
|
820
1103
|
*
|
|
821
|
-
*
|
|
822
|
-
*
|
|
823
|
-
* @
|
|
1104
|
+
* 由 `devCommand` 直接调用,devCommand 持有 app 引用并传给 watcher。
|
|
1105
|
+
*
|
|
1106
|
+
* @example
|
|
1107
|
+
* ```ts
|
|
1108
|
+
* // devCommand 内部
|
|
1109
|
+
* const app = await createDevApp();
|
|
1110
|
+
* await app.listen();
|
|
1111
|
+
* startWatcher({ rootDir, appDir, app });
|
|
1112
|
+
* ```
|
|
1113
|
+
*/
|
|
1114
|
+
declare function createDevApp(options?: CreateAppOptions): Promise<DevApp>;
|
|
1115
|
+
|
|
1116
|
+
/** prod 应用接口(AppBase,无 reloadRoutes) */
|
|
1117
|
+
type ProdApp = AppBase;
|
|
1118
|
+
|
|
1119
|
+
/**
|
|
1120
|
+
* prod 模式应用启动 API
|
|
1121
|
+
*
|
|
1122
|
+
* 直接返回 createAppBase 结果(共享逻辑),不含 dev 专用能力(reloadRoutes、缓存失效)。
|
|
1123
|
+
* 产物在 `faapi build` 阶段已固化,运行时不重建。
|
|
1124
|
+
*
|
|
1125
|
+
* 框架采用零入口设计——用户无需编写 main.ts:
|
|
1126
|
+
* - `faapi build` 自动生成 `dist/main.js` 启动入口,内部调用 `createProdApp()` + `listen()` 启动生产服务器
|
|
1127
|
+
* - 用户自定义启动逻辑通过 `faapi.config.ts` 的 `lifecycle.onReady` / `onClose` 钩子实现
|
|
1128
|
+
*
|
|
1129
|
+
* 编程式调用场景(如自定义 CLI 启动器)也可直接调用:
|
|
1130
|
+
*
|
|
1131
|
+
* @example
|
|
1132
|
+
* ```ts
|
|
1133
|
+
* import { createProdApp } from '@faapi/faapi';
|
|
1134
|
+
* const app = await createProdApp();
|
|
1135
|
+
* await app.listen();
|
|
1136
|
+
* ```
|
|
824
1137
|
*/
|
|
825
|
-
declare function
|
|
1138
|
+
declare function createProdApp(options?: CreateAppOptions): Promise<ProdApp>;
|
|
826
1139
|
|
|
827
|
-
export { type CorsOptions, type
|
|
1140
|
+
export { type ProdApp as App, type CorsOptions, type CreateAppOptions, type DevApp, type FaapiConfig, type FaapiContext, type FaapiContextConfig, FaapiError, type FaapiMiddleware, type FaapiPlugin, type HandlerTypeInfo, type HelmetOptions, type InjectOptions, type InjectResponse, type Injector, type InjectorMap, InternalError, type LifecycleContext, type LifecycleHooks, type LoggerOptions, MethodNotAllowedError, ModuleLoadError, type PluginContext, type PluginDeclaration, type ProdApp, type PropertyType, type RequestHandler, type RouteInfo, type RouteInputSchema, type RouteManifest, RouteNotFoundError, type RouteOutputSchema, type RouteParamSchema, type RouteSchemaSource, type RuntimeType, SchemaExtractionError, type SseEvent, type SseWriter, type TypeConstraint, type UpgradeHandler, ValidationError, type ValidationErrorCode, type ValidationIssue, type WsContext, type WsEventHandlers, type WsHandler, type WsSocket, collectRouteSchemaSources, cors, createProdApp as createApp, createDevApp, createProdApp, createProgram, extractTypeInfo, getInputTypeForMethod, helmet, invalidateProgramCache, loadConfig, logger, resolveTypeNode };
|