@faapi/faapi 0.0.0-canary.f5b23c6 → 1.0.0-canary.4f644f8
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/cli/index.js +314 -270
- package/dist/cli/index.js.map +1 -1
- package/dist/index.d.ts +69 -11
- package/dist/index.js +554 -465
- package/dist/index.js.map +1 -1
- package/package.json +7 -4
package/dist/index.d.ts
CHANGED
|
@@ -54,6 +54,17 @@ interface SseEvent {
|
|
|
54
54
|
interface SseWriter {
|
|
55
55
|
/** 推送一个 SSE 事件 */
|
|
56
56
|
send(event: SseEvent): void;
|
|
57
|
+
/**
|
|
58
|
+
* 直接写入原始字节/字符串,不做任何 SSE 序列化
|
|
59
|
+
*
|
|
60
|
+
* 用于透传上游已有的 SSE 原文(如 LLM 中转平台逐 chunk 转发 OpenAI 响应)。
|
|
61
|
+
* 调用方负责保证内容符合 HTML5 SSE 规范;`send` 会再次加 `data: ` 前缀,
|
|
62
|
+
* 不适用于原文透传场景。
|
|
63
|
+
*
|
|
64
|
+
* 接受 string 或 Uint8Array(Buffer 是 Uint8Array 子类,自然兼容)。
|
|
65
|
+
* 与 `send` 一致:close/aborted 后静默忽略,不抛错。
|
|
66
|
+
*/
|
|
67
|
+
sendRaw(chunk: string | Uint8Array): void;
|
|
57
68
|
/** 推送一个 error 事件并关闭流(用于流式输出中报错的优雅终止) */
|
|
58
69
|
sendError(error: unknown): void;
|
|
59
70
|
/** 关闭流(多次调用安全) */
|
|
@@ -550,21 +561,23 @@ interface LifecycleContext {
|
|
|
550
561
|
* } satisfies FaapiConfig;
|
|
551
562
|
* ```
|
|
552
563
|
*
|
|
553
|
-
*
|
|
564
|
+
* 自定义业务配置(任意 key):
|
|
554
565
|
* ```ts
|
|
555
566
|
* import type { FaapiConfig } from '@faapi/faapi';
|
|
556
567
|
* export default {
|
|
557
568
|
* cors: { origin: '*' },
|
|
558
|
-
* //
|
|
559
|
-
* db: { host: 'localhost', port: 5432 },
|
|
569
|
+
* // 通过 process.env.XXX 读取 .env 文件加载的环境变量
|
|
570
|
+
* db: { host: process.env.DB_HOST ?? 'localhost', port: 5432 },
|
|
560
571
|
* } satisfies FaapiConfig;
|
|
561
572
|
* ```
|
|
562
573
|
*
|
|
563
|
-
*
|
|
574
|
+
* 多环境差异通过 `.env` 系列文件实现(见 `loadEnv`):
|
|
575
|
+
* - `.env` / `.env.local` / `.env.{env}` / `.env.{env}.local`
|
|
576
|
+
* - 环境由 `NODE_ENV > 'development'` 决定
|
|
564
577
|
*
|
|
565
578
|
* 框架元信息通过环境变量配置(不放在 config 内):
|
|
566
579
|
* - `PORT`:服务端口,默认 3000
|
|
567
|
-
* - `FAAPI_DIST
|
|
580
|
+
* - `FAAPI_DIST`:产物输出目录,dev 固定为 `.faapi`(不可修改),prod 默认为 `dist`(可通过 `--dist` 修改)
|
|
568
581
|
*/
|
|
569
582
|
interface FaapiConfig {
|
|
570
583
|
/** CORS 配置,false 禁用 */
|
|
@@ -995,21 +1008,42 @@ declare function collectRouteSchemaSources(routes: RouteManifest, rootDir?: stri
|
|
|
995
1008
|
* 加载 faapi 配置文件
|
|
996
1009
|
*
|
|
997
1010
|
* 统一读取 `<dist>/faapi-config.js` 产物:
|
|
998
|
-
* - dev 模式:`faapi dev` 启动时由 `compileConfig` 生成 `.faapi/
|
|
999
|
-
* - prod 模式:`faapi build` 时由 `compileConfig` 生成
|
|
1011
|
+
* - dev 模式:`faapi dev` 启动时由 `compileConfig` 生成 `.faapi/faapi-config.js`
|
|
1012
|
+
* - prod 模式:`faapi build` 时由 `compileConfig` 生成 `dist/faapi-config.js`
|
|
1000
1013
|
*
|
|
1001
|
-
* 产物由 `compileConfig`
|
|
1014
|
+
* 产物由 `compileConfig` 在构建阶段编译,运行时不读源码、不现场编译。
|
|
1015
|
+
* 环境变量由 `loadEnv` 从 `.env` 系列文件加载到 `process.env`,配置文件中通过 `process.env.XXX` 读取。
|
|
1002
1016
|
*
|
|
1003
1017
|
* - 产物存在 → import 并返回 default
|
|
1004
1018
|
* - 产物不存在但源码有配置文件 → 抛错(强制 rebuild)
|
|
1005
1019
|
* - 源码也无配置文件 → 返回 `null`(配置可选)
|
|
1006
1020
|
*
|
|
1007
1021
|
* @param rootDir 项目根目录
|
|
1008
|
-
* @param dist 产物目录(如 '
|
|
1022
|
+
* @param dist 产物目录(如 'dist' 或 '.faapi')
|
|
1009
1023
|
* @returns 配置对象,无配置文件时返回 null
|
|
1010
1024
|
*/
|
|
1011
1025
|
declare function loadConfig(rootDir: string, dist: string): Promise<Partial<FaapiConfig> | null>;
|
|
1012
1026
|
|
|
1027
|
+
/**
|
|
1028
|
+
* 加载 `.env` 系列文件到 `process.env`
|
|
1029
|
+
*
|
|
1030
|
+
* 按 Next.js 约定加载四级文件(从低到高):
|
|
1031
|
+
* 1. `.env` — 所有环境共享
|
|
1032
|
+
* 2. `.env.local` — 本地覆盖
|
|
1033
|
+
* 3. `.env.{env}` — 按环境覆盖
|
|
1034
|
+
* 4. `.env.{env}.local` — 按环境本地覆盖
|
|
1035
|
+
*
|
|
1036
|
+
* env 由 `NODE_ENV || 'development'` 决定。调用方应在调 loadEnv 之前自行兜底 NODE_ENV
|
|
1037
|
+
* (dev 设 'development',prod 设 'production')。
|
|
1038
|
+
*
|
|
1039
|
+
* 合并规则:
|
|
1040
|
+
* - 后加载的文件覆盖先加载的同名变量
|
|
1041
|
+
* - **shell 已设置的变量不被覆盖**(`process.env` 已有的值优先)
|
|
1042
|
+
*
|
|
1043
|
+
* @param rootDir 项目根目录(`.env` 文件所在目录)
|
|
1044
|
+
*/
|
|
1045
|
+
declare function loadEnv(rootDir: string): void;
|
|
1046
|
+
|
|
1013
1047
|
declare const VALIDATION_ERROR = "VALIDATION_ERROR";
|
|
1014
1048
|
declare const ROUTE_NOT_FOUND = "ROUTE_NOT_FOUND";
|
|
1015
1049
|
declare const METHOD_NOT_ALLOWED = "METHOD_NOT_ALLOWED";
|
|
@@ -1064,6 +1098,30 @@ interface ValidationIssue {
|
|
|
1064
1098
|
}
|
|
1065
1099
|
type ValidationErrorCode = 'TYPE_MISMATCH' | 'MISSING_FIELD' | 'INVALID_FORMAT' | 'INVALID_VALUE' | 'COERCE_FAILED';
|
|
1066
1100
|
|
|
1101
|
+
/**
|
|
1102
|
+
* 从 Request 对象创建 FaapiContext
|
|
1103
|
+
* @param request Web Request 对象
|
|
1104
|
+
* @param params 动态路由参数
|
|
1105
|
+
* @param config 自定义业务配置(来自 faapi.config.ts)
|
|
1106
|
+
* @param ip 客户端 IP(由调用方从 IncomingMessage 提取,HTTP/WS 握手均通过 utils/getClientIp)
|
|
1107
|
+
*/
|
|
1108
|
+
declare function createContext(request: Request, params: Record<string, string>, config?: Record<string, unknown>, ip?: string): FaapiContext;
|
|
1109
|
+
|
|
1110
|
+
/**
|
|
1111
|
+
* 调用路由 handler 并将返回值转为 Response
|
|
1112
|
+
*
|
|
1113
|
+
* 流程(洋葱模型):
|
|
1114
|
+
* 1. 中间件按洋葱模型执行:mw1.before → mw2.before → ... → handler → ... → mw2.after → mw1.after
|
|
1115
|
+
* 2. 中间件不调用 next() 即拦截请求(必须返回 Response)
|
|
1116
|
+
* 3. 中间件可用 try/catch 捕获内层错误
|
|
1117
|
+
* 4. 最内层执行注入器(按需)→ handler
|
|
1118
|
+
*
|
|
1119
|
+
* 注入器与中间件解耦:
|
|
1120
|
+
* - 注入器按 handler 参数名匹配,只执行需要的
|
|
1121
|
+
* - 注入器可读取中间件塞进 ctx 的值
|
|
1122
|
+
*/
|
|
1123
|
+
declare function invokeHandler(handler: (...args: unknown[]) => unknown, ctx: FaapiContext, body?: unknown, middlewares?: FaapiMiddleware[], injectors?: InjectorMap): Promise<Response>;
|
|
1124
|
+
|
|
1067
1125
|
interface InjectOptions {
|
|
1068
1126
|
method?: string;
|
|
1069
1127
|
path?: string;
|
|
@@ -1079,7 +1137,7 @@ interface InjectResponse {
|
|
|
1079
1137
|
interface CreateAppOptions {
|
|
1080
1138
|
/** 项目根目录,默认 process.cwd() */
|
|
1081
1139
|
rootDir?: string;
|
|
1082
|
-
/**
|
|
1140
|
+
/** 产物输出目录(如 dist 或 .faapi),覆盖环境变量 FAAPI_DIST,默认 'dist' */
|
|
1083
1141
|
dist?: string;
|
|
1084
1142
|
/** 端口号,也可在 listen() 时传入;默认环境变量 PORT 或 3000 */
|
|
1085
1143
|
port?: number;
|
|
@@ -1157,4 +1215,4 @@ type ProdApp = AppBase;
|
|
|
1157
1215
|
*/
|
|
1158
1216
|
declare function createProdApp(options?: CreateAppOptions): Promise<ProdApp>;
|
|
1159
1217
|
|
|
1160
|
-
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 };
|
|
1218
|
+
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, createContext, createDevApp, createProdApp, createProgram, extractTypeInfo, getInputTypeForMethod, helmet, invalidateProgramCache, invokeHandler, loadConfig, loadEnv, logger, resolveTypeNode };
|