Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/hono/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,18 @@ import api from "./api";
const app = new Hono();
app.use("*", stlApi(api));
```

Individual handlers can also use Hono responses:

```ts
const retrieve = stl.endpoint({
endpoint: "GET /api/posts",
response: z.any() as z.ZodType<Response>,
handler: (_, context) => {
const [c] = context.server.args;

// c is a Hono context
return c.redirect("/");
},
});
```
19 changes: 19 additions & 0 deletions packages/hono/src/honoPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,19 @@ describe("hono passthrough", () => {
}),
},
}),
redirect: stl.resource({
summary: "redirect",
actions: {
retrieve: stl.endpoint({
endpoint: "GET /api/redirect",
response: z.any() as z.ZodType<Response>,
handler: (_, context) => {
const [c] = context.server.args;
return c.redirect("/");
},
}),
},
}),
},
});

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

test("hono response", async () => {
const response = await app.request("/api/redirect");
expect(response).toHaveProperty("status", 302);
expect(response.headers.get("location")).toMatchInlineSnapshot(`"/"`);
});

test("public passthrough", async () => {
const response = await app.request("/public/foo/bar");
expect(response).toHaveProperty("status", 200);
Expand Down
4 changes: 4 additions & 0 deletions packages/hono/src/honoPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ function makeHandler(endpoints: AnyEndpoint[], options?: StlAppOptions) {

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

if (result instanceof Response) {
return result;
}

return c.json(result);
} catch (error) {
if (options?.handleErrors === false) {
Expand Down
Loading