hono-openapi 0.3.1 → 0.4.1-rc.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.
Files changed (66) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +252 -252
  3. package/arktype.cjs +1 -0
  4. package/arktype.d.cts +1 -0
  5. package/arktype.d.ts +1 -0
  6. package/arktype.js +1 -0
  7. package/effect.cjs +1 -0
  8. package/effect.d.cts +1 -0
  9. package/effect.d.ts +1 -0
  10. package/effect.js +1 -0
  11. package/index.cjs +1 -0
  12. package/index.d.cts +1 -0
  13. package/index.d.ts +1 -0
  14. package/index.js +1 -0
  15. package/package.json +77 -33
  16. package/src/arktype.d.ts +13 -1
  17. package/src/effect.d.ts +12 -1
  18. package/src/helper.d.ts +1 -1
  19. package/src/index.d.ts +3 -3
  20. package/src/openapi.d.ts +200 -3
  21. package/src/route.d.ts +41 -1
  22. package/src/typebox.d.ts +13 -1
  23. package/src/types.d.ts +7 -4
  24. package/src/utils.d.ts +1 -1
  25. package/src/valibot.d.ts +13 -1
  26. package/src/zod.d.ts +13 -1
  27. package/typebox.cjs +1 -0
  28. package/typebox.d.cts +1 -0
  29. package/typebox.d.ts +1 -0
  30. package/typebox.js +1 -0
  31. package/valibot.cjs +1 -0
  32. package/valibot.d.cts +1 -0
  33. package/valibot.d.ts +1 -0
  34. package/valibot.js +1 -0
  35. package/zod.cjs +1 -0
  36. package/zod.d.cts +1 -0
  37. package/zod.d.ts +1 -0
  38. package/zod.js +1 -0
  39. package/arktype.cjs.d.ts +0 -1
  40. package/arktype.cjs.js +0 -1
  41. package/arktype.esm.d.ts +0 -1
  42. package/arktype.esm.js +0 -1
  43. package/effect.cjs.d.ts +0 -1
  44. package/effect.cjs.js +0 -1
  45. package/effect.esm.d.ts +0 -1
  46. package/effect.esm.js +0 -1
  47. package/index.cjs.d.ts +0 -1
  48. package/index.cjs.js +0 -1
  49. package/index.esm.d.ts +0 -1
  50. package/index.esm.js +0 -1
  51. package/typebox.cjs.d.ts +0 -1
  52. package/typebox.cjs.js +0 -1
  53. package/typebox.esm.d.ts +0 -1
  54. package/typebox.esm.js +0 -1
  55. package/valibot.cjs.d.ts +0 -1
  56. package/valibot.cjs.js +0 -1
  57. package/valibot.esm.d.ts +0 -1
  58. package/valibot.esm.js +0 -1
  59. package/zod.cjs.d.ts +0 -1
  60. package/zod.cjs.js +0 -1
  61. package/zod.esm.d.ts +0 -1
  62. package/zod.esm.js +0 -1
  63. /package/{toOpenAPISchema.cjs.js → toOpenAPISchema.cjs} +0 -0
  64. /package/{toOpenAPISchema.esm.js → toOpenAPISchema.js} +0 -0
  65. /package/{utils.cjs.js → utils.cjs} +0 -0
  66. /package/{utils.esm.js → utils.js} +0 -0
