Skip to content

Commit 6284911

Browse files
authored
Merge pull request #85 from stainless-api/cj/hono
feat(hono): let handlers use hono responses
2 parents 9fe9554 + fa78814 commit 6284911

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

packages/hono/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,18 @@ import api from "./api";
2525
const app = new Hono();
2626
app.use("*", stlApi(api));
2727
```
28+
29+
Individual handlers can also use Hono responses:
30+
31+
```ts
32+
const retrieve = stl.endpoint({
33+
endpoint: "GET /api/posts",
34+
response: z.any() as z.ZodType<Response>,
35+
handler: (_, context) => {
36+
const [c] = context.server.args;
37+
38+
// c is a Hono context
39+
return c.redirect("/");
40+
},
41+
});
42+
```

packages/hono/src/honoPlugin.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,19 @@ describe("hono passthrough", () => {
182182
}),
183183
},
184184
}),
185+
redirect: stl.resource({
186+
summary: "redirect",
187+
actions: {
188+
retrieve: stl.endpoint({
189+
endpoint: "GET /api/redirect",
190+
response: z.any() as z.ZodType<Response>,
191+
handler: (_, context) => {
192+
const [c] = context.server.args;
193+
return c.redirect("/");
194+
},
195+
}),
196+
},
197+
}),
185198
},
186199
});
187200

@@ -197,6 +210,12 @@ describe("hono passthrough", () => {
197210
return c.text(`custom error: ${err.message}`, 500);
198211
});
199212

213+
test("hono response", async () => {
214+
const response = await app.request("/api/redirect");
215+
expect(response).toHaveProperty("status", 302);
216+
expect(response.headers.get("location")).toMatchInlineSnapshot(`"/"`);
217+
});
218+
200219
test("public passthrough", async () => {
201220
const response = await app.request("/public/foo/bar");
202221
expect(response).toHaveProperty("status", 200);

packages/hono/src/honoPlugin.ts

+4
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ function makeHandler(endpoints: AnyEndpoint[], options?: StlAppOptions) {
8383

8484
const result = await stl.execute(params, context);
8585

86+
if (result instanceof Response) {
87+
return result;
88+
}
89+
8690
return c.json(result);
8791
} catch (error) {
8892
if (options?.handleErrors === false) {

0 commit comments

Comments
 (0)