Skip to content

Commit a1f5c79

Browse files
authored
Merge pull request #86 from stainless-api/cj/hono
feat(hono): let handlers use request bodies
2 parents 6284911 + f819d93 commit a1f5c79

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

packages/hono/src/honoPlugin.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,15 @@ describe("hono passthrough", () => {
180180
throw new Error("arbitrary error");
181181
},
182182
}),
183+
create: stl.endpoint({
184+
endpoint: "POST /api/posts",
185+
body: z.any(),
186+
response: z.any(),
187+
handler: async (body, context) => {
188+
const [c] = context.server.args;
189+
return { bodyStl: body, bodyRaw: await c.req.raw.text() };
190+
},
191+
}),
183192
},
184193
}),
185194
redirect: stl.resource({
@@ -235,4 +244,20 @@ describe("hono passthrough", () => {
235244
`"custom error: arbitrary error"`
236245
);
237246
});
247+
248+
test("request passthrough", async () => {
249+
const response = await app.request("/api/posts", {
250+
method: "POST",
251+
body: JSON.stringify({ message: "hello" }),
252+
});
253+
expect(response).toHaveProperty("status", 200);
254+
expect(await response.json()).toMatchInlineSnapshot(`
255+
{
256+
"bodyRaw": "{"message":"hello"}",
257+
"bodyStl": {
258+
"message": "hello",
259+
},
260+
}
261+
`);
262+
});
238263
});

packages/hono/src/honoPlugin.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ function makeHandler(endpoints: AnyEndpoint[], options?: StlAppOptions) {
7777
const params = stl.initParams({
7878
path,
7979
query: search ? qs.parse(search.replace(/^\?/, "")) : {},
80-
body: await c.req.json().catch(() => undefined),
80+
// Don't use up the raw body in case the handler needs to use it:
81+
body: await c.req.raw
82+
.clone()
83+
.json()
84+
.catch(() => undefined),
8185
headers: c.req.header(),
8286
});
8387

0 commit comments

Comments
 (0)