@hono/zod-openapi 0.8.6 → 0.9.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 +80 -1
- package/dist/index.d.mts +5 -4
- package/dist/index.d.ts +5 -4
- package/dist/index.js +4 -2
- package/dist/index.mjs +4 -2
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -231,7 +231,7 @@ You can generate OpenAPI v3.1 spec using the following methods:
|
|
|
231
231
|
|
|
232
232
|
```ts
|
|
233
233
|
app.doc31('/docs', {openapi: '3.1.0'}) // new endpoint
|
|
234
|
-
app.getOpenAPI31Document(
|
|
234
|
+
app.getOpenAPI31Document({openapi: '3.1.0'}) // raw json
|
|
235
235
|
```
|
|
236
236
|
|
|
237
237
|
### The Registry
|
|
@@ -284,12 +284,91 @@ const appRoutes = app.openapi(route, (c) => {
|
|
|
284
284
|
const client = hc<typeof appRoutes>('http://localhost:8787/')
|
|
285
285
|
```
|
|
286
286
|
|
|
287
|
+
## Tips
|
|
288
|
+
|
|
289
|
+
### How to register components
|
|
290
|
+
|
|
291
|
+
You can register components to the registry as follows:
|
|
292
|
+
|
|
293
|
+
```ts
|
|
294
|
+
app.openAPIRegistry.registerComponent('schemas', {
|
|
295
|
+
User: UserSchema,
|
|
296
|
+
})
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
About this feature, please refer to [the "Zod to OpenAPI" resource / Defining Custom Components](https://github.com/asteasolutions/zod-to-openapi#defining-custom-components)
|
|
300
|
+
|
|
301
|
+
### How to setup authorization
|
|
302
|
+
|
|
303
|
+
You can setup authorization as follows:
|
|
304
|
+
|
|
305
|
+
eg. Bearer Auth
|
|
306
|
+
|
|
307
|
+
Register the security scheme:
|
|
308
|
+
|
|
309
|
+
```ts
|
|
310
|
+
app.openAPIRegistry.registerComponent('securitySchema', {
|
|
311
|
+
Bearer: {
|
|
312
|
+
type: 'http',
|
|
313
|
+
scheme: 'bearer',
|
|
314
|
+
},
|
|
315
|
+
})
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
And setup the security scheme for specific routes:
|
|
319
|
+
|
|
320
|
+
```ts
|
|
321
|
+
const route = createRoute({
|
|
322
|
+
// ...
|
|
323
|
+
security: [
|
|
324
|
+
{
|
|
325
|
+
Bearer: [],
|
|
326
|
+
},
|
|
327
|
+
],
|
|
328
|
+
})
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
### How to access context in app.doc
|
|
332
|
+
|
|
333
|
+
You can access the context in `app.doc` as follows:
|
|
334
|
+
|
|
335
|
+
```ts
|
|
336
|
+
app.doc('/doc', c => ({
|
|
337
|
+
openapi: '3.0.0',
|
|
338
|
+
info: {
|
|
339
|
+
version: '1.0.0',
|
|
340
|
+
title: 'My API',
|
|
341
|
+
},
|
|
342
|
+
servers: [
|
|
343
|
+
{
|
|
344
|
+
url: new URL(c.req.url).hostname,
|
|
345
|
+
description: 'Current environment',
|
|
346
|
+
},
|
|
347
|
+
],
|
|
348
|
+
}))
|
|
349
|
+
```
|
|
350
|
+
|
|
287
351
|
## Limitations
|
|
288
352
|
|
|
353
|
+
### Combining with `Hono`
|
|
354
|
+
|
|
289
355
|
Be careful when combining `OpenAPIHono` instances with plain `Hono` instances. `OpenAPIHono` will merge the definitions of direct subapps, but plain `Hono` knows nothing about the OpenAPI spec additions. Similarly `OpenAPIHono` will not "dig" for instances deep inside a branch of plain `Hono` instances.
|
|
290
356
|
|
|
291
357
|
If you're migrating from plain `Hono` to `OpenAPIHono`, we recommend porting your top-level app, then working your way down the router tree.
|
|
292
358
|
|
|
359
|
+
### Header keys
|
|
360
|
+
|
|
361
|
+
Header keys that you define in your schema must be in lowercase.
|
|
362
|
+
|
|
363
|
+
```ts
|
|
364
|
+
const HeadersSchema = z.object({
|
|
365
|
+
// Header keys must be in lowercase, `Authorization` is not allowed.
|
|
366
|
+
authorization: z.string().openapi({
|
|
367
|
+
example: 'Bearer SECRET',
|
|
368
|
+
}),
|
|
369
|
+
})
|
|
370
|
+
```
|
|
371
|
+
|
|
293
372
|
## References
|
|
294
373
|
|
|
295
374
|
- [Hono](https://hono.dev/)
|
package/dist/index.d.mts
CHANGED
|
@@ -2,7 +2,7 @@ import * as openapi3_ts_oas31 from 'openapi3-ts/oas31';
|
|
|
2
2
|
import * as openapi3_ts_oas30 from 'openapi3-ts/oas30';
|
|
3
3
|
import { RouteConfig, OpenAPIRegistry, ZodRequestBody, ZodContentObject, ResponseConfig } from '@asteasolutions/zod-to-openapi';
|
|
4
4
|
import { OpenAPIObjectConfig } from '@asteasolutions/zod-to-openapi/dist/v3.0/openapi-generator';
|
|
5
|
-
import { Env, Input, Handler, Schema, Hono, ToSchema,
|
|
5
|
+
import { Env, Input, Handler, Context, Schema, Hono, ToSchema, TypedResponse } from 'hono';
|
|
6
6
|
import { MergeSchemaPath, MergePath } from 'hono/types';
|
|
7
7
|
import { RemoveBlankRecord } from 'hono/utils/types';
|
|
8
8
|
import { AnyZodObject, z, ZodSchema, ZodError, ZodType } from 'zod';
|
|
@@ -62,6 +62,7 @@ type OpenAPIHonoOptions<E extends Env> = {
|
|
|
62
62
|
type HonoInit<E extends Env> = ConstructorParameters<typeof Hono>[0] & OpenAPIHonoOptions<E>;
|
|
63
63
|
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, HandlerResponse<OutputType<R>>>;
|
|
64
64
|
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, OutputType<R>>;
|
|
65
|
+
type OpenAPIObjectConfigure<E extends Env, P extends string> = OpenAPIObjectConfig | ((context: Context<E, P>) => OpenAPIObjectConfig);
|
|
65
66
|
declare class OpenAPIHono<E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'> extends Hono<E, S, BasePath> {
|
|
66
67
|
openAPIRegistry: OpenAPIRegistry;
|
|
67
68
|
defaultHook?: OpenAPIHonoOptions<E>['defaultHook'];
|
|
@@ -69,8 +70,8 @@ declare class OpenAPIHono<E extends Env = Env, S extends Schema = {}, BasePath e
|
|
|
69
70
|
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, HandlerResponse<OutputType<R>>>, hook?: Hook<I, E, P, OutputType<R>> | undefined) => OpenAPIHono<E, S & ToSchema<R["method"], P, I["in"], OutputType<R>>, BasePath>;
|
|
70
71
|
getOpenAPIDocument: (config: OpenAPIObjectConfig) => openapi3_ts_oas30.OpenAPIObject;
|
|
71
72
|
getOpenAPI31Document: (config: OpenAPIObjectConfig) => openapi3_ts_oas31.OpenAPIObject;
|
|
72
|
-
doc: <P extends string>(path: P,
|
|
73
|
-
doc31: <P extends string>(path: P,
|
|
73
|
+
doc: <P extends string>(path: P, configure: OpenAPIObjectConfigure<E, P>) => OpenAPIHono<E, S & ToSchema<"get", P, {}, {}>, BasePath>;
|
|
74
|
+
doc31: <P extends string>(path: P, configure: OpenAPIObjectConfigure<E, P>) => OpenAPIHono<E, S & ToSchema<"get", P, {}, {}>, BasePath>;
|
|
74
75
|
route<SubPath extends string, SubEnv extends Env, SubSchema extends Schema, SubBasePath extends string>(path: SubPath, app: Hono<SubEnv, SubSchema, SubBasePath>): OpenAPIHono<E, MergeSchemaPath<SubSchema, MergePath<BasePath, SubPath>> & S, BasePath>;
|
|
75
76
|
route<SubPath extends string>(path: SubPath): Hono<E, RemoveBlankRecord<S>, BasePath>;
|
|
76
77
|
basePath<SubPath extends string>(path: SubPath): OpenAPIHono<E, S, MergePath<BasePath, SubPath>>;
|
|
@@ -82,4 +83,4 @@ declare const createRoute: <P extends string, R extends Omit<RouteConfig, "path"
|
|
|
82
83
|
getRoutingPath(): RoutingPath<R["path"]>;
|
|
83
84
|
};
|
|
84
85
|
|
|
85
|
-
export { OpenAPIHono, OpenAPIHonoOptions, RouteHandler, RouteHook, createRoute };
|
|
86
|
+
export { OpenAPIHono, OpenAPIHonoOptions, OpenAPIObjectConfigure, RouteHandler, RouteHook, createRoute };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import * as openapi3_ts_oas31 from 'openapi3-ts/oas31';
|
|
|
2
2
|
import * as openapi3_ts_oas30 from 'openapi3-ts/oas30';
|
|
3
3
|
import { RouteConfig, OpenAPIRegistry, ZodRequestBody, ZodContentObject, ResponseConfig } from '@asteasolutions/zod-to-openapi';
|
|
4
4
|
import { OpenAPIObjectConfig } from '@asteasolutions/zod-to-openapi/dist/v3.0/openapi-generator';
|
|
5
|
-
import { Env, Input, Handler, Schema, Hono, ToSchema,
|
|
5
|
+
import { Env, Input, Handler, Context, Schema, Hono, ToSchema, TypedResponse } from 'hono';
|
|
6
6
|
import { MergeSchemaPath, MergePath } from 'hono/types';
|
|
7
7
|
import { RemoveBlankRecord } from 'hono/utils/types';
|
|
8
8
|
import { AnyZodObject, z, ZodSchema, ZodError, ZodType } from 'zod';
|
|
@@ -62,6 +62,7 @@ type OpenAPIHonoOptions<E extends Env> = {
|
|
|
62
62
|
type HonoInit<E extends Env> = ConstructorParameters<typeof Hono>[0] & OpenAPIHonoOptions<E>;
|
|
63
63
|
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, HandlerResponse<OutputType<R>>>;
|
|
64
64
|
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, OutputType<R>>;
|
|
65
|
+
type OpenAPIObjectConfigure<E extends Env, P extends string> = OpenAPIObjectConfig | ((context: Context<E, P>) => OpenAPIObjectConfig);
|
|
65
66
|
declare class OpenAPIHono<E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'> extends Hono<E, S, BasePath> {
|
|
66
67
|
openAPIRegistry: OpenAPIRegistry;
|
|
67
68
|
defaultHook?: OpenAPIHonoOptions<E>['defaultHook'];
|
|
@@ -69,8 +70,8 @@ declare class OpenAPIHono<E extends Env = Env, S extends Schema = {}, BasePath e
|
|
|
69
70
|
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, HandlerResponse<OutputType<R>>>, hook?: Hook<I, E, P, OutputType<R>> | undefined) => OpenAPIHono<E, S & ToSchema<R["method"], P, I["in"], OutputType<R>>, BasePath>;
|
|
70
71
|
getOpenAPIDocument: (config: OpenAPIObjectConfig) => openapi3_ts_oas30.OpenAPIObject;
|
|
71
72
|
getOpenAPI31Document: (config: OpenAPIObjectConfig) => openapi3_ts_oas31.OpenAPIObject;
|
|
72
|
-
doc: <P extends string>(path: P,
|
|
73
|
-
doc31: <P extends string>(path: P,
|
|
73
|
+
doc: <P extends string>(path: P, configure: OpenAPIObjectConfigure<E, P>) => OpenAPIHono<E, S & ToSchema<"get", P, {}, {}>, BasePath>;
|
|
74
|
+
doc31: <P extends string>(path: P, configure: OpenAPIObjectConfigure<E, P>) => OpenAPIHono<E, S & ToSchema<"get", P, {}, {}>, BasePath>;
|
|
74
75
|
route<SubPath extends string, SubEnv extends Env, SubSchema extends Schema, SubBasePath extends string>(path: SubPath, app: Hono<SubEnv, SubSchema, SubBasePath>): OpenAPIHono<E, MergeSchemaPath<SubSchema, MergePath<BasePath, SubPath>> & S, BasePath>;
|
|
75
76
|
route<SubPath extends string>(path: SubPath): Hono<E, RemoveBlankRecord<S>, BasePath>;
|
|
76
77
|
basePath<SubPath extends string>(path: SubPath): OpenAPIHono<E, S, MergePath<BasePath, SubPath>>;
|
|
@@ -82,4 +83,4 @@ declare const createRoute: <P extends string, R extends Omit<RouteConfig, "path"
|
|
|
82
83
|
getRoutingPath(): RoutingPath<R["path"]>;
|
|
83
84
|
};
|
|
84
85
|
|
|
85
|
-
export { OpenAPIHono, OpenAPIHonoOptions, RouteHandler, RouteHook, createRoute };
|
|
86
|
+
export { OpenAPIHono, OpenAPIHonoOptions, OpenAPIObjectConfigure, RouteHandler, RouteHook, createRoute };
|
package/dist/index.js
CHANGED
|
@@ -90,14 +90,16 @@ var OpenAPIHono = class _OpenAPIHono extends import_hono.Hono {
|
|
|
90
90
|
const document = generator.generateDocument(config);
|
|
91
91
|
return document;
|
|
92
92
|
};
|
|
93
|
-
doc = (path,
|
|
93
|
+
doc = (path, configure) => {
|
|
94
94
|
return this.get(path, (c) => {
|
|
95
|
+
const config = typeof configure === "function" ? configure(c) : configure;
|
|
95
96
|
const document = this.getOpenAPIDocument(config);
|
|
96
97
|
return c.json(document);
|
|
97
98
|
});
|
|
98
99
|
};
|
|
99
|
-
doc31 = (path,
|
|
100
|
+
doc31 = (path, configure) => {
|
|
100
101
|
return this.get(path, (c) => {
|
|
102
|
+
const config = typeof configure === "function" ? configure(c) : configure;
|
|
101
103
|
const document = this.getOpenAPI31Document(config);
|
|
102
104
|
return c.json(document);
|
|
103
105
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -68,14 +68,16 @@ var OpenAPIHono = class _OpenAPIHono extends Hono {
|
|
|
68
68
|
const document = generator.generateDocument(config);
|
|
69
69
|
return document;
|
|
70
70
|
};
|
|
71
|
-
doc = (path,
|
|
71
|
+
doc = (path, configure) => {
|
|
72
72
|
return this.get(path, (c) => {
|
|
73
|
+
const config = typeof configure === "function" ? configure(c) : configure;
|
|
73
74
|
const document = this.getOpenAPIDocument(config);
|
|
74
75
|
return c.json(document);
|
|
75
76
|
});
|
|
76
77
|
};
|
|
77
|
-
doc31 = (path,
|
|
78
|
+
doc31 = (path, configure) => {
|
|
78
79
|
return this.get(path, (c) => {
|
|
80
|
+
const config = typeof configure === "function" ? configure(c) : configure;
|
|
79
81
|
const document = this.getOpenAPI31Document(config);
|
|
80
82
|
return c.json(document);
|
|
81
83
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hono/zod-openapi",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "A wrapper class of Hono which supports OpenAPI.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
"zod": "3.*"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
+
"@hono/zod-validator": "^0.1.11",
|
|
45
46
|
"hono": "^3.9.1",
|
|
46
47
|
"zod": "^3.22.1"
|
|
47
48
|
},
|