hono-openapi 0.1.0
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/LICENSE +21 -0
- package/README.md +86 -0
- package/index.cjs.js +1 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Rhinobase
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# Hono OpenAPI
|
|
2
|
+
|
|
3
|
+
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
|
+
|
|
5
|
+
Supported Validation Libraries:
|
|
6
|
+
|
|
7
|
+
- [x] [Zod](https://zod.dev/)
|
|
8
|
+
- [ ] [TypeBox](https://github.com/sinclairzx81/typebox) (coming soon)
|
|
9
|
+
- [ ] [Valibot](https://valibot.dev/) (coming soon)
|
|
10
|
+
|
|
11
|
+
> [!Note]
|
|
12
|
+
> This package doesn't validate the responses schema, it only generates the OpenAPI specification from it. You should use the validation library to validate your response data, if you wanna do it.
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
To generate the OpenAPI specification, run the following command:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pnpm add hono-openapi
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
And you can now start defining your routes and generating the OpenAPI specification. Here is an example using Zod:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
// For extending the Zod schema with OpenAPI properties
|
|
26
|
+
import "zod-openapi/extend";
|
|
27
|
+
|
|
28
|
+
import z from "zod";
|
|
29
|
+
import { Hono } from "hono";
|
|
30
|
+
import { zodResolver } from "hono-openapi/zod";
|
|
31
|
+
import { zValidator } from "@hono/zod-validator";
|
|
32
|
+
import { describeRoute, openAPISpecs } from "hono-openapi";
|
|
33
|
+
|
|
34
|
+
const app = new Hono();
|
|
35
|
+
|
|
36
|
+
const schema = z.object({
|
|
37
|
+
name: z.string().optional().openapi({ example: "Steven", ref: "name" }),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const nameValidation = z.object({
|
|
41
|
+
name: z
|
|
42
|
+
.string()
|
|
43
|
+
.optional()
|
|
44
|
+
.openapi({ example: "Steven", description: "User Name", ref: "name" }),
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
app.get(
|
|
48
|
+
"/",
|
|
49
|
+
describeRoute({
|
|
50
|
+
description: "Say hello to the user",
|
|
51
|
+
responses: {
|
|
52
|
+
200: {
|
|
53
|
+
description: "Successful greeting response",
|
|
54
|
+
content: {
|
|
55
|
+
"text/plain": {
|
|
56
|
+
schema: {
|
|
57
|
+
type: "string",
|
|
58
|
+
example: "Hello Steven!",
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
}),
|
|
65
|
+
zValidator("query", nameValidation),
|
|
66
|
+
(c) => {
|
|
67
|
+
const query = c.req.valid("query");
|
|
68
|
+
return c.text(`Hello ${query?.name ?? "Hono"}!`);
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
app.get(
|
|
73
|
+
"/openapi",
|
|
74
|
+
openAPISpecs(app, {
|
|
75
|
+
documentation: {
|
|
76
|
+
info: { title: "Hono", version: "1.0.0" },
|
|
77
|
+
servers: [
|
|
78
|
+
{
|
|
79
|
+
url: "http://localhost:3000",
|
|
80
|
+
description: "Local server",
|
|
81
|
+
},
|
|
82
|
+
],
|
|
83
|
+
},
|
|
84
|
+
})
|
|
85
|
+
);
|
|
86
|
+
```
|
package/index.cjs.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var e=require("./utils.cjs.js");exports.describeRoute=function(t){return Object.assign((async(e,t)=>{await t()}),{[e.uniqueSymbol]:{resolver:e=>{const o={...t};let n={};if(o.responses)for(const t of Object.keys(o.responses)){const s=o.responses[t];if(!s||"content"in s)for(const t of Object.keys(s.content??{})){const o=s.content?.[t];if(o&&(o.schema&&"builder"in o.schema)){const t=o.schema.builder(e);o.schema=t.schema,t.components&&(n={...n,...t.components})}}}return{docs:o,components:n}}}})},exports.openAPISpecs=function(t,{documentation:o={},excludeStaticFile:n=!0,exclude:s=[],excludeMethods:c=["OPTIONS"],excludeTags:i=[]}={documentation:{},excludeStaticFile:!0,exclude:[],excludeMethods:["OPTIONS"],excludeTags:[]}){const a={version:"3.0.3",components:{}},r={};for(const o of t.routes){if(!(e.uniqueSymbol in o.handler))continue;if(c.includes(o.method))continue;if(!1===e.ALLOWED_METHODS.includes(o.method)&&"ALL"!==o.method)continue;const{resolver:t,metadata:n={}}=o.handler[e.uniqueSymbol],{docs:s,components:i}=t({...a,...n});if(a.components={...a.components,...i??{}},"ALL"===o.method)for(const t of e.ALLOWED_METHODS)e.registerSchemaPath({path:o.path,data:s,method:t,schema:r});else e.registerSchemaPath({method:o.method,path:o.path,data:s,schema:r})}for(const e in r)for(const t in r[e])r[e][t].hide&&delete r[e][t];const m={openapi:a.version,...{...o,tags:o.tags?.filter((e=>!i?.includes(e?.name))),info:{title:"Hono Documentation",description:"Development documentation",version:"0.0.0",...o.info},paths:{...e.filterPaths(r,{excludeStaticFile:n,exclude:Array.isArray(s)?s:[s]}),...o.paths},components:{...o.components,schemas:{...a.components,...o.components?.schemas}}}};return e=>e.json(m)};
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "hono-openapi",
|
|
3
|
+
"description": "OpenAPI schema generator for Hono",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"hono",
|
|
8
|
+
"openapi"
|
|
9
|
+
],
|
|
10
|
+
"homepage": "https://github.com/rhinobase/hono-openapi",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/rhinobase/hono-openapi.git",
|
|
17
|
+
"directory": "packages/ui"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/rhinobase/hono-openapi/issues"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {},
|
|
23
|
+
"main": "./index.cjs.js",
|
|
24
|
+
"module": "./index.esm.js",
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"!**/*.tsbuildinfo"
|
|
28
|
+
],
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"hono": "^4.6.4"
|
|
31
|
+
},
|
|
32
|
+
"optionalDependencies": {
|
|
33
|
+
"@hono/zod-validator": "^0.4.1",
|
|
34
|
+
"zod": "^3.23.8",
|
|
35
|
+
"zod-openapi": "^3.1.1"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
39
|
+
"openapi-types": "^12.1.3"
|
|
40
|
+
},
|
|
41
|
+
"exports": {
|
|
42
|
+
".": {
|
|
43
|
+
"import": "./index.esm.js",
|
|
44
|
+
"require": "./index.cjs.js",
|
|
45
|
+
"types": "./index.cjs.d.ts"
|
|
46
|
+
},
|
|
47
|
+
"./zod": {
|
|
48
|
+
"import": "./zod.esm.js",
|
|
49
|
+
"require": "./zod.cjs.js",
|
|
50
|
+
"types": "./zod.cjs.d.ts"
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
"types": "./index.esm.d.ts"
|
|
54
|
+
}
|