Skip to content

Commit

Permalink
Merge pull request #86 from stainless-api/cj/hono
Browse files Browse the repository at this point in the history
feat(hono): let handlers use request bodies
  • Loading branch information
cjquines authored Jan 8, 2025
2 parents 6284911 + f819d93 commit a1f5c79
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
25 changes: 25 additions & 0 deletions packages/hono/src/honoPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ describe("hono passthrough", () => {
throw new Error("arbitrary error");
},
}),
create: stl.endpoint({
endpoint: "POST /api/posts",
body: z.any(),
response: z.any(),
handler: async (body, context) => {
const [c] = context.server.args;
return { bodyStl: body, bodyRaw: await c.req.raw.text() };
},
}),
},
}),
redirect: stl.resource({
Expand Down Expand Up @@ -235,4 +244,20 @@ describe("hono passthrough", () => {
`"custom error: arbitrary error"`
);
});

test("request passthrough", async () => {
const response = await app.request("/api/posts", {
method: "POST",
body: JSON.stringify({ message: "hello" }),
});
expect(response).toHaveProperty("status", 200);
expect(await response.json()).toMatchInlineSnapshot(`
{
"bodyRaw": "{"message":"hello"}",
"bodyStl": {
"message": "hello",
},
}
`);
});
});
6 changes: 5 additions & 1 deletion packages/hono/src/honoPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ function makeHandler(endpoints: AnyEndpoint[], options?: StlAppOptions) {
const params = stl.initParams({
path,
query: search ? qs.parse(search.replace(/^\?/, "")) : {},
body: await c.req.json().catch(() => undefined),
// Don't use up the raw body in case the handler needs to use it:
body: await c.req.raw
.clone()
.json()
.catch(() => undefined),
headers: c.req.header(),
});

Expand Down

0 comments on commit a1f5c79

Please sign in to comment.