hono-openapi 0.1.2 → 0.1.4
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 +122 -27
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,48 +1,63 @@
|
|
|
1
|
-
# Hono OpenAPI
|
|
1
|
+
# 📜 Hono OpenAPI
|
|
2
|
+
|
|
3
|
+
[](https://npmjs.org/package/hono-openapi "View this project on NPM")
|
|
4
|
+
[](https://www.npmjs.com/package/hono-openapi)
|
|
5
|
+
[](LICENSE)
|
|
2
6
|
|
|
3
7
|
This can automatically generate the OpenAPI specification for the Hono API using your validation schema, which can be used to generate client libraries, documentation, and more.
|
|
4
8
|
|
|
5
9
|
Supported Validation Libraries:
|
|
6
10
|
|
|
7
11
|
- [x] [Zod](https://zod.dev/)
|
|
8
|
-
- [ ] [TypeBox](https://github.com/sinclairzx81/typebox)
|
|
9
|
-
- [ ] [Valibot](https://valibot.dev/)
|
|
12
|
+
- [ ] [TypeBox](https://github.com/sinclairzx81/typebox)
|
|
13
|
+
- [ ] [Valibot](https://valibot.dev/)
|
|
10
14
|
|
|
11
15
|
> [!Note]
|
|
12
|
-
> This package
|
|
16
|
+
> This package is still in development and your feedback is highly appreciated. If you have any suggestions or issues, please let us know by creating an issue on GitHub.
|
|
13
17
|
|
|
14
18
|
## Usage
|
|
15
19
|
|
|
16
|
-
|
|
20
|
+
### Installation
|
|
21
|
+
|
|
22
|
+
You can install the package using favorite package manager.
|
|
17
23
|
|
|
18
24
|
```bash
|
|
19
25
|
pnpm add hono-openapi
|
|
20
26
|
```
|
|
21
27
|
|
|
22
|
-
|
|
28
|
+
### Basic Usage
|
|
29
|
+
|
|
30
|
+
#### Setting up your application
|
|
31
|
+
|
|
32
|
+
First, define your schemas, here is an example using Zod:
|
|
23
33
|
|
|
24
34
|
```ts
|
|
35
|
+
import z from "zod";
|
|
36
|
+
|
|
25
37
|
// For extending the Zod schema with OpenAPI properties
|
|
26
38
|
import "zod-openapi/extend";
|
|
27
39
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
40
|
+
const querySchema = z
|
|
41
|
+
.object({
|
|
42
|
+
name: z.string().optional().openapi({ example: "Steven" }),
|
|
43
|
+
})
|
|
44
|
+
.openapi({ ref: "Query" });
|
|
33
45
|
|
|
34
|
-
const
|
|
46
|
+
const responseSchema = z.string().openapi({ example: "Hello Steven!" });
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Extending the Zod schema with OpenAPI properties is optional, but it will help you generate the OpenAPI specification. You can learn more about it here - [https://github.com/samchungy/zod-openapi](https://github.com/samchungy/zod-openapi).
|
|
35
50
|
|
|
36
|
-
|
|
37
|
-
name: z.string().optional().openapi({ example: "Steven", ref: "name" }),
|
|
38
|
-
});
|
|
51
|
+
> [!Tip] > `querySchema` schema will be registered as "#/components/schemas/Query" refs in the OpenAPI document. If you want to register the schema as referenced components, use .openapi() method.
|
|
39
52
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
53
|
+
Next, create your route -
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { Hono } from "hono";
|
|
57
|
+
import { describeRoute } from "hono-openapi";
|
|
58
|
+
import { resolver, validator as zValidator } from "hono-openapi/zod";
|
|
59
|
+
|
|
60
|
+
const app = new Hono();
|
|
46
61
|
|
|
47
62
|
app.get(
|
|
48
63
|
"/",
|
|
@@ -53,27 +68,34 @@ app.get(
|
|
|
53
68
|
description: "Successful greeting response",
|
|
54
69
|
content: {
|
|
55
70
|
"text/plain": {
|
|
56
|
-
schema:
|
|
57
|
-
type: "string",
|
|
58
|
-
example: "Hello Steven!",
|
|
59
|
-
},
|
|
71
|
+
schema: resolver(responseSchema),
|
|
60
72
|
},
|
|
61
73
|
},
|
|
62
74
|
},
|
|
63
75
|
},
|
|
64
76
|
}),
|
|
65
|
-
zValidator("query",
|
|
77
|
+
zValidator("query", querySchema),
|
|
66
78
|
(c) => {
|
|
67
79
|
const query = c.req.valid("query");
|
|
68
80
|
return c.text(`Hello ${query?.name ?? "Hono"}!`);
|
|
69
81
|
}
|
|
70
82
|
);
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
You might be wondering why are we importing `validator` from `hono-openapi/zod` instead of `@hono/zod-validator` and as `zValidator`? This is because `hono-openapi` provides a wrapper around the `@hono/zod-validator` to make it easier to use. The idea is if you are already using `@hono/zod-validator` to validate your schemas, you can easily switch to `hono-openapi` without changing much of your code.
|
|
86
|
+
|
|
87
|
+
Finally, generate the OpenAPI specification -
|
|
71
88
|
|
|
89
|
+
```ts
|
|
72
90
|
app.get(
|
|
73
91
|
"/openapi",
|
|
74
92
|
openAPISpecs(app, {
|
|
75
93
|
documentation: {
|
|
76
|
-
info: {
|
|
94
|
+
info: {
|
|
95
|
+
title: "Hono",
|
|
96
|
+
version: "1.0.0",
|
|
97
|
+
description: "API for greeting users",
|
|
98
|
+
},
|
|
77
99
|
servers: [
|
|
78
100
|
{
|
|
79
101
|
url: "http://localhost:3000",
|
|
@@ -84,3 +106,76 @@ app.get(
|
|
|
84
106
|
})
|
|
85
107
|
);
|
|
86
108
|
```
|
|
109
|
+
|
|
110
|
+
Now, you can access the OpenAPI specification by visiting `http://localhost:3000/openapi`, and you can use this specification to generate client libraries, documentation, and more. Some tools that I used to generate documentation are -
|
|
111
|
+
|
|
112
|
+
- [Swagger UI](https://github.com/honojs/middleware/tree/main/packages/swagger-ui)
|
|
113
|
+
- [Scalar](https://github.com/scalar/scalar/tree/main/packages/hono-api-reference)
|
|
114
|
+
|
|
115
|
+
##### Scalar Example
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
app.get(
|
|
119
|
+
"/docs",
|
|
120
|
+
apiReference({
|
|
121
|
+
theme: "saturn",
|
|
122
|
+
spec: {
|
|
123
|
+
url: "/openapi",
|
|
124
|
+
},
|
|
125
|
+
})
|
|
126
|
+
);
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
And that's it! You have successfully generated the OpenAPI specification for your Hono API.
|
|
130
|
+
|
|
131
|
+
### Advanced Usage
|
|
132
|
+
|
|
133
|
+
#### Adding Security Definitions
|
|
134
|
+
|
|
135
|
+
You can add security definitions to your OpenAPI specification by using the `security` property in the `openAPISpecs` function.
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
app.get(
|
|
139
|
+
"/openapi",
|
|
140
|
+
openAPISpecs(appRouter, {
|
|
141
|
+
documentation: {
|
|
142
|
+
info: {
|
|
143
|
+
title: "Rhinobase Cloud",
|
|
144
|
+
version: "1.0.0",
|
|
145
|
+
description: "API Documentation",
|
|
146
|
+
},
|
|
147
|
+
components: {
|
|
148
|
+
securitySchemes: {
|
|
149
|
+
bearerAuth: {
|
|
150
|
+
type: "http",
|
|
151
|
+
scheme: "bearer",
|
|
152
|
+
bearerFormat: "JWT",
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
security: [
|
|
157
|
+
{
|
|
158
|
+
bearerAuth: [],
|
|
159
|
+
},
|
|
160
|
+
],
|
|
161
|
+
servers: [
|
|
162
|
+
{
|
|
163
|
+
url: "http://localhost:3004",
|
|
164
|
+
description: "Local server",
|
|
165
|
+
},
|
|
166
|
+
],
|
|
167
|
+
},
|
|
168
|
+
})
|
|
169
|
+
);
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Contributing
|
|
173
|
+
|
|
174
|
+
We would love to have more contributors involved!
|
|
175
|
+
|
|
176
|
+
To get started, please read our [Contributing Guide](https://github.com/rhinobase/hono-openapi/blob/main/CONTRIBUTING.md).
|
|
177
|
+
|
|
178
|
+
## Credits
|
|
179
|
+
|
|
180
|
+
- The idea for this project was inspired by [ElysiaJS](https://elysiajs.com/) and their amazing work on generating [OpenAPI](https://elysiajs.com/recipe/openapi.html) specifications.
|
|
181
|
+
- This project would not have been possible without the work of [Sam Chung](https://github.com/samchungy) and his [Zod OpenAPI](https://github.com/samchungy/zod-openapi) package.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hono-openapi",
|
|
3
3
|
"description": "OpenAPI schema generator for Hono",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.4",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"hono",
|
|
@@ -23,11 +23,11 @@
|
|
|
23
23
|
"hono": "^4.6.4",
|
|
24
24
|
"@hono/zod-validator": "^0.4.1",
|
|
25
25
|
"zod": "^3.23.8",
|
|
26
|
-
"zod-openapi": "^3.1.1"
|
|
26
|
+
"zod-openapi": "^3.1.1",
|
|
27
|
+
"openapi-types": "^12.1.3"
|
|
27
28
|
},
|
|
28
29
|
"devDependencies": {
|
|
29
|
-
"@rollup/plugin-terser": "^0.4.4"
|
|
30
|
-
"openapi-types": "^12.1.3"
|
|
30
|
+
"@rollup/plugin-terser": "^0.4.4"
|
|
31
31
|
},
|
|
32
32
|
"exports": {
|
|
33
33
|
".": {
|