@hono/zod-openapi 0.11.0 → 0.12.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/README.md +32 -27
- package/dist/index.d.mts +48 -13
- package/dist/index.d.ts +48 -13
- package/dist/index.js +38 -1
- package/dist/index.mjs +38 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -51,8 +51,7 @@ const UserSchema = z
|
|
|
51
51
|
.openapi('User')
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
-
> [!TIP]
|
|
55
|
-
> `UserSchema` schema will be registered as `"#/components/schemas/User"` refs in the OpenAPI document.
|
|
54
|
+
> [!TIP] > `UserSchema` schema will be registered as `"#/components/schemas/User"` refs in the OpenAPI document.
|
|
56
55
|
> If you want to register the schema as referenced components, use `.openapi()` method.
|
|
57
56
|
|
|
58
57
|
Next, create a route:
|
|
@@ -88,11 +87,14 @@ const app = new OpenAPIHono()
|
|
|
88
87
|
|
|
89
88
|
app.openapi(route, (c) => {
|
|
90
89
|
const { id } = c.req.valid('param')
|
|
91
|
-
return c.json(
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
90
|
+
return c.json(
|
|
91
|
+
{
|
|
92
|
+
id,
|
|
93
|
+
age: 20,
|
|
94
|
+
name: 'Ultra-man',
|
|
95
|
+
},
|
|
96
|
+
200 // You should specify the status code even if it is 200.
|
|
97
|
+
)
|
|
96
98
|
})
|
|
97
99
|
|
|
98
100
|
// The OpenAPI documentation will be available at /doc
|
|
@@ -157,11 +159,14 @@ app.openapi(
|
|
|
157
159
|
route,
|
|
158
160
|
(c) => {
|
|
159
161
|
const { id } = c.req.valid('param')
|
|
160
|
-
return c.json(
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
162
|
+
return c.json(
|
|
163
|
+
{
|
|
164
|
+
id,
|
|
165
|
+
age: 20,
|
|
166
|
+
name: 'Ultra-man',
|
|
167
|
+
},
|
|
168
|
+
200
|
|
169
|
+
)
|
|
165
170
|
},
|
|
166
171
|
// Hook
|
|
167
172
|
(result, c) => {
|
|
@@ -213,7 +218,7 @@ app.openapi(
|
|
|
213
218
|
createBookRoute,
|
|
214
219
|
(c) => {
|
|
215
220
|
const { title } = c.req.valid('json')
|
|
216
|
-
return c.json({ title })
|
|
221
|
+
return c.json({ title }, 200)
|
|
217
222
|
},
|
|
218
223
|
(result, c) => {
|
|
219
224
|
if (!result.success) {
|
|
@@ -234,8 +239,8 @@ app.openapi(
|
|
|
234
239
|
You can generate OpenAPI v3.1 spec using the following methods:
|
|
235
240
|
|
|
236
241
|
```ts
|
|
237
|
-
app.doc31('/docs', {openapi: '3.1.0'}) // new endpoint
|
|
238
|
-
app.getOpenAPI31Document({openapi: '3.1.0'}) // raw json
|
|
242
|
+
app.doc31('/docs', { openapi: '3.1.0' }) // new endpoint
|
|
243
|
+
app.getOpenAPI31Document({ openapi: '3.1.0' }) // raw json
|
|
239
244
|
```
|
|
240
245
|
|
|
241
246
|
### The Registry
|
|
@@ -279,10 +284,7 @@ const route = createRoute({
|
|
|
279
284
|
request: {
|
|
280
285
|
params: ParamsSchema,
|
|
281
286
|
},
|
|
282
|
-
middleware: [
|
|
283
|
-
prettyJSON(),
|
|
284
|
-
cache({ cacheName: 'my-cache' })
|
|
285
|
-
],
|
|
287
|
+
middleware: [prettyJSON(), cache({ cacheName: 'my-cache' })],
|
|
286
288
|
responses: {
|
|
287
289
|
200: {
|
|
288
290
|
content: {
|
|
@@ -305,10 +307,13 @@ import { hc } from 'hono/client'
|
|
|
305
307
|
|
|
306
308
|
const appRoutes = app.openapi(route, (c) => {
|
|
307
309
|
const data = c.req.valid('json')
|
|
308
|
-
return c.json(
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
310
|
+
return c.json(
|
|
311
|
+
{
|
|
312
|
+
id: data.id,
|
|
313
|
+
message: 'Success',
|
|
314
|
+
},
|
|
315
|
+
200
|
|
316
|
+
)
|
|
312
317
|
})
|
|
313
318
|
|
|
314
319
|
const client = hc<typeof appRoutes>('http://localhost:8787/')
|
|
@@ -337,9 +342,9 @@ eg. Bearer Auth
|
|
|
337
342
|
Register the security scheme:
|
|
338
343
|
|
|
339
344
|
```ts
|
|
340
|
-
app.openAPIRegistry.registerComponent(
|
|
341
|
-
type:
|
|
342
|
-
scheme:
|
|
345
|
+
app.openAPIRegistry.registerComponent('securitySchemes', 'Bearer', {
|
|
346
|
+
type: 'http',
|
|
347
|
+
scheme: 'bearer',
|
|
343
348
|
})
|
|
344
349
|
```
|
|
345
350
|
|
|
@@ -361,7 +366,7 @@ const route = createRoute({
|
|
|
361
366
|
You can access the context in `app.doc` as follows:
|
|
362
367
|
|
|
363
368
|
```ts
|
|
364
|
-
app.doc('/doc', c => ({
|
|
369
|
+
app.doc('/doc', (c) => ({
|
|
365
370
|
openapi: '3.0.0',
|
|
366
371
|
info: {
|
|
367
372
|
version: '1.0.0',
|
package/dist/index.d.mts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import * as openapi3_ts_oas31 from 'openapi3-ts/oas31';
|
|
2
2
|
import * as openapi3_ts_oas30 from 'openapi3-ts/oas30';
|
|
3
|
-
import { ZodMediaTypeObject, OpenAPIRegistry, RouteConfig as RouteConfig$1, ZodRequestBody, ZodContentObject
|
|
3
|
+
import { ZodMediaTypeObject, OpenAPIRegistry, RouteConfig as RouteConfig$1, ZodRequestBody, ZodContentObject } from '@asteasolutions/zod-to-openapi';
|
|
4
4
|
import { OpenAPIObjectConfig } from '@asteasolutions/zod-to-openapi/dist/v3.0/openapi-generator';
|
|
5
|
-
import { Env, Context,
|
|
5
|
+
import { TypedResponse, Env, Context, Input, Handler, Schema, Hono, ToSchema, MiddlewareHandler } from 'hono';
|
|
6
6
|
import { MergePath, MergeSchemaPath } from 'hono/types';
|
|
7
|
+
import { StatusCode } from 'hono/utils/http-status';
|
|
7
8
|
import { RemoveBlankRecord } from 'hono/utils/types';
|
|
8
9
|
import { ZodError, AnyZodObject, z, ZodSchema, ZodType } from 'zod';
|
|
9
10
|
export { z } from 'zod';
|
|
@@ -49,45 +50,79 @@ type InputTypeParam<R extends RouteConfig> = InputTypeBase<R, 'params', 'param'>
|
|
|
49
50
|
type InputTypeQuery<R extends RouteConfig> = InputTypeBase<R, 'query', 'query'>;
|
|
50
51
|
type InputTypeHeader<R extends RouteConfig> = InputTypeBase<R, 'headers', 'header'>;
|
|
51
52
|
type InputTypeCookie<R extends RouteConfig> = InputTypeBase<R, 'cookies', 'cookie'>;
|
|
52
|
-
type
|
|
53
|
-
|
|
53
|
+
type ExtractContent<T> = T extends {
|
|
54
|
+
[K in keyof T]: infer A;
|
|
55
|
+
} ? A extends Record<'schema', ZodSchema> ? z.infer<A['schema']> : never : never;
|
|
56
|
+
type RouteConfigToTypedResponse<R extends RouteConfig> = {
|
|
57
|
+
[Status in keyof R['responses'] & StatusCode]: IsJson<keyof R['responses'][Status]['content']> extends never ? TypedResponse<{}, Status, string> : TypedResponse<ExtractContent<R['responses'][Status]['content']>, Status, 'json'>;
|
|
58
|
+
}[keyof R['responses'] & StatusCode];
|
|
59
|
+
type Hook<T, E extends Env, P extends string, R> = (result: {
|
|
54
60
|
success: true;
|
|
55
61
|
data: T;
|
|
56
62
|
} | {
|
|
57
63
|
success: false;
|
|
58
64
|
error: ZodError;
|
|
59
|
-
}, c: Context<E, P>) =>
|
|
65
|
+
}, c: Context<E, P>) => R;
|
|
60
66
|
type ConvertPathType<T extends string> = T extends `${infer Start}/{${infer Param}}${infer Rest}` ? `${Start}/:${Param}${ConvertPathType<Rest>}` : T;
|
|
61
|
-
type HandlerTypedResponse<O> = TypedResponse<O> | Promise<TypedResponse<O>>;
|
|
62
|
-
type HandlerAllResponse<O> = Response | Promise<Response> | TypedResponse<O> | Promise<TypedResponse<O>>;
|
|
63
67
|
type OpenAPIHonoOptions<E extends Env> = {
|
|
64
68
|
defaultHook?: Hook<any, E, any, any>;
|
|
65
69
|
};
|
|
66
70
|
type HonoInit<E extends Env> = ConstructorParameters<typeof Hono>[0] & OpenAPIHonoOptions<E>;
|
|
67
71
|
type RouteHandler<R extends RouteConfig, E extends Env = Env, I extends Input = InputTypeParam<R> & InputTypeQuery<R> & InputTypeHeader<R> & InputTypeCookie<R> & InputTypeForm<R> & InputTypeJson<R>, P extends string = ConvertPathType<R['path']>> = Handler<E, P, I, R extends {
|
|
68
72
|
responses: {
|
|
69
|
-
[statusCode:
|
|
73
|
+
[statusCode: number]: {
|
|
70
74
|
content: {
|
|
71
75
|
[mediaType: string]: ZodMediaTypeObject;
|
|
72
76
|
};
|
|
73
77
|
};
|
|
74
78
|
};
|
|
75
|
-
} ?
|
|
76
|
-
type RouteHook<R extends RouteConfig, E extends Env = Env, I extends Input = InputTypeParam<R> & InputTypeQuery<R> & InputTypeHeader<R> & InputTypeCookie<R> & InputTypeForm<R> & InputTypeJson<R>, P extends string = ConvertPathType<R['path']>> = Hook<I, E, P,
|
|
79
|
+
} ? RouteConfigToTypedResponse<R> : RouteConfigToTypedResponse<R> | Response | Promise<Response>>;
|
|
80
|
+
type RouteHook<R extends RouteConfig, E extends Env = Env, I extends Input = InputTypeParam<R> & InputTypeQuery<R> & InputTypeHeader<R> & InputTypeCookie<R> & InputTypeForm<R> & InputTypeJson<R>, P extends string = ConvertPathType<R['path']>> = Hook<I, E, P, RouteConfigToTypedResponse<R> | Response | Promise<Response> | void | Promise<void>>;
|
|
77
81
|
type OpenAPIObjectConfigure<E extends Env, P extends string> = OpenAPIObjectConfig | ((context: Context<E, P>) => OpenAPIObjectConfig);
|
|
78
82
|
declare class OpenAPIHono<E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'> extends Hono<E, S, BasePath> {
|
|
79
83
|
openAPIRegistry: OpenAPIRegistry;
|
|
80
84
|
defaultHook?: OpenAPIHonoOptions<E>['defaultHook'];
|
|
81
85
|
constructor(init?: HonoInit<E>);
|
|
86
|
+
/**
|
|
87
|
+
*
|
|
88
|
+
* @param {RouteConfig} route - The route definition which you create with `createRoute()`.
|
|
89
|
+
* @param {Handler} handler - The handler. If you want to return a JSON object, you should specify the status code with `c.json()`.
|
|
90
|
+
* @param {Hook} hook - Optional. The hook method defines what it should do after validation.
|
|
91
|
+
* @example
|
|
92
|
+
* app.openapi(
|
|
93
|
+
* route,
|
|
94
|
+
* (c) => {
|
|
95
|
+
* // ...
|
|
96
|
+
* return c.json(
|
|
97
|
+
* {
|
|
98
|
+
* age: 20,
|
|
99
|
+
* name: 'Young man',
|
|
100
|
+
* },
|
|
101
|
+
* 200 // You should specify the status code even if it's 200.
|
|
102
|
+
* )
|
|
103
|
+
* },
|
|
104
|
+
* (result, c) => {
|
|
105
|
+
* if (!result.success) {
|
|
106
|
+
* return c.json(
|
|
107
|
+
* {
|
|
108
|
+
* code: 400,
|
|
109
|
+
* message: 'Custom Message',
|
|
110
|
+
* },
|
|
111
|
+
* 400
|
|
112
|
+
* )
|
|
113
|
+
* }
|
|
114
|
+
* }
|
|
115
|
+
*)
|
|
116
|
+
*/
|
|
82
117
|
openapi: <R extends RouteConfig, I extends Input = InputTypeBase<R, "params", "param"> & InputTypeBase<R, "query", "query"> & InputTypeBase<R, "headers", "header"> & InputTypeBase<R, "cookies", "cookie"> & InputTypeForm<R> & InputTypeJson<R>, P extends string = ConvertPathType<R["path"]>>(route: R, handler: Handler<E, P, I, R extends {
|
|
83
118
|
responses: {
|
|
84
|
-
[statusCode:
|
|
119
|
+
[statusCode: number]: {
|
|
85
120
|
content: {
|
|
86
121
|
[mediaType: string]: ZodMediaTypeObject;
|
|
87
122
|
};
|
|
88
123
|
};
|
|
89
124
|
};
|
|
90
|
-
} ?
|
|
125
|
+
} ? RouteConfigToTypedResponse<R> : RouteConfigToTypedResponse<R> | Response | Promise<Response>>, hook?: Hook<I, E, P, RouteConfigToTypedResponse<R> | Response | Promise<Response> | void | Promise<void>> | undefined) => OpenAPIHono<E, S & ToSchema<R['method'], MergePath<BasePath, P>, I, RouteConfigToTypedResponse<R>>, BasePath>;
|
|
91
126
|
getOpenAPIDocument: (config: OpenAPIObjectConfig) => openapi3_ts_oas30.OpenAPIObject;
|
|
92
127
|
getOpenAPI31Document: (config: OpenAPIObjectConfig) => openapi3_ts_oas31.OpenAPIObject;
|
|
93
128
|
doc: <P extends string>(path: P, configure: OpenAPIObjectConfigure<E, P>) => OpenAPIHono<E, S & ToSchema<'get', P, {}, {}>, BasePath>;
|
|
@@ -103,4 +138,4 @@ declare const createRoute: <P extends string, R extends Omit<RouteConfig, "path"
|
|
|
103
138
|
getRoutingPath(): RoutingPath<R['path']>;
|
|
104
139
|
};
|
|
105
140
|
|
|
106
|
-
export { type Hook, OpenAPIHono, type OpenAPIHonoOptions, type OpenAPIObjectConfigure, type RouteHandler, type RouteHook, createRoute };
|
|
141
|
+
export { type Hook, OpenAPIHono, type OpenAPIHonoOptions, type OpenAPIObjectConfigure, type RouteConfigToTypedResponse, type RouteHandler, type RouteHook, createRoute };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import * as openapi3_ts_oas31 from 'openapi3-ts/oas31';
|
|
2
2
|
import * as openapi3_ts_oas30 from 'openapi3-ts/oas30';
|
|
3
|
-
import { ZodMediaTypeObject, OpenAPIRegistry, RouteConfig as RouteConfig$1, ZodRequestBody, ZodContentObject
|
|
3
|
+
import { ZodMediaTypeObject, OpenAPIRegistry, RouteConfig as RouteConfig$1, ZodRequestBody, ZodContentObject } from '@asteasolutions/zod-to-openapi';
|
|
4
4
|
import { OpenAPIObjectConfig } from '@asteasolutions/zod-to-openapi/dist/v3.0/openapi-generator';
|
|
5
|
-
import { Env, Context,
|
|
5
|
+
import { TypedResponse, Env, Context, Input, Handler, Schema, Hono, ToSchema, MiddlewareHandler } from 'hono';
|
|
6
6
|
import { MergePath, MergeSchemaPath } from 'hono/types';
|
|
7
|
+
import { StatusCode } from 'hono/utils/http-status';
|
|
7
8
|
import { RemoveBlankRecord } from 'hono/utils/types';
|
|
8
9
|
import { ZodError, AnyZodObject, z, ZodSchema, ZodType } from 'zod';
|
|
9
10
|
export { z } from 'zod';
|
|
@@ -49,45 +50,79 @@ type InputTypeParam<R extends RouteConfig> = InputTypeBase<R, 'params', 'param'>
|
|
|
49
50
|
type InputTypeQuery<R extends RouteConfig> = InputTypeBase<R, 'query', 'query'>;
|
|
50
51
|
type InputTypeHeader<R extends RouteConfig> = InputTypeBase<R, 'headers', 'header'>;
|
|
51
52
|
type InputTypeCookie<R extends RouteConfig> = InputTypeBase<R, 'cookies', 'cookie'>;
|
|
52
|
-
type
|
|
53
|
-
|
|
53
|
+
type ExtractContent<T> = T extends {
|
|
54
|
+
[K in keyof T]: infer A;
|
|
55
|
+
} ? A extends Record<'schema', ZodSchema> ? z.infer<A['schema']> : never : never;
|
|
56
|
+
type RouteConfigToTypedResponse<R extends RouteConfig> = {
|
|
57
|
+
[Status in keyof R['responses'] & StatusCode]: IsJson<keyof R['responses'][Status]['content']> extends never ? TypedResponse<{}, Status, string> : TypedResponse<ExtractContent<R['responses'][Status]['content']>, Status, 'json'>;
|
|
58
|
+
}[keyof R['responses'] & StatusCode];
|
|
59
|
+
type Hook<T, E extends Env, P extends string, R> = (result: {
|
|
54
60
|
success: true;
|
|
55
61
|
data: T;
|
|
56
62
|
} | {
|
|
57
63
|
success: false;
|
|
58
64
|
error: ZodError;
|
|
59
|
-
}, c: Context<E, P>) =>
|
|
65
|
+
}, c: Context<E, P>) => R;
|
|
60
66
|
type ConvertPathType<T extends string> = T extends `${infer Start}/{${infer Param}}${infer Rest}` ? `${Start}/:${Param}${ConvertPathType<Rest>}` : T;
|
|
61
|
-
type HandlerTypedResponse<O> = TypedResponse<O> | Promise<TypedResponse<O>>;
|
|
62
|
-
type HandlerAllResponse<O> = Response | Promise<Response> | TypedResponse<O> | Promise<TypedResponse<O>>;
|
|
63
67
|
type OpenAPIHonoOptions<E extends Env> = {
|
|
64
68
|
defaultHook?: Hook<any, E, any, any>;
|
|
65
69
|
};
|
|
66
70
|
type HonoInit<E extends Env> = ConstructorParameters<typeof Hono>[0] & OpenAPIHonoOptions<E>;
|
|
67
71
|
type RouteHandler<R extends RouteConfig, E extends Env = Env, I extends Input = InputTypeParam<R> & InputTypeQuery<R> & InputTypeHeader<R> & InputTypeCookie<R> & InputTypeForm<R> & InputTypeJson<R>, P extends string = ConvertPathType<R['path']>> = Handler<E, P, I, R extends {
|
|
68
72
|
responses: {
|
|
69
|
-
[statusCode:
|
|
73
|
+
[statusCode: number]: {
|
|
70
74
|
content: {
|
|
71
75
|
[mediaType: string]: ZodMediaTypeObject;
|
|
72
76
|
};
|
|
73
77
|
};
|
|
74
78
|
};
|
|
75
|
-
} ?
|
|
76
|
-
type RouteHook<R extends RouteConfig, E extends Env = Env, I extends Input = InputTypeParam<R> & InputTypeQuery<R> & InputTypeHeader<R> & InputTypeCookie<R> & InputTypeForm<R> & InputTypeJson<R>, P extends string = ConvertPathType<R['path']>> = Hook<I, E, P,
|
|
79
|
+
} ? RouteConfigToTypedResponse<R> : RouteConfigToTypedResponse<R> | Response | Promise<Response>>;
|
|
80
|
+
type RouteHook<R extends RouteConfig, E extends Env = Env, I extends Input = InputTypeParam<R> & InputTypeQuery<R> & InputTypeHeader<R> & InputTypeCookie<R> & InputTypeForm<R> & InputTypeJson<R>, P extends string = ConvertPathType<R['path']>> = Hook<I, E, P, RouteConfigToTypedResponse<R> | Response | Promise<Response> | void | Promise<void>>;
|
|
77
81
|
type OpenAPIObjectConfigure<E extends Env, P extends string> = OpenAPIObjectConfig | ((context: Context<E, P>) => OpenAPIObjectConfig);
|
|
78
82
|
declare class OpenAPIHono<E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'> extends Hono<E, S, BasePath> {
|
|
79
83
|
openAPIRegistry: OpenAPIRegistry;
|
|
80
84
|
defaultHook?: OpenAPIHonoOptions<E>['defaultHook'];
|
|
81
85
|
constructor(init?: HonoInit<E>);
|
|
86
|
+
/**
|
|
87
|
+
*
|
|
88
|
+
* @param {RouteConfig} route - The route definition which you create with `createRoute()`.
|
|
89
|
+
* @param {Handler} handler - The handler. If you want to return a JSON object, you should specify the status code with `c.json()`.
|
|
90
|
+
* @param {Hook} hook - Optional. The hook method defines what it should do after validation.
|
|
91
|
+
* @example
|
|
92
|
+
* app.openapi(
|
|
93
|
+
* route,
|
|
94
|
+
* (c) => {
|
|
95
|
+
* // ...
|
|
96
|
+
* return c.json(
|
|
97
|
+
* {
|
|
98
|
+
* age: 20,
|
|
99
|
+
* name: 'Young man',
|
|
100
|
+
* },
|
|
101
|
+
* 200 // You should specify the status code even if it's 200.
|
|
102
|
+
* )
|
|
103
|
+
* },
|
|
104
|
+
* (result, c) => {
|
|
105
|
+
* if (!result.success) {
|
|
106
|
+
* return c.json(
|
|
107
|
+
* {
|
|
108
|
+
* code: 400,
|
|
109
|
+
* message: 'Custom Message',
|
|
110
|
+
* },
|
|
111
|
+
* 400
|
|
112
|
+
* )
|
|
113
|
+
* }
|
|
114
|
+
* }
|
|
115
|
+
*)
|
|
116
|
+
*/
|
|
82
117
|
openapi: <R extends RouteConfig, I extends Input = InputTypeBase<R, "params", "param"> & InputTypeBase<R, "query", "query"> & InputTypeBase<R, "headers", "header"> & InputTypeBase<R, "cookies", "cookie"> & InputTypeForm<R> & InputTypeJson<R>, P extends string = ConvertPathType<R["path"]>>(route: R, handler: Handler<E, P, I, R extends {
|
|
83
118
|
responses: {
|
|
84
|
-
[statusCode:
|
|
119
|
+
[statusCode: number]: {
|
|
85
120
|
content: {
|
|
86
121
|
[mediaType: string]: ZodMediaTypeObject;
|
|
87
122
|
};
|
|
88
123
|
};
|
|
89
124
|
};
|
|
90
|
-
} ?
|
|
125
|
+
} ? RouteConfigToTypedResponse<R> : RouteConfigToTypedResponse<R> | Response | Promise<Response>>, hook?: Hook<I, E, P, RouteConfigToTypedResponse<R> | Response | Promise<Response> | void | Promise<void>> | undefined) => OpenAPIHono<E, S & ToSchema<R['method'], MergePath<BasePath, P>, I, RouteConfigToTypedResponse<R>>, BasePath>;
|
|
91
126
|
getOpenAPIDocument: (config: OpenAPIObjectConfig) => openapi3_ts_oas30.OpenAPIObject;
|
|
92
127
|
getOpenAPI31Document: (config: OpenAPIObjectConfig) => openapi3_ts_oas31.OpenAPIObject;
|
|
93
128
|
doc: <P extends string>(path: P, configure: OpenAPIObjectConfigure<E, P>) => OpenAPIHono<E, S & ToSchema<'get', P, {}, {}>, BasePath>;
|
|
@@ -103,4 +138,4 @@ declare const createRoute: <P extends string, R extends Omit<RouteConfig, "path"
|
|
|
103
138
|
getRoutingPath(): RoutingPath<R['path']>;
|
|
104
139
|
};
|
|
105
140
|
|
|
106
|
-
export { type Hook, OpenAPIHono, type OpenAPIHonoOptions, type OpenAPIObjectConfigure, type RouteHandler, type RouteHook, createRoute };
|
|
141
|
+
export { type Hook, OpenAPIHono, type OpenAPIHonoOptions, type OpenAPIObjectConfigure, type RouteConfigToTypedResponse, type RouteHandler, type RouteHook, createRoute };
|
package/dist/index.js
CHANGED
|
@@ -39,6 +39,37 @@ var OpenAPIHono = class _OpenAPIHono extends import_hono.Hono {
|
|
|
39
39
|
this.openAPIRegistry = new import_zod_to_openapi.OpenAPIRegistry();
|
|
40
40
|
this.defaultHook = init?.defaultHook;
|
|
41
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
*
|
|
44
|
+
* @param {RouteConfig} route - The route definition which you create with `createRoute()`.
|
|
45
|
+
* @param {Handler} handler - The handler. If you want to return a JSON object, you should specify the status code with `c.json()`.
|
|
46
|
+
* @param {Hook} hook - Optional. The hook method defines what it should do after validation.
|
|
47
|
+
* @example
|
|
48
|
+
* app.openapi(
|
|
49
|
+
* route,
|
|
50
|
+
* (c) => {
|
|
51
|
+
* // ...
|
|
52
|
+
* return c.json(
|
|
53
|
+
* {
|
|
54
|
+
* age: 20,
|
|
55
|
+
* name: 'Young man',
|
|
56
|
+
* },
|
|
57
|
+
* 200 // You should specify the status code even if it's 200.
|
|
58
|
+
* )
|
|
59
|
+
* },
|
|
60
|
+
* (result, c) => {
|
|
61
|
+
* if (!result.success) {
|
|
62
|
+
* return c.json(
|
|
63
|
+
* {
|
|
64
|
+
* code: 400,
|
|
65
|
+
* message: 'Custom Message',
|
|
66
|
+
* },
|
|
67
|
+
* 400
|
|
68
|
+
* )
|
|
69
|
+
* }
|
|
70
|
+
* }
|
|
71
|
+
*)
|
|
72
|
+
*/
|
|
42
73
|
openapi = (route, handler, hook = this.defaultHook) => {
|
|
43
74
|
this.openAPIRegistry.registerPath(route);
|
|
44
75
|
const validators = [];
|
|
@@ -79,7 +110,13 @@ var OpenAPIHono = class _OpenAPIHono extends import_hono.Hono {
|
|
|
79
110
|
}
|
|
80
111
|
}
|
|
81
112
|
const middleware = route.middleware ? Array.isArray(route.middleware) ? route.middleware : [route.middleware] : [];
|
|
82
|
-
this.on(
|
|
113
|
+
this.on(
|
|
114
|
+
[route.method],
|
|
115
|
+
route.path.replaceAll(/\/{(.+?)}/g, "/:$1"),
|
|
116
|
+
...middleware,
|
|
117
|
+
...validators,
|
|
118
|
+
handler
|
|
119
|
+
);
|
|
83
120
|
return this;
|
|
84
121
|
};
|
|
85
122
|
getOpenAPIDocument = (config) => {
|
package/dist/index.mjs
CHANGED
|
@@ -17,6 +17,37 @@ var OpenAPIHono = class _OpenAPIHono extends Hono {
|
|
|
17
17
|
this.openAPIRegistry = new OpenAPIRegistry();
|
|
18
18
|
this.defaultHook = init?.defaultHook;
|
|
19
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
*
|
|
22
|
+
* @param {RouteConfig} route - The route definition which you create with `createRoute()`.
|
|
23
|
+
* @param {Handler} handler - The handler. If you want to return a JSON object, you should specify the status code with `c.json()`.
|
|
24
|
+
* @param {Hook} hook - Optional. The hook method defines what it should do after validation.
|
|
25
|
+
* @example
|
|
26
|
+
* app.openapi(
|
|
27
|
+
* route,
|
|
28
|
+
* (c) => {
|
|
29
|
+
* // ...
|
|
30
|
+
* return c.json(
|
|
31
|
+
* {
|
|
32
|
+
* age: 20,
|
|
33
|
+
* name: 'Young man',
|
|
34
|
+
* },
|
|
35
|
+
* 200 // You should specify the status code even if it's 200.
|
|
36
|
+
* )
|
|
37
|
+
* },
|
|
38
|
+
* (result, c) => {
|
|
39
|
+
* if (!result.success) {
|
|
40
|
+
* return c.json(
|
|
41
|
+
* {
|
|
42
|
+
* code: 400,
|
|
43
|
+
* message: 'Custom Message',
|
|
44
|
+
* },
|
|
45
|
+
* 400
|
|
46
|
+
* )
|
|
47
|
+
* }
|
|
48
|
+
* }
|
|
49
|
+
*)
|
|
50
|
+
*/
|
|
20
51
|
openapi = (route, handler, hook = this.defaultHook) => {
|
|
21
52
|
this.openAPIRegistry.registerPath(route);
|
|
22
53
|
const validators = [];
|
|
@@ -57,7 +88,13 @@ var OpenAPIHono = class _OpenAPIHono extends Hono {
|
|
|
57
88
|
}
|
|
58
89
|
}
|
|
59
90
|
const middleware = route.middleware ? Array.isArray(route.middleware) ? route.middleware : [route.middleware] : [];
|
|
60
|
-
this.on(
|
|
91
|
+
this.on(
|
|
92
|
+
[route.method],
|
|
93
|
+
route.path.replaceAll(/\/{(.+?)}/g, "/:$1"),
|
|
94
|
+
...middleware,
|
|
95
|
+
...validators,
|
|
96
|
+
handler
|
|
97
|
+
);
|
|
61
98
|
return this;
|
|
62
99
|
};
|
|
63
100
|
getOpenAPIDocument = (config) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hono/zod-openapi",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "A wrapper class of Hono which supports OpenAPI.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -37,12 +37,12 @@
|
|
|
37
37
|
},
|
|
38
38
|
"homepage": "https://github.com/honojs/middleware",
|
|
39
39
|
"peerDependencies": {
|
|
40
|
-
"hono": ">=3.
|
|
40
|
+
"hono": ">=4.3.6",
|
|
41
41
|
"zod": "3.*"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@cloudflare/workers-types": "^4.20240117.0",
|
|
45
|
-
"hono": "^4.
|
|
45
|
+
"hono": "^4.3.6",
|
|
46
46
|
"jest": "^29.7.0",
|
|
47
47
|
"openapi3-ts": "^4.1.2",
|
|
48
48
|
"tsup": "^8.0.1",
|