@hono/zod-openapi 0.9.4 → 0.9.6
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 +11 -13
- package/dist/index.js +15 -6
- package/dist/index.mjs +15 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -88,7 +88,7 @@ const app = new OpenAPIHono()
|
|
|
88
88
|
|
|
89
89
|
app.openapi(route, (c) => {
|
|
90
90
|
const { id } = c.req.valid('param')
|
|
91
|
-
return c.
|
|
91
|
+
return c.json({
|
|
92
92
|
id,
|
|
93
93
|
age: 20,
|
|
94
94
|
name: 'Ultra-man',
|
|
@@ -157,7 +157,7 @@ app.openapi(
|
|
|
157
157
|
route,
|
|
158
158
|
(c) => {
|
|
159
159
|
const { id } = c.req.valid('param')
|
|
160
|
-
return c.
|
|
160
|
+
return c.json({
|
|
161
161
|
id,
|
|
162
162
|
age: 20,
|
|
163
163
|
name: 'Ultra-man',
|
|
@@ -166,7 +166,7 @@ app.openapi(
|
|
|
166
166
|
// Hook
|
|
167
167
|
(result, c) => {
|
|
168
168
|
if (!result.success) {
|
|
169
|
-
return c.
|
|
169
|
+
return c.json(
|
|
170
170
|
{
|
|
171
171
|
code: 400,
|
|
172
172
|
message: 'Validation Error',
|
|
@@ -186,7 +186,7 @@ In the case that you have a common error formatter, you can initialize the `Open
|
|
|
186
186
|
const app = new OpenAPIHono({
|
|
187
187
|
defaultHook: (result, c) => {
|
|
188
188
|
if (!result.success) {
|
|
189
|
-
return c.
|
|
189
|
+
return c.json(
|
|
190
190
|
{
|
|
191
191
|
ok: false,
|
|
192
192
|
errors: formatZodErrors(result),
|
|
@@ -205,7 +205,7 @@ You can still override the `defaultHook` by providing the hook at the call site
|
|
|
205
205
|
// uses the defaultHook
|
|
206
206
|
app.openapi(createPostRoute, (c) => {
|
|
207
207
|
const { title } = c.req.valid('json')
|
|
208
|
-
return c.
|
|
208
|
+
return c.json({ title })
|
|
209
209
|
})
|
|
210
210
|
|
|
211
211
|
// override the defaultHook by passing in a hook
|
|
@@ -213,11 +213,11 @@ app.openapi(
|
|
|
213
213
|
createBookRoute,
|
|
214
214
|
(c) => {
|
|
215
215
|
const { title } = c.req.valid('json')
|
|
216
|
-
return c.
|
|
216
|
+
return c.json({ title })
|
|
217
217
|
},
|
|
218
218
|
(result, c) => {
|
|
219
219
|
if (!result.success) {
|
|
220
|
-
return c.
|
|
220
|
+
return c.json(
|
|
221
221
|
{
|
|
222
222
|
ok: false,
|
|
223
223
|
source: 'routeHook' as const,
|
|
@@ -279,7 +279,7 @@ import { hc } from 'hono/client'
|
|
|
279
279
|
|
|
280
280
|
const appRoutes = app.openapi(route, (c) => {
|
|
281
281
|
const data = c.req.valid('json')
|
|
282
|
-
return c.
|
|
282
|
+
return c.json({
|
|
283
283
|
id: data.id,
|
|
284
284
|
message: 'Success',
|
|
285
285
|
})
|
|
@@ -311,11 +311,9 @@ eg. Bearer Auth
|
|
|
311
311
|
Register the security scheme:
|
|
312
312
|
|
|
313
313
|
```ts
|
|
314
|
-
app.openAPIRegistry.registerComponent(
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
scheme: 'bearer',
|
|
318
|
-
},
|
|
314
|
+
app.openAPIRegistry.registerComponent("securitySchemes", "Bearer", {
|
|
315
|
+
type: "http",
|
|
316
|
+
scheme: "bearer",
|
|
319
317
|
})
|
|
320
318
|
```
|
|
321
319
|
|
package/dist/index.js
CHANGED
|
@@ -93,18 +93,27 @@ var OpenAPIHono = class _OpenAPIHono extends import_hono.Hono {
|
|
|
93
93
|
doc = (path, configure) => {
|
|
94
94
|
return this.get(path, (c) => {
|
|
95
95
|
const config = typeof configure === "function" ? configure(c) : configure;
|
|
96
|
-
|
|
97
|
-
|
|
96
|
+
try {
|
|
97
|
+
const document = this.getOpenAPIDocument(config);
|
|
98
|
+
return c.json(document);
|
|
99
|
+
} catch (e) {
|
|
100
|
+
return c.json(e, 500);
|
|
101
|
+
}
|
|
98
102
|
});
|
|
99
103
|
};
|
|
100
104
|
doc31 = (path, configure) => {
|
|
101
105
|
return this.get(path, (c) => {
|
|
102
106
|
const config = typeof configure === "function" ? configure(c) : configure;
|
|
103
|
-
|
|
104
|
-
|
|
107
|
+
try {
|
|
108
|
+
const document = this.getOpenAPI31Document(config);
|
|
109
|
+
return c.json(document);
|
|
110
|
+
} catch (e) {
|
|
111
|
+
return c.json(e, 500);
|
|
112
|
+
}
|
|
105
113
|
});
|
|
106
114
|
};
|
|
107
115
|
route(path, app) {
|
|
116
|
+
const pathForOpenAPI = path.replaceAll(/:([^\/]+)/g, "{$1}");
|
|
108
117
|
super.route(path, app);
|
|
109
118
|
if (!(app instanceof _OpenAPIHono)) {
|
|
110
119
|
return this;
|
|
@@ -116,12 +125,12 @@ var OpenAPIHono = class _OpenAPIHono extends import_hono.Hono {
|
|
|
116
125
|
case "route":
|
|
117
126
|
return this.openAPIRegistry.registerPath({
|
|
118
127
|
...def.route,
|
|
119
|
-
path: (0, import_url.mergePath)(
|
|
128
|
+
path: (0, import_url.mergePath)(pathForOpenAPI, def.route.path)
|
|
120
129
|
});
|
|
121
130
|
case "webhook":
|
|
122
131
|
return this.openAPIRegistry.registerWebhook({
|
|
123
132
|
...def.webhook,
|
|
124
|
-
path: (0, import_url.mergePath)(
|
|
133
|
+
path: (0, import_url.mergePath)(pathForOpenAPI, def.webhook.path)
|
|
125
134
|
});
|
|
126
135
|
case "schema":
|
|
127
136
|
return this.openAPIRegistry.register(def.schema._def.openapi._internal.refId, def.schema);
|
package/dist/index.mjs
CHANGED
|
@@ -71,18 +71,27 @@ var OpenAPIHono = class _OpenAPIHono extends Hono {
|
|
|
71
71
|
doc = (path, configure) => {
|
|
72
72
|
return this.get(path, (c) => {
|
|
73
73
|
const config = typeof configure === "function" ? configure(c) : configure;
|
|
74
|
-
|
|
75
|
-
|
|
74
|
+
try {
|
|
75
|
+
const document = this.getOpenAPIDocument(config);
|
|
76
|
+
return c.json(document);
|
|
77
|
+
} catch (e) {
|
|
78
|
+
return c.json(e, 500);
|
|
79
|
+
}
|
|
76
80
|
});
|
|
77
81
|
};
|
|
78
82
|
doc31 = (path, configure) => {
|
|
79
83
|
return this.get(path, (c) => {
|
|
80
84
|
const config = typeof configure === "function" ? configure(c) : configure;
|
|
81
|
-
|
|
82
|
-
|
|
85
|
+
try {
|
|
86
|
+
const document = this.getOpenAPI31Document(config);
|
|
87
|
+
return c.json(document);
|
|
88
|
+
} catch (e) {
|
|
89
|
+
return c.json(e, 500);
|
|
90
|
+
}
|
|
83
91
|
});
|
|
84
92
|
};
|
|
85
93
|
route(path, app) {
|
|
94
|
+
const pathForOpenAPI = path.replaceAll(/:([^\/]+)/g, "{$1}");
|
|
86
95
|
super.route(path, app);
|
|
87
96
|
if (!(app instanceof _OpenAPIHono)) {
|
|
88
97
|
return this;
|
|
@@ -94,12 +103,12 @@ var OpenAPIHono = class _OpenAPIHono extends Hono {
|
|
|
94
103
|
case "route":
|
|
95
104
|
return this.openAPIRegistry.registerPath({
|
|
96
105
|
...def.route,
|
|
97
|
-
path: mergePath(
|
|
106
|
+
path: mergePath(pathForOpenAPI, def.route.path)
|
|
98
107
|
});
|
|
99
108
|
case "webhook":
|
|
100
109
|
return this.openAPIRegistry.registerWebhook({
|
|
101
110
|
...def.webhook,
|
|
102
|
-
path: mergePath(
|
|
111
|
+
path: mergePath(pathForOpenAPI, def.webhook.path)
|
|
103
112
|
});
|
|
104
113
|
case "schema":
|
|
105
114
|
return this.openAPIRegistry.register(def.schema._def.openapi._internal.refId, def.schema);
|