Skip to content

Commit 4482f1f

Browse files
authored
Merge pull request #21 from d2verb/feat/use-hono-as-backend
Use hono as backend
2 parents fe1b379 + e7678aa commit 4482f1f

11 files changed

Lines changed: 135 additions & 294 deletions

File tree

README.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ deno run -A page.tsx
2828
/** @jsxImportSource https://esm.sh/preact@10.27.2 */
2929
// deno-lint-ignore-file no-import-prefix
3030
import { useEffect, useState } from "https://esm.sh/preact@10.27.2/hooks";
31-
import {
32-
defineApi,
33-
serve,
34-
} from "https://esm.sh/jsr/@d2verb/pera?deps=preact@10.27.2";
31+
import { serve } from "https://esm.sh/jsr/@d2verb/pera?deps=preact@10.27.2";
3532

3633
type Props = { initial?: number };
3734

@@ -77,11 +74,12 @@ await serve(App, {
7774
title: "Counter Sample",
7875
moduleUrl: import.meta.url,
7976
props: { initial: 4 },
80-
api: defineApi({
81-
"/users/:name": {
82-
GET: (_, ctx) => new Response(`Hello, ${ctx.params.name}!`),
83-
},
84-
}),
77+
api: (app) => {
78+
app.get(
79+
"/users/:name",
80+
(c) => new Response(`Hello, ${c.req.param("name")}!`),
81+
);
82+
},
8583
});
8684
```
8785

deno.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"imports": {
1919
"@deno/emit": "jsr:@deno/emit@^0.46.0",
20+
"@hono/hono": "jsr:@hono/hono@^4.9.10",
2021
"@std/assert": "jsr:@std/assert@1",
2122
"@std/async": "jsr:@std/async@^1.0.14",
2223
"@std/path": "jsr:@std/path@^1.1.2",

deno.lock

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/counter.tsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
// deno-lint-ignore-file no-import-prefix
33
// @ts-nocheck The old cached version of the library causes type errors sometimes.
44
import { useEffect, useState } from "https://esm.sh/preact@10.27.2/hooks";
5-
import {
6-
defineApi,
7-
serve,
8-
} from "https://esm.sh/jsr/@d2verb/pera?deps=preact@10.27.2";
5+
import { serve } from "https://esm.sh/jsr/@d2verb/pera?deps=preact@10.27.2";
96

107
type Props = { initial?: number };
118

@@ -51,9 +48,10 @@ await serve(App, {
5148
title: "Counter Sample",
5249
moduleUrl: import.meta.url,
5350
props: { initial: 4 },
54-
api: defineApi({
55-
"/users/:name": {
56-
GET: (_, ctx) => new Response(`Hello, ${ctx.params.name}!`),
57-
},
58-
}),
51+
api: (app) => {
52+
app.get(
53+
"/users/:name",
54+
(c) => new Response(`Hello, ${c.req.param("name")}!`),
55+
);
56+
},
5957
});

src/api.ts

Lines changed: 0 additions & 117 deletions
This file was deleted.

src/mod.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ import type { PeraApp, PeraOptions } from "./types.ts";
1313
* ```
1414
*
1515
* @example With API
16-
* You should use defineApi() to define your API endpoints — it helps TypeScript infer the types of path parameters automatically.
16+
* `app` is a Hono app instance. You can use it to define your API endpoints.
1717
* ```ts
1818
* await serve(App, {
1919
* port: 8080,
2020
* moduleUrl: import.meta.url,
21-
* api: defineApi({
21+
* api: (app) => {
2222
* // The actual path is `/_pera/api/students/:name`
23-
* "/students/:name": {
24-
* GET: (_, ctx) => new Response(`Hello, ${ctx.params.name}!`),
25-
* },
26-
* }),
23+
* app.get("/students/:name", (c) =>
24+
* new Response(`Hello, ${c.req.param("name")}!`),
25+
* );
26+
* },
2727
* });
2828
* ```
2929
*
@@ -42,13 +42,4 @@ export async function serve<
4242
return serveImpl(App, opts);
4343
}
4444

45-
export type {
46-
ApiContext,
47-
ApiFn,
48-
ApiMap,
49-
ApiMethod,
50-
ApiMethodMap,
51-
PathParams,
52-
} from "./api.ts";
53-
export { defineApi } from "./api.ts";
5445
export type { PeraOptions };

0 commit comments

Comments
 (0)