@hono/zod-openapi
Advanced tools
Comparing version 0.8.6 to 0.9.0
@@ -5,3 +5,3 @@ import * as openapi3_ts_oas31 from 'openapi3-ts/oas31'; | ||
import { OpenAPIObjectConfig } from '@asteasolutions/zod-to-openapi/dist/v3.0/openapi-generator'; | ||
import { Env, Input, Handler, Schema, Hono, ToSchema, Context, TypedResponse } from 'hono'; | ||
import { Env, Input, Handler, Context, Schema, Hono, ToSchema, TypedResponse } from 'hono'; | ||
import { MergeSchemaPath, MergePath } from 'hono/types'; | ||
@@ -66,2 +66,3 @@ import { RemoveBlankRecord } from 'hono/utils/types'; | ||
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>>; | ||
type OpenAPIObjectConfigure<E extends Env, P extends string> = OpenAPIObjectConfig | ((context: Context<E, P>) => OpenAPIObjectConfig); | ||
declare class OpenAPIHono<E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'> extends Hono<E, S, BasePath> { | ||
@@ -74,4 +75,4 @@ openAPIRegistry: OpenAPIRegistry; | ||
getOpenAPI31Document: (config: OpenAPIObjectConfig) => openapi3_ts_oas31.OpenAPIObject; | ||
doc: <P extends string>(path: P, config: OpenAPIObjectConfig) => OpenAPIHono<E, S & ToSchema<"get", P, {}, {}>, BasePath>; | ||
doc31: <P extends string>(path: P, config: OpenAPIObjectConfig) => OpenAPIHono<E, S & ToSchema<"get", P, {}, {}>, BasePath>; | ||
doc: <P extends string>(path: P, configure: OpenAPIObjectConfigure<E, P>) => OpenAPIHono<E, S & ToSchema<"get", P, {}, {}>, BasePath>; | ||
doc31: <P extends string>(path: P, configure: OpenAPIObjectConfigure<E, P>) => OpenAPIHono<E, S & ToSchema<"get", P, {}, {}>, BasePath>; | ||
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>; | ||
@@ -88,2 +89,2 @@ route<SubPath extends string>(path: SubPath): Hono<E, RemoveBlankRecord<S>, BasePath>; | ||
export { OpenAPIHono, OpenAPIHonoOptions, RouteHandler, RouteHook, createRoute }; | ||
export { OpenAPIHono, OpenAPIHonoOptions, OpenAPIObjectConfigure, RouteHandler, RouteHook, createRoute }; |
@@ -93,4 +93,5 @@ "use strict"; | ||
}; | ||
doc = (path, config) => { | ||
doc = (path, configure) => { | ||
return this.get(path, (c) => { | ||
const config = typeof configure === "function" ? configure(c) : configure; | ||
const document = this.getOpenAPIDocument(config); | ||
@@ -100,4 +101,5 @@ return c.json(document); | ||
}; | ||
doc31 = (path, config) => { | ||
doc31 = (path, configure) => { | ||
return this.get(path, (c) => { | ||
const config = typeof configure === "function" ? configure(c) : configure; | ||
const document = this.getOpenAPI31Document(config); | ||
@@ -104,0 +106,0 @@ return c.json(document); |
{ | ||
"name": "@hono/zod-openapi", | ||
"version": "0.8.6", | ||
"version": "0.9.0", | ||
"description": "A wrapper class of Hono which supports OpenAPI.", | ||
@@ -45,2 +45,3 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@hono/zod-validator": "^0.1.11", | ||
"hono": "^3.9.1", | ||
@@ -47,0 +48,0 @@ "zod": "^3.22.1" |
@@ -234,3 +234,3 @@ # Zod OpenAPI Hono | ||
app.doc31('/docs', {openapi: '3.1.0'}) // new endpoint | ||
app.getOpenAPI31Document(, {openapi: '3.1.0'}) // raw json | ||
app.getOpenAPI31Document({openapi: '3.1.0'}) // raw json | ||
``` | ||
@@ -288,4 +288,70 @@ | ||
## Tips | ||
### How to register components | ||
You can register components to the registry as follows: | ||
```ts | ||
app.openAPIRegistry.registerComponent('schemas', { | ||
User: UserSchema, | ||
}) | ||
``` | ||
About this feature, please refer to [the "Zod to OpenAPI" resource / Defining Custom Components](https://github.com/asteasolutions/zod-to-openapi#defining-custom-components) | ||
### How to setup authorization | ||
You can setup authorization as follows: | ||
eg. Bearer Auth | ||
Register the security scheme: | ||
```ts | ||
app.openAPIRegistry.registerComponent('securitySchema', { | ||
Bearer: { | ||
type: 'http', | ||
scheme: 'bearer', | ||
}, | ||
}) | ||
``` | ||
And setup the security scheme for specific routes: | ||
```ts | ||
const route = createRoute({ | ||
// ... | ||
security: [ | ||
{ | ||
Bearer: [], | ||
}, | ||
], | ||
}) | ||
``` | ||
### How to access context in app.doc | ||
You can access the context in `app.doc` as follows: | ||
```ts | ||
app.doc('/doc', c => ({ | ||
openapi: '3.0.0', | ||
info: { | ||
version: '1.0.0', | ||
title: 'My API', | ||
}, | ||
servers: [ | ||
{ | ||
url: new URL(c.req.url).hostname, | ||
description: 'Current environment', | ||
}, | ||
], | ||
})) | ||
``` | ||
## Limitations | ||
### Combining with `Hono` | ||
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. | ||
@@ -295,2 +361,15 @@ | ||
### Header keys | ||
Header keys that you define in your schema must be in lowercase. | ||
```ts | ||
const HeadersSchema = z.object({ | ||
// Header keys must be in lowercase, `Authorization` is not allowed. | ||
authorization: z.string().openapi({ | ||
example: 'Bearer SECRET', | ||
}), | ||
}) | ||
``` | ||
## References | ||
@@ -297,0 +376,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33418
383
385
3