@faapi/faapi 1.2.0 → 1.3.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.
- package/dist/cli/index.js +117 -76
- package/dist/cli/index.js.map +1 -1
- package/dist/index.d.ts +128 -1
- package/dist/index.js +318 -274
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -87,6 +87,31 @@ interface CookieOptions {
|
|
|
87
87
|
secure?: boolean;
|
|
88
88
|
sameSite?: 'Strict' | 'Lax' | 'None';
|
|
89
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* ctx.fail() 的参数类型(对象形式,status 和 code 均可省略)
|
|
92
|
+
*
|
|
93
|
+
* - status: HTTP 状态码(可选,省略时默认 500)
|
|
94
|
+
* - code: 业务错误码(可选,省略时响应 body 里不含 code 字段)
|
|
95
|
+
* - message: 人类可读错误描述(必填)
|
|
96
|
+
*
|
|
97
|
+
* status 和 code 是两个独立维度,无关联:
|
|
98
|
+
* - status 控制 HTTP 状态码
|
|
99
|
+
* - code 是 body 里的业务错误码字段
|
|
100
|
+
*
|
|
101
|
+
* ```ts
|
|
102
|
+
* ctx.fail({ message: '出错' }) // HTTP 500, { error: { message: '出错' } }
|
|
103
|
+
* ctx.fail({ status: 404, message: '用户不存在' }) // HTTP 404, { error: { message: '用户不存在' } }
|
|
104
|
+
* ctx.fail({ status: 404, code: 'USER_NOT_FOUND', message: '用户不存在' }) // HTTP 404, { error: { code: 'USER_NOT_FOUND', message: '用户不存在' } }
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
interface FailOptions {
|
|
108
|
+
/** HTTP 状态码(可选,省略时默认 500) */
|
|
109
|
+
status?: number;
|
|
110
|
+
/** 业务错误码(可选,省略时响应 body 里不含 code 字段) */
|
|
111
|
+
code?: string;
|
|
112
|
+
/** 人类可读错误描述 */
|
|
113
|
+
message: string;
|
|
114
|
+
}
|
|
90
115
|
/**
|
|
91
116
|
* ctx.config 的类型:用户自定义业务配置
|
|
92
117
|
*
|
|
@@ -192,6 +217,41 @@ interface FaapiContext {
|
|
|
192
217
|
* ```
|
|
193
218
|
*/
|
|
194
219
|
sse(): SseWriter;
|
|
220
|
+
/**
|
|
221
|
+
* 显式包装成功响应(返回 Response 对象)
|
|
222
|
+
*
|
|
223
|
+
* 用 config.response.ok 包裹 data 并返回 Response。
|
|
224
|
+
* 等价于 handler 直接 `return data`(框架自动包裹),但显式调用语义更清晰。
|
|
225
|
+
*
|
|
226
|
+
* 返回 Response 对象,不会被框架自动包裹再次包装(避免双重包裹)。
|
|
227
|
+
*
|
|
228
|
+
* ```ts
|
|
229
|
+
* // 以下两种写法等价(假设配置了 response.ok = (data) => ({ data })):
|
|
230
|
+
* export function GET() {
|
|
231
|
+
* return { id: 1 }; // 自动包裹 → { data: { id: 1 } }
|
|
232
|
+
* }
|
|
233
|
+
* export function GET2(ctx) {
|
|
234
|
+
* return ctx.ok({ id: 1 }); // 显式包裹 → { data: { id: 1 } }
|
|
235
|
+
* }
|
|
236
|
+
* ```
|
|
237
|
+
*/
|
|
238
|
+
ok(data: unknown): Response;
|
|
239
|
+
/**
|
|
240
|
+
* 返回错误响应(对象形式参数,status 和 code 均可省略)
|
|
241
|
+
*
|
|
242
|
+
* @param options.status HTTP 状态码(可选,省略时默认 500)
|
|
243
|
+
* @param options.code 业务错误码(可选,省略时响应 body 里不含 code 字段)
|
|
244
|
+
* @param options.message 人类可读错误描述(必填)
|
|
245
|
+
*
|
|
246
|
+
* status 和 code 独立无关联:status 控制 HTTP 状态码,code 是 body 里的业务错误码字段。
|
|
247
|
+
*
|
|
248
|
+
* ```ts
|
|
249
|
+
* return ctx.fail({ message: '出错' }); // HTTP 500, { error: { message: '出错' } }
|
|
250
|
+
* return ctx.fail({ status: 404, message: '用户不存在' }); // HTTP 404, { error: { message: '用户不存在' } }
|
|
251
|
+
* return ctx.fail({ status: 404, code: 'USER_NOT_FOUND', message: '用户不存在' }); // HTTP 404, { error: { code: 'USER_NOT_FOUND', message: '用户不存在' } }
|
|
252
|
+
* ```
|
|
253
|
+
*/
|
|
254
|
+
fail(options: FailOptions): Response;
|
|
195
255
|
/**
|
|
196
256
|
* 读取 cookie 值
|
|
197
257
|
*/
|
|
@@ -562,6 +622,50 @@ interface LifecycleContext {
|
|
|
562
622
|
/** 服务器实例 */
|
|
563
623
|
server: node_http.Server;
|
|
564
624
|
}
|
|
625
|
+
/**
|
|
626
|
+
* 统一响应包装配置
|
|
627
|
+
*
|
|
628
|
+
* 配置后,框架自动:
|
|
629
|
+
* - 成功响应:handler return 非 Response 的值时,用 ok 函数包裹
|
|
630
|
+
* - 错误响应:ctx.fail() 用 fail 函数包装 body
|
|
631
|
+
*
|
|
632
|
+
* 未配置 response 时,使用框架默认实现:
|
|
633
|
+
* - ok: (data) => ({ data })
|
|
634
|
+
* - fail: ({ status, code, message }) => 省略的字段不放入 error 对象
|
|
635
|
+
*
|
|
636
|
+
* ```ts
|
|
637
|
+
* import type { FaapiConfig } from '@faapi/faapi';
|
|
638
|
+
* export default {
|
|
639
|
+
* response: {
|
|
640
|
+
* // 自定义成功包装(默认 { data })
|
|
641
|
+
* ok: (data) => ({ code: 0, data }),
|
|
642
|
+
* // 自定义错误包装(默认 { error: { message, ...code?, ...status? } })
|
|
643
|
+
* fail: ({ status, code, message }) => ({ error: { code, message } }),
|
|
644
|
+
* },
|
|
645
|
+
* } satisfies FaapiConfig;
|
|
646
|
+
* ```
|
|
647
|
+
*/
|
|
648
|
+
interface ResponseConfig {
|
|
649
|
+
/**
|
|
650
|
+
* 成功响应包装函数
|
|
651
|
+
*
|
|
652
|
+
* handler return 非 Response 的值时调用。
|
|
653
|
+
* 默认: (data) => ({ data })
|
|
654
|
+
*/
|
|
655
|
+
ok?: (data: unknown) => unknown;
|
|
656
|
+
/**
|
|
657
|
+
* 错误响应包装函数
|
|
658
|
+
*
|
|
659
|
+
* ctx.fail() 调用时使用,接收 { status?, code?, message }。
|
|
660
|
+
* status 和 code 均可能为 undefined(用户调用 ctx.fail 时省略则不传),
|
|
661
|
+
* 默认实现只把非 undefined 的字段放入 error 对象。
|
|
662
|
+
*/
|
|
663
|
+
fail?: (error: {
|
|
664
|
+
status?: number;
|
|
665
|
+
code?: string;
|
|
666
|
+
message: string;
|
|
667
|
+
}) => unknown;
|
|
668
|
+
}
|
|
565
669
|
/**
|
|
566
670
|
* faapi 配置文件类型
|
|
567
671
|
*
|
|
@@ -604,6 +708,29 @@ interface FaapiConfig {
|
|
|
604
708
|
logger?: LoggerOptions | boolean;
|
|
605
709
|
/** HTTP/2 配置,false 禁用(默认 http/1.1) */
|
|
606
710
|
http2?: Http2Options | boolean;
|
|
711
|
+
/**
|
|
712
|
+
* 统一响应包装配置
|
|
713
|
+
*
|
|
714
|
+
* 配置后,框架自动包裹 handler 返回值:
|
|
715
|
+
* - 成功响应:handler return 非 Response → 用 ok 函数包裹(默认 `{ data }`)
|
|
716
|
+
* - 错误响应:ctx.fail() 用 fail 函数包装(默认 `{ error: { message, ...code? } }`)
|
|
717
|
+
*
|
|
718
|
+
* 未配置 response 时,使用框架默认实现(见 ResponseConfig)。
|
|
719
|
+
* 配置 response 后,ok/fail 各字段均可选,按需覆盖。
|
|
720
|
+
*
|
|
721
|
+
* ```ts
|
|
722
|
+
* import type { FaapiConfig } from '@faapi/faapi';
|
|
723
|
+
* export default {
|
|
724
|
+
* response: {
|
|
725
|
+
* ok: (data) => ({ code: 0, data }),
|
|
726
|
+
* fail: ({ status, code, message }) => ({ error: { code, message } }),
|
|
727
|
+
* },
|
|
728
|
+
* } satisfies FaapiConfig;
|
|
729
|
+
* ```
|
|
730
|
+
*
|
|
731
|
+
* 详见 `src/config/configTypes.md` 统一响应包装章节。
|
|
732
|
+
*/
|
|
733
|
+
response?: ResponseConfig;
|
|
607
734
|
/**
|
|
608
735
|
* 全局中间件:对所有路由(HTTP + WebSocket 握手)生效
|
|
609
736
|
*
|
|
@@ -1405,4 +1532,4 @@ type ProdApp = AppBase;
|
|
|
1405
1532
|
*/
|
|
1406
1533
|
declare function createProdApp(options?: CreateAppOptions): Promise<ProdApp>;
|
|
1407
1534
|
|
|
1408
|
-
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, MessageQueue, 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 TestServer, type TestServerOptions, type TypeConstraint, type UpgradeHandler, ValidationError, type ValidationErrorCode, type ValidationIssue, type WsContext, type WsEventHandlers, type WsHandler, type WsSocket, type WsTestClient, type WsTestClientOptions, collectRouteSchemaSources, connectWs, cors, createProdApp as createApp, createContext, createDevApp, createProdApp, createProgram, createTestServer, extractTypeInfo, getInputTypeForMethod, helmet, invalidateProgramCache, invokeHandler, loadConfig, loadEnv, logger, resolveTypeNode, waitForWsOpen };
|
|
1535
|
+
export { type ProdApp as App, type CorsOptions, type CreateAppOptions, type DevApp, type FaapiConfig, type FaapiContext, type FaapiContextConfig, FaapiError, type FaapiMiddleware, type FaapiPlugin, type FailOptions, type HandlerTypeInfo, type HelmetOptions, type InjectOptions, type InjectResponse, type Injector, type InjectorMap, InternalError, type LifecycleContext, type LifecycleHooks, type LoggerOptions, MessageQueue, MethodNotAllowedError, ModuleLoadError, type PluginContext, type PluginDeclaration, type ProdApp, type PropertyType, type RequestHandler, type ResponseConfig, type RouteInfo, type RouteInputSchema, type RouteManifest, RouteNotFoundError, type RouteOutputSchema, type RouteParamSchema, type RouteSchemaSource, type RuntimeType, SchemaExtractionError, type SseEvent, type SseWriter, type TestServer, type TestServerOptions, type TypeConstraint, type UpgradeHandler, ValidationError, type ValidationErrorCode, type ValidationIssue, type WsContext, type WsEventHandlers, type WsHandler, type WsSocket, type WsTestClient, type WsTestClientOptions, collectRouteSchemaSources, connectWs, cors, createProdApp as createApp, createContext, createDevApp, createProdApp, createProgram, createTestServer, extractTypeInfo, getInputTypeForMethod, helmet, invalidateProgramCache, invokeHandler, loadConfig, loadEnv, logger, resolveTypeNode, waitForWsOpen };
|