diff --git a/src/pages/_api/api/demo/chat.ts b/src/pages/_api/api/demo/chat.ts index 307986ce..01544ec0 100644 --- a/src/pages/_api/api/demo/chat.ts +++ b/src/pages/_api/api/demo/chat.ts @@ -1,5 +1,6 @@ import { mppx } from "../../../../mppx.server"; import { getProxyFetch } from "../../../../mppx-proxy-client"; +import { sessionUpdateResponse } from "../../../../session-response"; const responses = [ [ @@ -82,7 +83,7 @@ export default async function handler(request: Request) { if (result.status === 402) return result.challenge; // POST = voucher update (no body needed) - if (request.method === "POST") return result.withReceipt(); + if (request.method === "POST") return sessionUpdateResponse(result); // GET = stream a chat response const proxyFetch = getProxyFetch(); diff --git a/src/pages/_api/api/demo/poem.ts b/src/pages/_api/api/demo/poem.ts index f31d2191..a02dd057 100644 --- a/src/pages/_api/api/demo/poem.ts +++ b/src/pages/_api/api/demo/poem.ts @@ -1,4 +1,5 @@ import { mppx } from "../../../../mppx.server"; +import { sessionUpdateResponse } from "../../../../session-response"; const poems = [ [ @@ -116,7 +117,7 @@ export default async function handler(request: Request) { if (result.status === 402) return result.challenge; // POST = voucher update (no body needed) - if (request.method === "POST") return result.withReceipt(); + if (request.method === "POST") return sessionUpdateResponse(result); // GET = stream a poem const poem = poems[Math.floor(Math.random() * poems.length)]; diff --git a/src/pages/_api/api/sessions/poem.ts b/src/pages/_api/api/sessions/poem.ts index 9cec0357..e9a0293f 100644 --- a/src/pages/_api/api/sessions/poem.ts +++ b/src/pages/_api/api/sessions/poem.ts @@ -1,4 +1,5 @@ import { mppx } from "../../../../mppx.server"; +import { sessionUpdateResponse } from "../../../../session-response"; const poems = [ { @@ -75,7 +76,7 @@ export default async function handler(request: Request) { if (result.status === 402) return result.challenge; // POST = voucher update (no body needed) - if (request.method === "POST") return result.withReceipt(); + if (request.method === "POST") return sessionUpdateResponse(result); // GET = stream a poem const poem = poems[Math.floor(Math.random() * poems.length)]; diff --git a/src/session-response.test.ts b/src/session-response.test.ts new file mode 100644 index 00000000..78cd93bd --- /dev/null +++ b/src/session-response.test.ts @@ -0,0 +1,15 @@ +import { describe, expect, it } from "vitest"; +import { sessionUpdateResponse } from "./session-response"; + +describe("sessionUpdateResponse", () => { + it("passes an explicit empty response to withReceipt", () => { + const response = sessionUpdateResponse({ + withReceipt(inner) { + expect(inner.status).toBe(204); + return new Response(null, { status: inner.status }); + }, + }); + + expect(response.status).toBe(204); + }); +}); diff --git a/src/session-response.ts b/src/session-response.ts new file mode 100644 index 00000000..ba733dab --- /dev/null +++ b/src/session-response.ts @@ -0,0 +1,7 @@ +type ReceiptResult = { + withReceipt(response: Response): Response; +}; + +export function sessionUpdateResponse(result: ReceiptResult): Response { + return result.withReceipt(new Response(null, { status: 204 })); +}