@faapi/faapi 6.10.1 → 6.11.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 +14 -3
- package/dist/cli/index.js.map +1 -1
- package/dist/index.d.ts +14 -1
- package/dist/index.js +14 -3
- package/dist/index.js.map +1 -1
- package/dist/testing.js +10 -2
- package/dist/testing.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1622,7 +1622,18 @@ interface InjectOptions {
|
|
|
1622
1622
|
interface InjectResponse {
|
|
1623
1623
|
status: number;
|
|
1624
1624
|
headers: Headers;
|
|
1625
|
+
/** 响应字节按 JSON 反序列化的结果(非 JSON 回退字符串)——HTTP 语义,与真实客户端一致 */
|
|
1625
1626
|
body: unknown;
|
|
1627
|
+
/**
|
|
1628
|
+
* handler 原始返回值(自动包裹与 JSON 序列化之前,Date 等富类型不丢)
|
|
1629
|
+
*
|
|
1630
|
+
* 仅当响应体由 handler 数据返回值自动包裹产生时捕获(同进程取数类型保真,
|
|
1631
|
+
* 快路径判断用 `res.raw !== undefined`);handler 返回 Response 形态
|
|
1632
|
+
* (ctx.ok/ctx.fail/ctx.json)、抛错、中间件拦截、SSE、404/405/校验失败等
|
|
1633
|
+
* handler 未产生数据返回值的场景为 undefined。语义详见 createAppCore.md
|
|
1634
|
+
* 「inject 的 raw 字段」节。
|
|
1635
|
+
*/
|
|
1636
|
+
raw?: unknown;
|
|
1626
1637
|
}
|
|
1627
1638
|
/**
|
|
1628
1639
|
* 获取当前 faapi app 单例
|
|
@@ -1682,7 +1693,9 @@ interface AppBase {
|
|
|
1682
1693
|
* 无服务器测试注入
|
|
1683
1694
|
*
|
|
1684
1695
|
* 构建一个模拟请求直接走完整请求链路(CORS / helmet / logger / 全局中间件 / 路由匹配 /
|
|
1685
|
-
* schema 校验 / 目录中间件 / handler),不绑定端口,返回已解析的
|
|
1696
|
+
* schema 校验 / 目录中间件 / handler),不绑定端口,返回已解析的
|
|
1697
|
+
* `{ status, headers, body, raw }`——body 为 JSON 反序列化结果(HTTP 语义),
|
|
1698
|
+
* raw 为 handler 原始返回值(同进程取数类型保真,语义见 InjectResponse.raw)。
|
|
1686
1699
|
*
|
|
1687
1700
|
* `listen()` 前后均可调用——`listen()` 后调用常用于 Next.js Server Component 等同进程场景
|
|
1688
1701
|
* (配合 `getApp()` 拿到 app 实例)。
|
package/dist/index.js
CHANGED
|
@@ -5164,6 +5164,9 @@ async function injectParamsAsync(handler, ctx, body, injectors) {
|
|
|
5164
5164
|
function wrapResult(result, ctx) {
|
|
5165
5165
|
return wrapOkResult(result, ctx.config);
|
|
5166
5166
|
}
|
|
5167
|
+
function stashHandlerResult(ctx, result) {
|
|
5168
|
+
ctx.__faapiHandlerResult = result;
|
|
5169
|
+
}
|
|
5167
5170
|
function mergeMeta(response, meta) {
|
|
5168
5171
|
const hasMeta = meta.status !== void 0 || Object.keys(meta.headers).length > 0 || meta.setCookies.length > 0;
|
|
5169
5172
|
if (!hasMeta) return response;
|
|
@@ -5232,7 +5235,9 @@ async function invokeHandler(handler, ctx, body, middlewares, injectors) {
|
|
|
5232
5235
|
const result = await injectParamsAsync(handler, ctx, body, injectors);
|
|
5233
5236
|
const sseResponse = pickSseAndAutoClose();
|
|
5234
5237
|
if (sseResponse) return sseResponse;
|
|
5235
|
-
|
|
5238
|
+
const response = await toResponse(wrapResult(result, ctx), meta);
|
|
5239
|
+
if (!(result instanceof Response)) stashHandlerResult(ctx, result);
|
|
5240
|
+
return response;
|
|
5236
5241
|
} catch (err) {
|
|
5237
5242
|
autoCloseSseOnError();
|
|
5238
5243
|
throw err;
|
|
@@ -5243,7 +5248,9 @@ async function invokeHandler(handler, ctx, body, middlewares, injectors) {
|
|
|
5243
5248
|
const result = await injectParamsAsync(handler, ctx, body, injectors);
|
|
5244
5249
|
const sseResponse = pickSseAndAutoClose();
|
|
5245
5250
|
if (sseResponse) return sseResponse;
|
|
5246
|
-
|
|
5251
|
+
const response = await toResponse(wrapResult(result, ctx), meta);
|
|
5252
|
+
if (!(result instanceof Response)) stashHandlerResult(ctx, result);
|
|
5253
|
+
return response;
|
|
5247
5254
|
} catch (err) {
|
|
5248
5255
|
autoCloseSseOnError();
|
|
5249
5256
|
throw err;
|
|
@@ -6153,6 +6160,7 @@ async function handleRequest(routes, rootDir, dist, req, res, outerMiddlewares,
|
|
|
6153
6160
|
globalInjectors
|
|
6154
6161
|
});
|
|
6155
6162
|
const response = outerMiddlewares.length > 0 ? await compose(outerMiddlewares, ctx, routePipeline) : await routePipeline();
|
|
6163
|
+
res.__faapiHandlerResult = ctx.__faapiHandlerResult;
|
|
6156
6164
|
await sendSuccessResponse(response, res);
|
|
6157
6165
|
} catch (err) {
|
|
6158
6166
|
if (res.destroyed || res.writableEnded) return;
|
|
@@ -7196,7 +7204,10 @@ async function createAppBase(options) {
|
|
|
7196
7204
|
resolve({
|
|
7197
7205
|
status: mockRes.statusCode,
|
|
7198
7206
|
headers: new Headers(mockRes._headers),
|
|
7199
|
-
body: parsed
|
|
7207
|
+
body: parsed,
|
|
7208
|
+
// inject raw 捕获桥(第三跳):handler 原始返回值(handleRequest 从 ctx
|
|
7209
|
+
// 桥接到 res;未捕获时 undefined——语义见 InjectResponse.raw 注释)
|
|
7210
|
+
raw: mockRes.__faapiHandlerResult
|
|
7200
7211
|
});
|
|
7201
7212
|
});
|
|
7202
7213
|
const listeners = server.listeners("request");
|