Skip to content

Commit bc377c1

Browse files
committed
feat: add Hono support
1 parent 44a9a80 commit bc377c1

31 files changed

+2548
-782
lines changed

examples/express/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ const app = express();
77

88
app.use(express.json());
99

10+
const usersRouter = express.Router();
11+
const versionRouter = express.Router();
12+
1013
/**
1114
* @summary 更新用户信息
1215
* @description 更新指定用户的个人信息
1316
* @tags users
1417
* @response 200 {@link UpdateUserVo} 更新用户信息成功
1518
*/
16-
app.put(
17-
"/api/users/:id",
19+
usersRouter.put(
20+
":id",
1821
zodValidator({
1922
params: UserIdDto,
2023
body: UpdateUserDto,
@@ -31,6 +34,9 @@ app.put(
3134
},
3235
);
3336

37+
versionRouter.use("users", usersRouter);
38+
app.use(versionRouter);
39+
3440
// 生成 OpenAPI 文档
3541
const openapi = await generateDocument(
3642
{
@@ -47,6 +53,8 @@ const openapi = await generateDocument(
4753
},
4854
);
4955

56+
console.log(openapi);
57+
5058
// 提供 OpenAPI JSON 文档
5159
app.get("/openapi.json", (_req, res) => {
5260
res.json(openapi);

examples/hono/index.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { serve } from "@hono/node-server";
2+
import { generateDocument } from "api-morph";
3+
import { setupSwaggerUI, zodValidator } from "api-morph/hono";
4+
import { Hono } from "hono";
5+
import { UpdateUserDto, UpdateUserVo, UserIdDto } from "./schema";
6+
7+
const app = new Hono();
8+
9+
/**
10+
* @summary 更新用户信息
11+
* @description 更新指定用户的个人信息
12+
* @tags users
13+
* @response 200 {@link UpdateUserVo} 更新用户信息成功
14+
*/
15+
app.put(
16+
"/api/users/:id",
17+
zodValidator("param", UserIdDto),
18+
zodValidator("json", UpdateUserDto),
19+
(c) => {
20+
const { id } = c.req.valid("param");
21+
const { email, username } = c.req.valid("json");
22+
23+
return c.json({
24+
id,
25+
email,
26+
username,
27+
});
28+
},
29+
);
30+
31+
// 生成 OpenAPI 文档
32+
const openapi = await generateDocument(
33+
{
34+
info: {
35+
version: "1.0.0",
36+
title: "用户管理 API",
37+
description: "这是一个用户管理 API 的文档示例",
38+
},
39+
},
40+
{
41+
parserOptions: {
42+
include: ["examples/hono/**/*.ts"],
43+
},
44+
},
45+
);
46+
47+
// 提供 OpenAPI JSON 文档
48+
app.get("/openapi.json", (c) => {
49+
return c.json(openapi);
50+
});
51+
52+
// 提供 Swagger UI 界面
53+
setupSwaggerUI("/swagger-ui", app);
54+
55+
serve(
56+
{
57+
fetch: app.fetch,
58+
port: 3000,
59+
},
60+
(info) => {
61+
console.log(`Example app listening on port ${info.port}`);
62+
console.log(`访问 http://localhost:${info.port}/swagger-ui 查看 API 文档`);
63+
},
64+
);

examples/hono/schema.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { z } from "zod/v4";
2+
3+
export const UserIdDto = z.object({
4+
id: z.string().meta({ description: "用户ID" }),
5+
});
6+
7+
export const UpdateUserDto = z.object({
8+
email: z.email().meta({
9+
description: "用户邮箱地址",
10+
examples: ["[email protected]"],
11+
}),
12+
username: z
13+
.string()
14+
.min(3)
15+
.max(50)
16+
.meta({
17+
description: "用户名",
18+
examples: ["John Doe"],
19+
}),
20+
});
21+
22+
export const UpdateUserVo = z.object({
23+
id: z.string().meta({ description: "用户ID" }),
24+
email: z.email().meta({
25+
description: "用户邮箱地址",
26+
examples: ["[email protected]"],
27+
}),
28+
username: z
29+
.string()
30+
.min(3)
31+
.max(50)
32+
.meta({
33+
description: "用户名",
34+
examples: ["John Doe"],
35+
}),
36+
});

examples/koa/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ router.put(
3131
},
3232
);
3333

34-
app.use(router.routes()).use(router.allowedMethods());
34+
app.use(router.routes());
3535

3636
// 生成 OpenAPI 文档
3737
const openapi = await generateDocument(

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
"express-swagger",
2424
"koa-openapi",
2525
"koa-swagger",
26+
"hono-openapi",
27+
"hono-swagger",
2628
"zod-openapi",
2729
"zod-swagger"
2830
],
@@ -41,6 +43,11 @@
4143
"types": "./dist/koa.d.ts",
4244
"import": "./dist/koa.js",
4345
"require": "./dist/koa.js"
46+
},
47+
"./hono": {
48+
"types": "./dist/hono.d.ts",
49+
"import": "./dist/hono.js",
50+
"require": "./dist/hono.js"
4451
}
4552
},
4653
"main": "./dist/index.js",
@@ -73,6 +80,8 @@
7380
"postinstall": "lefthook install"
7481
},
7582
"dependencies": {
83+
"@hono/node-server": "^1.15.0",
84+
"@hono/zod-validator": "^0.7.0",
7685
"@hyperjump/json-schema": "^1.16.0",
7786
"@types/koa-static": "^4.0.4",
7887
"deepmerge": "^4.3.1",
@@ -113,6 +122,7 @@
113122
"@types/koa": "^2.0.0",
114123
"@types/koa__router": "^12.0.0",
115124
"express": "^5.0.0",
125+
"hono": "^4.0.0",
116126
"koa": "^3.0.0"
117127
}
118128
}

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)