enlace-hono 0.0.1-beta.1 → 0.0.1-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +68 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# enlace-hono
|
|
2
|
+
|
|
3
|
+
Type adapter to convert [Hono](https://hono.dev) app types to Enlace's ApiSchema format.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install enlace-hono
|
|
9
|
+
# peer dependencies
|
|
10
|
+
npm install hono enlace-core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
// server.ts (Hono)
|
|
17
|
+
import { Hono } from "hono";
|
|
18
|
+
import { zValidator } from "@hono/zod-validator";
|
|
19
|
+
import { z } from "zod";
|
|
20
|
+
|
|
21
|
+
const postSchema = z.object({ title: z.string() });
|
|
22
|
+
|
|
23
|
+
const app = new Hono()
|
|
24
|
+
.basePath("/api")
|
|
25
|
+
.get("/posts", (c) => c.json([{ id: 1, title: "Hello" }]))
|
|
26
|
+
.post("/posts", zValidator("json", postSchema), (c) => {
|
|
27
|
+
const body = c.req.valid("json");
|
|
28
|
+
return c.json({ id: 2, ...body }, 201);
|
|
29
|
+
})
|
|
30
|
+
.get("/posts/:id", (c) => c.json({ id: c.req.param("id"), title: "Post" }));
|
|
31
|
+
|
|
32
|
+
export type AppType = typeof app;
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
// client.ts (Enlace)
|
|
37
|
+
import { enlace } from "enlace-core";
|
|
38
|
+
import type { HonoToEnlace } from "enlace-hono";
|
|
39
|
+
import type { AppType } from "./server";
|
|
40
|
+
|
|
41
|
+
type ApiSchema = HonoToEnlace<AppType>;
|
|
42
|
+
|
|
43
|
+
// Use the 'api' key since basePath is '/api'
|
|
44
|
+
const client = enlace<ApiSchema["api"]>("http://localhost:3000/api");
|
|
45
|
+
|
|
46
|
+
// Fully typed!
|
|
47
|
+
const posts = await client.posts.$get(); // { id: number, title: string }[]
|
|
48
|
+
const post = await client.posts["123"].$get(); // { id: string, title: string }
|
|
49
|
+
const newPost = await client.posts.$post({
|
|
50
|
+
body: { title: "New Post" },
|
|
51
|
+
}); // { id: number, title: string }
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Type Transformation
|
|
55
|
+
|
|
56
|
+
`HonoToEnlace` converts Hono's flat path schema to Enlace's nested structure:
|
|
57
|
+
|
|
58
|
+
| Hono | Enlace |
|
|
59
|
+
| ------------------------- | ---------------------------------- |
|
|
60
|
+
| `"/api/posts"` | `{ api: { posts: {...} } }` |
|
|
61
|
+
| `"/api/posts/:id"` | `{ api: { posts: { _: {...} } } }` |
|
|
62
|
+
| `{ input: { json: T } }` | `Endpoint<Output, T>` |
|
|
63
|
+
| `{ input: { query: T } }` | `EndpointWithQuery<Output, T>` |
|
|
64
|
+
| `{ input: { form: T } }` | `EndpointWithFormData<Output, T>` |
|
|
65
|
+
|
|
66
|
+
## License
|
|
67
|
+
|
|
68
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "enlace-hono",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
20
|
"hono": "^4.10.6",
|
|
21
|
-
"enlace-core": "0.0.1-beta.
|
|
21
|
+
"enlace-core": "0.0.1-beta.10"
|
|
22
22
|
},
|
|
23
23
|
"scripts": {
|
|
24
24
|
"dev": "tsup --watch",
|