@faapi/faapi 0.0.0-canary.0e994fe → 0.0.0-canary.22b65a2

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/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
- /** 自定义日志输出函数,默认 console.log */
269
- log?: (message: string) => void;
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
- * 日志格式:GET /api/users 200 12ms
275
- * 错误格式:POST /api/users 400 45ms - Error: ...
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
  *
@@ -416,6 +482,11 @@ type PluginDeclaration = string | [string, unknown] | {
416
482
  options?: unknown;
417
483
  };
418
484
 
485
+ interface Http2Options {
486
+ key?: string;
487
+ cert?: string;
488
+ }
489
+
419
490
  /**
420
491
  * 统一响应格式化函数
421
492
  *
@@ -434,7 +505,7 @@ type ErrorFormatFn = (error: unknown, ctx?: FaapiContext) => Response | null | u
434
505
  * 生命周期钩子
435
506
  */
436
507
  interface LifecycleHooks {
437
- /** 路由加载完成、服务器启动前调用(适合初始化数据库连接等) */
508
+ /** 服务器启动后调用(适合初始化数据库连接等) */
438
509
  onReady?: (ctx: LifecycleContext) => Promise<void> | void;
439
510
  /** 服务器关闭时调用(适合清理资源、优雅关闭) */
440
511
  onClose?: (ctx: LifecycleContext) => Promise<void> | void;
@@ -471,7 +542,6 @@ interface LifecycleContext {
471
542
  * ```ts
472
543
  * import type { FaapiConfig } from '@faapi/faapi';
473
544
  * export default {
474
- * port: 3000,
475
545
  * cors: { origin: '*' },
476
546
  * } satisfies FaapiConfig;
477
547
  * ```
@@ -480,7 +550,6 @@ interface LifecycleContext {
480
550
  * ```ts
481
551
  * import type { FaapiConfig } from '@faapi/faapi';
482
552
  * export default {
483
- * port: 3000,
484
553
  * cors: { origin: '*' },
485
554
  * // 自定义业务配置(任意 key)
486
555
  * db: { host: 'localhost', port: 5432 },
@@ -488,12 +557,13 @@ interface LifecycleContext {
488
557
  * ```
489
558
  *
490
559
  * 环境覆盖通过 faapi.config.{NODE_ENV}.ts 实现(如 faapi.config.production.ts)
560
+ *
561
+ * 框架元信息通过环境变量配置(不放在 config 内):
562
+ * - `FAAPI_APP_DIR`:源码目录前缀,默认 'src',设为 '.' 表示源码在项目根目录
563
+ * - `PORT`:服务端口,默认 3000
564
+ * - `FAAPI_OUT_DIR`:产物输出目录,dev 固定为 '.faapi/dev',prod 默认 'dist'
491
565
  */
492
566
  interface FaapiConfig {
493
- /** 服务端口,默认 3000(可被 --port / PORT 环境变量覆盖) */
494
- port?: number;
495
- /** 静态文件目录 */
496
- staticDir?: string;
497
567
  /** CORS 配置,false 禁用 */
498
568
  cors?: CorsOptions | boolean;
499
569
  /** 统一响应格式化函数 */
@@ -502,6 +572,14 @@ interface FaapiConfig {
502
572
  errorFormat?: ErrorFormatFn;
503
573
  /** 生命周期钩子 */
504
574
  lifecycle?: LifecycleHooks;
575
+ /** 安全头配置,false 禁用 */
576
+ helmet?: HelmetOptions | boolean;
577
+ /** 请求体大小限制(字节),默认 10MB(10 * 1024 * 1024) */
578
+ bodyLimit?: number;
579
+ /** 日志中间件配置 */
580
+ logger?: LoggerOptions | boolean;
581
+ /** HTTP/2 配置,false 禁用(默认 http/1.1) */
582
+ http2?: Http2Options | boolean;
505
583
  /**
506
584
  * 全局中间件:对所有路由(HTTP + WebSocket 握手)生效
507
585
  *
@@ -664,6 +742,15 @@ interface WsContext {
664
742
  */
665
743
  type WsHandler = (ctx: WsContext) => WsEventHandlers | void;
666
744
 
745
+ /**
746
+ * 清理所有 Program 缓存(watch 模式下文件变化时调用)
747
+ *
748
+ * 全量清理而非增量清理,理由:
749
+ * - 简单可靠,无状态一致性问题
750
+ * - 跨文件类型引用需要所有文件的 Program 同步更新
751
+ * - dev 模式文件量有限,全量重建在百毫秒级
752
+ */
753
+ declare function invalidateProgramCache(): void;
667
754
  /**
668
755
  * 为指定文件创建 TypeScript Program(带缓存)
669
756
  *
@@ -780,48 +867,150 @@ declare function extractTypeInfo(program: ts.Program, filePath: string, typeName
780
867
  declare function getInputTypeForMethod(method: string): 'query' | 'body';
781
868
 
782
869
  /**
783
- * 单个参数的 schema 描述(简化版,供扩展包消费)
870
+ * 单个路由的 schema 提取结果
871
+ *
872
+ * key 使用 urlPath(如 '/api/hello')而非 filePath,因为 urlPath 在 dev/prod 完全一致,
873
+ * 无需 remapManifestKeys 桥接 .ts/.js 路径差异。
784
874
  */
785
- interface SchemaPropertyDescriptor {
786
- name: string;
787
- type: string;
788
- required: boolean;
875
+ interface RouteSchemaSource {
876
+ /** 路由 URL 路径(如 '/api/hello'),作为 schema key */
877
+ urlPath: string;
878
+ /** 源文件绝对路径(用于 generateSchemaFiles 按文件分组生成 zod.js) */
879
+ filePath: string;
880
+ schemaName: string;
881
+ typeInfo: HandlerTypeInfo | null;
789
882
  }
790
883
  /**
791
- * 单个输入源的 schema 描述
884
+ * 从路由清单收集 schema 提取所需的原始数据
885
+ *
886
+ * dev 和 prd 共享的核心提取流程:
887
+ * 1. 按文件分组遍历路由
888
+ * 2. 对每个文件 createProgram + extractAllTypes 收集所有类型
889
+ * 3. 用 analyzeInjection + extractTypeInfo 提取每个路由的 schema 类型
890
+ * 4. 同时返回按文件分组的 allTypesMap 和合并后的全局 allTypes
891
+ *
892
+ * 调用方基于返回的 sources 和 allTypes 各自做最终转换:
893
+ * - dev:生成 JS 模块文件 → import 加载(用 allTypesByFile)
894
+ * - prd:生成 JS 模块代码 → SchemaModuleEntry[](用 allTypesByFile)
792
895
  */
793
- interface InputSchemaDescriptor {
794
- /** 输入源类型名(如 'Query'、'CreateUserBody'),无类型声明时 null */
795
- schemaName: string | null;
796
- /** 参数列表 */
797
- properties: SchemaPropertyDescriptor[];
896
+ declare function collectRouteSchemaSources(routes: RouteManifest, rootDir?: string): {
897
+ sources: RouteSchemaSource[];
898
+ /** 按文件分组的类型映射(prd writeSchemaModule 用) */
899
+ allTypesByFile: Map<string, Map<string, HandlerTypeInfo>>;
900
+ /** 合并后的全局类型映射(兼容旧调用方保留,新路径使用 allTypesByFile) */
901
+ mergedAllTypes: Map<string, HandlerTypeInfo>;
902
+ };
903
+
904
+ /**
905
+ * 加载 faapi 配置文件
906
+ *
907
+ * 统一读取 `<outDir>/faapi-config.js` 产物:
908
+ * - dev 模式:`faapi dev` 启动时由 `compileConfig` 生成 `.faapi/dev/faapi-config.js`
909
+ * - prod 模式:`faapi build` 时由 `compileConfig` 生成 `dist/faapi-config.js`
910
+ *
911
+ * 产物由 `compileConfig` 在构建阶段合并 env 后固化,运行时不读源码、不现场编译、不按 env 合并。
912
+ *
913
+ * - 产物存在 → import 并返回 default
914
+ * - 产物不存在但源码有配置文件 → 抛错(强制 rebuild)
915
+ * - 源码也无配置文件 → 返回 `null`(配置可选)
916
+ *
917
+ * @param rootDir 项目根目录
918
+ * @param outDir 产物目录(如 'dist' 或 '.faapi/dev')
919
+ * @returns 配置对象,无配置文件时返回 null
920
+ */
921
+ declare function loadConfig(rootDir: string, outDir: string): Promise<Partial<FaapiConfig> | null>;
922
+
923
+ interface InjectOptions {
924
+ method?: string;
925
+ path?: string;
926
+ headers?: Record<string, string>;
927
+ query?: Record<string, string>;
928
+ body?: unknown;
929
+ }
930
+ interface InjectResponse {
931
+ status: number;
932
+ headers: Headers;
933
+ body: unknown;
934
+ }
935
+ interface CreateAppOptions {
936
+ /** 项目根目录,默认 process.cwd() */
937
+ rootDir?: string;
938
+ /** 源码目录前缀,覆盖环境变量 FAAPI_APP_DIR,默认 'src' */
939
+ appDir?: string;
940
+ /** 端口号,也可在 listen() 时传入;默认环境变量 PORT 或 3000 */
941
+ port?: number;
942
+ }
943
+ /** 应用基础接口(dev/prod 共用,不含 reloadRoutes) */
944
+ interface AppBase {
945
+ /** Node.js Server 实例(listen 后可用,close 后置 null) */
946
+ server: Server | null;
947
+ /** 排序后的路由清单 */
948
+ routes: RouteManifest;
949
+ /** WebSocket 路由清单 */
950
+ wsRoutes: WsRouteManifest;
951
+ /** 项目根目录 */
952
+ rootDir: string;
953
+ /** 启动 HTTP server,打印路由表,执行 onReady 钩子 */
954
+ listen(port?: number): Promise<Server>;
955
+ /** 关闭 server,执行 onClose 钩子 */
956
+ close(): Promise<void>;
957
+ /**
958
+ * 无服务器测试注入
959
+ *
960
+ * 构建一个模拟请求直接走完整请求链路,不绑定端口。
961
+ * 需要在 listen() 之前调用(server 未启动时)。
962
+ */
963
+ inject(options?: InjectOptions): Promise<InjectResponse>;
964
+ }
965
+
966
+ /** dev 应用接口(AppBase + reloadRoutes 热替换) */
967
+ interface DevApp extends AppBase {
968
+ /** 重新水合路由清单 + 清 schema 缓存 + 更新 server 路由引用(dev 热替换用) */
969
+ reloadRoutes(): Promise<void>;
798
970
  }
799
971
  /**
800
- * 查询指定路由 handler 的输入参数 schema
972
+ * dev 模式应用启动 API
973
+ *
974
+ * 在 createAppBase(共享逻辑)基础上增加 `reloadRoutes` 热替换能力,供 `faapi dev` watcher 调用。
801
975
  *
802
- * 复用 schemaRegistry 已有的类型提取结果,避免重复 AST 分析。
803
- * schema 尚未注册时返回 undefined。
976
+ * createProdApp 的区别:
977
+ * - dev:含 reloadRoutes(重新扫描路由 + 重新生成 schema + 清缓存 + 更新 server 路由引用)
978
+ * - prod:精简,无 reloadRoutes(产物已固化,运行时不重建)
804
979
  *
805
- * @param filePath handler 文件绝对路径
806
- * @param method HTTP 方法(GET/POST 等)
807
- * @param inputType 输入源类型(query/body/params)
980
+ * `devCommand` 直接调用,devCommand 持有 app 引用并传给 watcher。
981
+ *
982
+ * @example
983
+ * ```ts
984
+ * // devCommand 内部
985
+ * const app = await createDevApp();
986
+ * await app.listen();
987
+ * startWatcher({ rootDir, appDir, app });
988
+ * ```
808
989
  */
809
- declare function getSchemaProperties(filePath: string, method: string, inputType: 'query' | 'body' | 'params'): InputSchemaDescriptor | undefined;
990
+ declare function createDevApp(options?: CreateAppOptions): Promise<DevApp>;
991
+
992
+ /** prod 应用接口(AppBase,无 reloadRoutes) */
993
+ type ProdApp = AppBase;
810
994
 
811
995
  /**
812
- * 加载 faapi 配置文件
996
+ * prod 模式应用启动 API
813
997
  *
814
- * 查找顺序:
815
- * 1. 指定的 configPath
816
- * 2. faapi.config.ts / faapi.config.js(基础配置)
817
- * 3. faapi.config.{env}.ts / faapi.config.{env}.js(环境覆盖,深度合并)
998
+ * 直接返回 createAppBase 结果(共享逻辑),不含 dev 专用能力(reloadRoutes、缓存失效)。
999
+ * 产物在 `faapi build` 阶段已固化,运行时不重建。
818
1000
  *
819
- * 环境由 NODE_ENV 或 FAAPI_ENV 决定,默认 'development'
1001
+ * 框架采用零入口设计——用户无需编写 main.ts:
1002
+ * - `faapi build` 自动生成 `dist/main.js` 启动入口,内部调用 `createProdApp()` + `listen()` 启动生产服务器
1003
+ * - 用户自定义启动逻辑通过 `faapi.config.ts` 的 `lifecycle.onReady` / `onClose` 钩子实现
820
1004
  *
821
- * @param rootDir 项目根目录
822
- * @param configPath 指定的配置文件路径(可选)
823
- * @returns 合并后的配置,如果无配置文件则返回 null
1005
+ * 编程式调用场景(如自定义 CLI 启动器)也可直接调用:
1006
+ *
1007
+ * @example
1008
+ * ```ts
1009
+ * import { createProdApp } from '@faapi/faapi';
1010
+ * const app = await createProdApp();
1011
+ * await app.listen();
1012
+ * ```
824
1013
  */
825
- declare function loadConfig(rootDir: string, configPath?: string): Promise<Partial<FaapiConfig> | null>;
1014
+ declare function createProdApp(options?: CreateAppOptions): Promise<ProdApp>;
826
1015
 
827
- export { type CorsOptions, type ErrorFormatFn, type FaapiConfig, type FaapiContext, type FaapiContextConfig, type FaapiMiddleware, type FaapiPlugin, type HandlerTypeInfo, type Injector, type InjectorMap, type InputSchemaDescriptor, type LifecycleContext, type LifecycleHooks, type LoggerOptions, type PluginContext, type PluginDeclaration, type PropertyType, type RequestHandler, type ResponseFormatFn, type RouteInfo, type RouteInputSchema, type RouteManifest, type RouteParamSchema, type RuntimeType, SchemaExtractionError, type SchemaPropertyDescriptor, type SseEvent, type SseWriter, type UpgradeHandler, type WsContext, type WsEventHandlers, type WsHandler, type WsSocket, cors, createProgram, extractTypeInfo, getInputTypeForMethod, getSchemaProperties, loadConfig, logger };
1016
+ export { type ProdApp as App, type CorsOptions, type CreateAppOptions, type DevApp, type ErrorFormatFn, type FaapiConfig, type FaapiContext, type FaapiContextConfig, type FaapiMiddleware, type FaapiPlugin, type HandlerTypeInfo, type HelmetOptions, type InjectOptions, type InjectResponse, type Injector, type InjectorMap, type LifecycleContext, type LifecycleHooks, type LoggerOptions, type PluginContext, type PluginDeclaration, type ProdApp, type PropertyType, type RequestHandler, type ResponseFormatFn, type RouteInfo, type RouteInputSchema, type RouteManifest, type RouteParamSchema, type RouteSchemaSource, type RuntimeType, SchemaExtractionError, type SseEvent, type SseWriter, type UpgradeHandler, type WsContext, type WsEventHandlers, type WsHandler, type WsSocket, collectRouteSchemaSources, cors, createProdApp as createApp, createDevApp, createProdApp, createProgram, extractTypeInfo, getInputTypeForMethod, helmet, invalidateProgramCache, loadConfig, logger };