package/LICENSE CHANGED
@@ -1,21 +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.
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 CHANGED
@@ -1,252 +1,252 @@
1
- # 📜 Hono OpenAPI
2
-
3
- [![npm version](https://img.shields.io/npm/v/hono-openapi.svg)](https://npmjs.org/package/hono-openapi "View this project on NPM")
4
- [![npm downloads](https://img.shields.io/npm/dm/hono-openapi)](https://www.npmjs.com/package/hono-openapi)
5
- [![license](https://img.shields.io/npm/l/hono-openapi)](LICENSE)
6
-
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.
8
-
9
- Supported Validation Libraries:
10
-
11
- - [x] [Zod](https://zod.dev/)
12
- - [x] [Valibot](https://valibot.dev/)
13
- - [x] [ArkType](https://arktype.io/)
14
- - [x] [TypeBox](https://github.com/sinclairzx81/typebox)
15
- - [x] [Effect](https://effect.website/docs/schema/introduction/)
16
-
17
- > [!Note]
18
- > 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.
19
-
20
- ## Usage
21
-
22
- ### Installation
23
-
24
- You can install the package using favorite package manager.
25
-
26
- #### For Zod
27
-
28
- ```bash
29
- pnpm add hono-openapi @hono/zod-validator zod zod-openapi
30
- ```
31
-
32
- #### For Valibot
33
-
34
- ```bash
35
- pnpm add hono-openapi @hono/valibot-validator valibot @valibot/to-json-schema
36
- ```
37
-
38
- #### For ArkType
39
-
40
- ```bash
41
- pnpm add hono-openapi @hono/arktype-validator arktype
42
- ```
43
-
44
- #### For TypeBox
45
-
46
- ```bash
47
- pnpm add hono-openapi @hono/typebox-validator @sinclair/typebox"
48
- ```
49
-
50
- #### For Effect
51
-
52
- ```bash
53
- pnpm add hono-openapi @hono/effect-validator effect
54
- ```
55
-
56
- > [!IMPORTANT]
57
- >
58
- > Requires `effect@^3.10.0`.
59
- > Also, use the `Schema` class from the `effect` package, as `@effect/schema` is not supported.
60
-
61
- ### Basic Usage
62
-
63
- #### Setting up your application
64
-
65
- First, define your schemas, here is an example using Zod:
66
-
67
- ```ts
68
- import z from "zod";
69
-
70
- // For extending the Zod schema with OpenAPI properties
71
- import "zod-openapi/extend";
72
-
73
- const querySchema = z
74
- .object({
75
- name: z.string().optional().openapi({ example: "Steven" }),
76
- })
77
- .openapi({ ref: "Query" });
78
-
79
- const responseSchema = z.string().openapi({ example: "Hello Steven!" });
80
- ```
81
-
82
- 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).
83
-
84
- > [!Tip]
85
- > The `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.
86
-
87
- Next, create your route -
88
-
89
- ```ts
90
- import { Hono } from "hono";
91
- import { describeRoute } from "hono-openapi";
92
- import { resolver, validator as zValidator } from "hono-openapi/zod";
93
-
94
- const app = new Hono();
95
-
96
- app.get(
97
- "/",
98
- describeRoute({
99
- description: "Say hello to the user",
100
- responses: {
101
- 200: {
102
- description: "Successful greeting response",
103
- content: {
104
- "text/plain": {
105
- schema: resolver(responseSchema),
106
- },
107
- },
108
- },
109
- },
110
- }),
111
- zValidator("query", querySchema),
112
- (c) => {
113
- const query = c.req.valid("query");
114
- return c.text(`Hello ${query?.name ?? "Hono"}!`);
115
- }
116
- );
117
- ```
118
-
119
- 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.
120
-
121
- Finally, generate the OpenAPI specification -
122
-
123
- ```ts
124
- app.get(
125
- "/openapi",
126
- openAPISpecs(app, {
127
- documentation: {
128
- info: {
129
- title: "Hono",
130
- version: "1.0.0",
131
- description: "API for greeting users",
132
- },
133
- servers: [
134
- {
135
- url: "http://localhost:3000",
136
- description: "Local server",
137
- },
138
- ],
139
- },
140
- })
141
- );
142
- ```
143
-
144
- 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 -
145
-
146
- - [Swagger UI](https://github.com/honojs/middleware/tree/main/packages/swagger-ui)
147
- - [Scalar](https://github.com/scalar/scalar/tree/main/packages/hono-api-reference)
148
-
149
- ##### Scalar Example
150
-
151
- ```ts
152
- app.get(
153
- "/docs",
154
- apiReference({
155
- theme: "saturn",
156
- spec: {
157
- url: "/openapi",
158
- },
159
- })
160
- );
161
- ```
162
-
163
- And that's it! You have successfully generated the OpenAPI specification for your Hono API.
164
-
165
- ### Advanced Usage
166
-
167
- #### Adding Security Definitions
168
-
169
- You can add security definitions to your OpenAPI specification by using the `security` property in the `openAPISpecs` function.
170
-
171
- ```ts
172
- app.get(
173
- "/openapi",
174
- openAPISpecs(appRouter, {
175
- documentation: {
176
- info: {
177
- title: "Rhinobase Cloud",
178
- version: "1.0.0",
179
- description: "API Documentation",
180
- },
181
- components: {
182
- securitySchemes: {
183
- bearerAuth: {
184
- type: "http",
185
- scheme: "bearer",
186
- bearerFormat: "JWT",
187
- },
188
- },
189
- },
190
- security: [
191
- {
192
- bearerAuth: [],
193
- },
194
- ],
195
- servers: [
196
- {
197
- url: "http://localhost:3004",
198
- description: "Local server",
199
- },
200
- ],
201
- },
202
- })
203
- );
204
- ```
205
-
206
- #### Conditionaly Hiding Routes
207
-
208
- You can conditionally hide routes from the OpenAPI specification by using the `hide` property in the `describeRoute` function.
209
-
210
- ```ts
211
- app.get(
212
- "/",
213
- describeRoute({
214
- // ...
215
- hide: process.env.NODE_ENV === "production",
216
- }),
217
- (c) => {
218
- return c.text("Private Route");
219
- }
220
- );
221
- ```
222
-
223
- #### Validating Responses
224
-
225
- > [!Warning]
226
- > Experimental
227
-
228
- You can validate the responses using the `validateResponse` property in the `describeRoute` function. This will validate the response against the schema and return an error if the response is invalid.
229
-
230
- ```ts
231
- app.get(
232
- "/",
233
- describeRoute({
234
- // ...
235
- validateResponse: true,
236
- }),
237
- (c) => {
238
- return c.json({ message: "This response will be validated" });
239
- }
240
- );
241
- ```
242
-
243
- ## Contributing
244
-
245
- We would love to have more contributors involved!
246
-
247
- To get started, please read our [Contributing Guide](https://github.com/rhinobase/hono-openapi/blob/main/CONTRIBUTING.md).
248
-
249
- ## Credits
250
-
251
- - 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.
252
- - 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.
1
+ # 📜 Hono OpenAPI
2
+
3
+ [![npm version](https://img.shields.io/npm/v/hono-openapi.svg)](https://npmjs.org/package/hono-openapi "View this project on NPM")
4
+ [![npm downloads](https://img.shields.io/npm/dm/hono-openapi)](https://www.npmjs.com/package/hono-openapi)
5
+ [![license](https://img.shields.io/npm/l/hono-openapi)](LICENSE)
6
+
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.
8
+
9
+ Supported Validation Libraries:
10
+
11
+ - [x] [Zod](https://zod.dev/)
12
+ - [x] [Valibot](https://valibot.dev/)
13
+ - [x] [ArkType](https://arktype.io/)
14
+ - [x] [TypeBox](https://github.com/sinclairzx81/typebox)
15
+ - [x] [Effect](https://effect.website/docs/schema/introduction/)
16
+
17
+ > [!Note]
18
+ > 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.
19
+
20
+ ## Usage
21
+
22
+ ### Installation
23
+
24
+ You can install the package using favorite package manager.
25
+
26
+ #### For Zod
27
+
28
+ ```bash
29
+ pnpm add hono-openapi @hono/zod-validator zod zod-openapi
30
+ ```
31
+
32
+ #### For Valibot
33
+
34
+ ```bash
35
+ pnpm add hono-openapi @hono/valibot-validator valibot @valibot/to-json-schema
36
+ ```
37
+
38
+ #### For ArkType
39
+
40
+ ```bash
41
+ pnpm add hono-openapi @hono/arktype-validator arktype
42
+ ```
43
+
44
+ #### For TypeBox
45
+
46
+ ```bash
47
+ pnpm add hono-openapi @hono/typebox-validator @sinclair/typebox"
48
+ ```
49
+
50
+ #### For Effect
51
+
52
+ ```bash
53
+ pnpm add hono-openapi @hono/effect-validator effect
54
+ ```
55
+
56
+ > [!IMPORTANT]
57
+ >
58
+ > Requires `effect@^3.10.0`.
59
+ > Also, use the `Schema` class from the `effect` package, as `@effect/schema` is not supported.
60
+
61
+ ### Basic Usage
62
+
63
+ #### Setting up your application
64
+
65
+ First, define your schemas, here is an example using Zod:
66
+
67
+ ```ts
68
+ import z from "zod";
69
+
70
+ // For extending the Zod schema with OpenAPI properties
71
+ import "zod-openapi/extend";
72
+
73
+ const querySchema = z
74
+ .object({
75
+ name: z.string().optional().openapi({ example: "Steven" }),
76
+ })
77
+ .openapi({ ref: "Query" });
78
+
79
+ const responseSchema = z.string().openapi({ example: "Hello Steven!" });
80
+ ```
81
+
82
+ 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).
83
+
84
+ > [!Tip]
85
+ > The `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.
86
+
87
+ Next, create your route -
88
+
89
+ ```ts
90
+ import { Hono } from "hono";
91
+ import { describeRoute } from "hono-openapi";
92
+ import { resolver, validator as zValidator } from "hono-openapi/zod";
93
+
94
+ const app = new Hono();
95
+
96
+ app.get(
97
+ "/",
98
+ describeRoute({
99
+ description: "Say hello to the user",
100
+ responses: {
101
+ 200: {
102
+ description: "Successful greeting response",
103
+ content: {
104
+ "text/plain": {
105
+ schema: resolver(responseSchema),
106
+ },
107
+ },
108
+ },
109
+ },
110
+ }),
111
+ zValidator("query", querySchema),
112
+ (c) => {
113
+ const query = c.req.valid("query");
114
+ return c.text(`Hello ${query?.name ?? "Hono"}!`);
115
+ }
116
+ );
117
+ ```
118
+
119
+ 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.
120
+
121
+ Finally, generate the OpenAPI specification -
122
+
123
+ ```ts
124
+ app.get(
125
+ "/openapi",
126
+ openAPISpecs(app, {
127
+ documentation: {
128
+ info: {
129
+ title: "Hono",
130
+ version: "1.0.0",
131
+ description: "API for greeting users",
132
+ },
133
+ servers: [
134
+ {
135
+ url: "http://localhost:3000",
136
+ description: "Local server",
137
+ },
138
+ ],
139
+ },
140
+ })
141
+ );
142
+ ```
143
+
144
+ 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 -
145
+
146
+ - [Swagger UI](https://github.com/honojs/middleware/tree/main/packages/swagger-ui)
147
+ - [Scalar](https://github.com/scalar/scalar/tree/main/packages/hono-api-reference)
148
+
149
+ ##### Scalar Example
150
+
151
+ ```ts
152
+ app.get(
153
+ "/docs",
154
+ apiReference({
155
+ theme: "saturn",
156
+ spec: {
157
+ url: "/openapi",
158
+ },
159
+ })
160
+ );
161
+ ```
162
+
163
+ And that's it! You have successfully generated the OpenAPI specification for your Hono API.
164
+
165
+ ### Advanced Usage
166
+
167
+ #### Adding Security Definitions
168
+
169
+ You can add security definitions to your OpenAPI specification by using the `security` property in the `openAPISpecs` function.
170
+
171
+ ```ts
172
+ app.get(
173
+ "/openapi",
174
+ openAPISpecs(appRouter, {
175
+ documentation: {
176
+ info: {
177
+ title: "Rhinobase Cloud",
178
+ version: "1.0.0",
179
+ description: "API Documentation",
180
+ },
181
+ components: {
182
+ securitySchemes: {
183
+ bearerAuth: {
184
+ type: "http",
185
+ scheme: "bearer",
186
+ bearerFormat: "JWT",
187
+ },
188
+ },
189
+ },
190
+ security: [
191
+ {
192
+ bearerAuth: [],
193
+ },
194
+ ],
195
+ servers: [
196
+ {
197
+ url: "http://localhost:3004",
198
+ description: "Local server",
199
+ },
200
+ ],
201
+ },
202
+ })
203
+ );
204
+ ```
205
+
206
+ #### Conditionaly Hiding Routes
207
+
208
+ You can conditionally hide routes from the OpenAPI specification by using the `hide` property in the `describeRoute` function.
209
+
210
+ ```ts
211
+ app.get(
212
+ "/",
213
+ describeRoute({
214
+ // ...
215
+ hide: process.env.NODE_ENV === "production",
216
+ }),
217
+ (c) => {
218
+ return c.text("Private Route");
219
+ }
220
+ );
221
+ ```
222
+
223
+ #### Validating Responses
224
+
225
+ > [!Warning]
226
+ > Experimental
227
+
228
+ You can validate the responses using the `validateResponse` property in the `describeRoute` function. This will validate the response against the schema and return an error if the response is invalid.
229
+
230
+ ```ts
231
+ app.get(
232
+ "/",
233
+ describeRoute({
234
+ // ...
235
+ validateResponse: true,
236
+ }),
237
+ (c) => {
238
+ return c.json({ message: "This response will be validated" });
239
+ }
240
+ );
241
+ ```
242
+
243
+ ## Contributing
244
+
245
+ We would love to have more contributors involved!
246
+
247
+ To get started, please read our [Contributing Guide](https://github.com/rhinobase/hono-openapi/blob/main/CONTRIBUTING.md).
248
+
249
+ ## Credits
250
+
251
+ - 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.
252
+ - 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/arktype.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";var e=require("@hono/arktype-validator"),r=require("./toOpenAPISchema.cjs"),a=require("./utils.cjs");function t(e){return{builder:async a=>({schema:await r.convert(e.toJsonSchema())}),validator:r=>{e(r)}}}require("json-schema-walker"),exports.resolver=t,exports.validator=function(r,o,i){const s=e.arktypeValidator(r,o,i);return Object.assign(s,{[a.uniqueSymbol]:{resolver:async e=>a.generateValidatorDocs(r,await t(o).builder(e))}})};
package/arktype.d.cts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/arktype";
package/arktype.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/arktype.js";
package/arktype.js ADDED
@@ -0,0 +1 @@
1
+ import{arktypeValidator as a}from"@hono/arktype-validator";import{c as o}from"./toOpenAPISchema.js";import{u as r,g as t}from"./utils.js";import"json-schema-walker";function s(a){return{builder:async r=>({schema:await o(a.toJsonSchema())}),validator:o=>{a(o)}}}function e(o,e,i){const n=a(o,e,i);return Object.assign(n,{[r]:{resolver:async a=>t(o,await s(e).builder(a))}})}export{s as resolver,e as validator};
package/effect.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";var e=require("@hono/effect-validator"),r=require("effect"),a=require("./toOpenAPISchema.cjs"),t=require("./utils.cjs");function i(e){return{builder:async t=>({schema:await a.convert(r.JSONSchema.make(e))}),validator:async a=>{await r.Schema.decodeUnknownPromise(e)(a)}}}require("json-schema-walker"),exports.resolver=i,exports.validator=function(r,a){const o=e.effectValidator(r,a);return Object.assign(o,{[t.uniqueSymbol]:{resolver:async e=>t.generateValidatorDocs(r,await i(a).builder(e))}})};
package/effect.d.cts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/effect";
package/effect.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/effect.js";
package/effect.js ADDED
@@ -0,0 +1 @@
1
+ import{effectValidator as o}from"@hono/effect-validator";import{JSONSchema as a,Schema as r}from"effect";import{c as e}from"./toOpenAPISchema.js";import{u as t,g as i}from"./utils.js";import"json-schema-walker";function n(o){return{builder:async r=>({schema:await e(a.make(o))}),validator:async a=>{await r.decodeUnknownPromise(o)(a)}}}function s(a,r){const e=o(a,r);return Object.assign(e,{[t]:{resolver:async o=>i(a,await n(r).builder(o))}})}export{n as resolver,s as validator};
package/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";var e=require("hono/http-exception"),t=require("./utils.cjs");const n=["GET","PUT","POST","DELETE","OPTIONS","HEAD","PATCH","TRACE"],s=e=>e.charAt(0).toUpperCase()+e.slice(1),o=(e,t)=>{let n=e;if("/"===t)return`${n}Index`;for(const e of t.split("/"))123===e.charCodeAt(0)?n+=`By${s(e.slice(1,-1))}`:n+=s(e);return n};function c({path:e,method:t,data:n,schema:s}){e=(e=>e.split("/").map((e=>{let t=e;return t.startsWith(":")&&(t=t.slice(1,t.length),t.endsWith("?")&&(t=t.slice(0,-1)),t=`{${t}}`),t})).join("/"))(e);const c=t.toLowerCase();s[e]={...s[e]?s[e]:{},[c]:{responses:{},...s[e]?.[c]??{},operationId:o(c,e),...n}}}function i(e,{excludeStaticFile:t=!0,exclude:n=[]}){const s={};for(const[o,c]of Object.entries(e))if(!(n.some((e=>"string"==typeof e?o===e:e.test(o)))||o.includes("*")||t&&o.includes("."))){for(const e of Object.keys(c)){const t=c[e];if(o.includes("{")){t.parameters||(t.parameters=[]);const e=o.split("/").filter((e=>e.startsWith("{")&&!t.parameters.find((t=>"path"===t.in&&t.name===e.slice(1,e.length-1)))));for(const n of e){const e=n.slice(1,n.length-1),s=t.parameters.findIndex((t=>"param"===t.in&&t.name===e));-1!==s?t.parameters[s].in="path":t.parameters.push({schema:{type:"string"},in:"path",name:e,required:!0})}}t.responses||(t.responses={200:{}})}s[o]=c}return s}async function a(e,{documentation:s={},excludeStaticFile:o=!0,exclude:a=[],excludeMethods:r=["OPTIONS"],excludeTags:p=[],defaultOptions:l}={documentation:{},excludeStaticFile:!0,exclude:[],excludeMethods:["OPTIONS"],excludeTags:[]},{version:d="3.1.0",components:m={}}={version:"3.1.0",components:{}},u){const h={version:d,components:m},f={};for(const s of e.routes){if(!(t.uniqueSymbol in s.handler))continue;if(r.includes(s.method))continue;if(!1===n.includes(s.method)&&"ALL"!==s.method)continue;const{resolver:e,metadata:o={}}=s.handler[t.uniqueSymbol],i=l?.[s.method],{docs:a,components:p}=await e({...h,...o},i);if(h.components={...h.components,...p??{}},"ALL"===s.method)for(const e of n)c({path:s.path,data:a,method:e,schema:f});else c({method:s.method,path:s.path,data:a,schema:f})}for(const e in f)for(const t in f[e]){const n=f[e][t]?.hide;n&&("boolean"==typeof n?n:u&&n(u))&&delete f[e][t]}return{openapi:h.version,...{...s,tags:s.tags?.filter((e=>!p?.includes(e?.name))),info:{title:"Hono Documentation",description:"Development documentation",version:"0.0.0",...s.info},paths:{...i(f,{excludeStaticFile:o,exclude:Array.isArray(a)?a:[a]}),...s.paths},components:{...s.components,schemas:{...h.components,...s.components?.schemas}}}}}exports.describeRoute=function(n){const{validateResponse:s,...o}=n;return Object.assign((async(t,o)=>{if(await o(),s&&n.responses){const s=t.res.status,o=t.res.headers.get("content-type");if(s&&o){const c=n.responses[s];if(c&&"content"in c&&c.content){const n=o.split(";")[0],s=c.content[n];if(s?.schema&&"validator"in s.schema)try{let e;if("application/json"===n?e=await t.res.json():"text/plain"===n&&(e=await t.res.text()),!e)throw new Error("No data to validate!");await s.schema.validator(e)}catch(t){throw new e.HTTPException(400,{message:"Response validation failed!"})}}}}}),{[t.uniqueSymbol]:{resolver:(e,t)=>async function(e,t,n={}){let s={};const o={...n,...t,responses:{...n?.responses,...t.responses}};for(const t of Object.keys(o.responses)){const n=o.responses[t];if(n&&"content"in n)for(const t of Object.keys(n.content??{})){const o=n.content?.[t];if(o&&(o.schema&&"builder"in o.schema)){const t=await o.schema.builder(e);o.schema=t.schema,t.components&&(s={...s,...t.components})}}}return{docs:o,components:s}}(e,o,t)}})},exports.generateSpecs=a,exports.openAPISpecs=function(e,t){const n={version:"3.1.0",components:{}};let s=null;return async o=>(s||(s=await a(e,t,n,o)),o.json(s))};
package/index.d.cts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/index";
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/index.js";
package/index.js ADDED
@@ -0,0 +1 @@
1
+ import{HTTPException as e}from"hono/http-exception";import{u as t}from"./utils.js";function n(n){const{validateResponse:s,...o}=n;return Object.assign((async(t,o)=>{if(await o(),s&&n.responses){const s=t.res.status,o=t.res.headers.get("content-type");if(s&&o){const c=n.responses[s];if(c&&"content"in c&&c.content){const n=o.split(";")[0],s=c.content[n];if(s?.schema&&"validator"in s.schema)try{let e;if("application/json"===n?e=await t.res.json():"text/plain"===n&&(e=await t.res.text()),!e)throw new Error("No data to validate!");await s.schema.validator(e)}catch(t){throw new e(400,{message:"Response validation failed!"})}}}}}),{[t]:{resolver:(e,t)=>async function(e,t,n={}){let s={};const o={...n,...t,responses:{...n?.responses,...t.responses}};for(const t of Object.keys(o.responses)){const n=o.responses[t];if(n&&"content"in n)for(const t of Object.keys(n.content??{})){const o=n.content?.[t];if(o&&(o.schema&&"builder"in o.schema)){const t=await o.schema.builder(e);o.schema=t.schema,t.components&&(s={...s,...t.components})}}}return{docs:o,components:s}}(e,o,t)}})}const s=["GET","PUT","POST","DELETE","OPTIONS","HEAD","PATCH","TRACE"],o=e=>e.charAt(0).toUpperCase()+e.slice(1),c=(e,t)=>{let n=e;if("/"===t)return`${n}Index`;for(const e of t.split("/"))123===e.charCodeAt(0)?n+=`By${o(e.slice(1,-1))}`:n+=o(e);return n};function a({path:e,method:t,data:n,schema:s}){e=(e=>e.split("/").map((e=>{let t=e;return t.startsWith(":")&&(t=t.slice(1,t.length),t.endsWith("?")&&(t=t.slice(0,-1)),t=`{${t}}`),t})).join("/"))(e);const o=t.toLowerCase();s[e]={...s[e]?s[e]:{},[o]:{responses:{},...s[e]?.[o]??{},operationId:c(o,e),...n}}}function i(e,{excludeStaticFile:t=!0,exclude:n=[]}){const s={};for(const[o,c]of Object.entries(e))if(!(n.some((e=>"string"==typeof e?o===e:e.test(o)))||o.includes("*")||t&&o.includes("."))){for(const e of Object.keys(c)){const t=c[e];if(o.includes("{")){t.parameters||(t.parameters=[]);const e=o.split("/").filter((e=>e.startsWith("{")&&!t.parameters.find((t=>"path"===t.in&&t.name===e.slice(1,e.length-1)))));for(const n of e){const e=n.slice(1,n.length-1),s=t.parameters.findIndex((t=>"param"===t.in&&t.name===e));-1!==s?t.parameters[s].in="path":t.parameters.push({schema:{type:"string"},in:"path",name:e,required:!0})}}t.responses||(t.responses={200:{}})}s[o]=c}return s}function r(e,t){const n={version:"3.1.0",components:{}};let s=null;return async o=>(s||(s=await p(e,t,n,o)),o.json(s))}async function p(e,{documentation:n={},excludeStaticFile:o=!0,exclude:c=[],excludeMethods:r=["OPTIONS"],excludeTags:p=[],defaultOptions:l}={documentation:{},excludeStaticFile:!0,exclude:[],excludeMethods:["OPTIONS"],excludeTags:[]},{version:d="3.1.0",components:m={}}={version:"3.1.0",components:{}},h){const f={version:d,components:m},u={};for(const n of e.routes){if(!(t in n.handler))continue;if(r.includes(n.method))continue;if(!1===s.includes(n.method)&&"ALL"!==n.method)continue;const{resolver:e,metadata:o={}}=n.handler[t],c=l?.[n.method],{docs:i,components:p}=await e({...f,...o},c);if(f.components={...f.components,...p??{}},"ALL"===n.method)for(const e of s)a({path:n.path,data:i,method:e,schema:u});else a({method:n.method,path:n.path,data:i,schema:u})}for(const e in u)for(const t in u[e]){const n=u[e][t]?.hide;n&&("boolean"==typeof n?n:h&&n(h))&&delete u[e][t]}return{openapi:f.version,...{...n,tags:n.tags?.filter((e=>!p?.includes(e?.name))),info:{title:"Hono Documentation",description:"Development documentation",version:"0.0.0",...n.info},paths:{...i(u,{excludeStaticFile:o,exclude:Array.isArray(c)?c:[c]}),...n.paths},components:{...n.components,schemas:{...f.components,...n.components?.schemas}}}}}export{n as describeRoute,p as generateSpecs,r as openAPISpecs};