@hono/zod-openapi
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -50,6 +50,7 @@ import * as openapi3_ts_oas30 from 'openapi3-ts/oas30'; | ||
}, c: Context<E, P>) => TypedResponse<O> | Promise<TypedResponse<T>> | void; | ||
type ConvertPathType<T extends string> = T extends `${infer _}/{${infer Param}}${infer _}` ? `/:${Param}` : T; | ||
declare class OpenAPIHono<E extends Env = Env, S = {}, BasePath extends string = '/'> extends Hono<E, S, BasePath> { | ||
#private; | ||
constructor(); | ||
openapi: <R extends RouteConfig, I extends Input = InputTypeBase<R, "params", "param"> & InputTypeBase<R, "query", "query"> & InputTypeForm<R> & InputTypeJson<R>>(route: R, handler: Handler<E, R["path"], I, OutputType<R>>, hook?: Hook<I, E, R["path"], OutputType<R>> | undefined) => Hono<E, Schema<R["method"], R["path"], I["in"], OutputType<R>>, BasePath>; | ||
openapi: <R extends RouteConfig, I extends Input = InputTypeBase<R, "params", "param"> & InputTypeBase<R, "query", "query"> & InputTypeForm<R> & InputTypeJson<R>, P extends string = ConvertPathType<R["path"]>>(route: R, handler: Handler<E, P, I, OutputType<R>>, hook?: Hook<I, E, P, OutputType<R>> | undefined) => Hono<E, Schema<R["method"], P, I["in"], OutputType<R>>, BasePath>; | ||
getOpenAPIDocument: (config: OpenAPIObjectConfig) => openapi3_ts_oas30.OpenAPIObject; | ||
@@ -56,0 +57,0 @@ doc: (path: string, config: OpenAPIObjectConfig) => void; |
@@ -61,3 +61,3 @@ var __accessCheck = (obj, member, msg) => { | ||
} | ||
this.on([route.method], route.path, ...validators, handler); | ||
this.on([route.method], route.path.replace(/\/{(.+)}/, "/:$1"), ...validators, handler); | ||
return this; | ||
@@ -64,0 +64,0 @@ }; |
{ | ||
"name": "@hono/zod-openapi", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "A wrapper class of Hono which supports OpenAPI.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
# Zod OpenAPI Hono | ||
**Zod OpenAPI Hono** is extending Hono to support OpenAPI. | ||
With it, you can validate values and types using [**Zod**](https://zod.dev/) and generate OpenAPI Swagger documentation. | ||
This is based on [**Zod to OpenAPI**](https://github.com/asteasolutions/zod-to-openapi). | ||
For details on creating schemas and defining routes, please refer to this resource. | ||
**Zod OpenAPI Hono** is an extended Hono class that supports OpenAPI. With it, you can validate values and types using [**Zod**](https://zod.dev/) and generate OpenAPI Swagger documentation. This is based on [**Zod to OpenAPI**](https://github.com/asteasolutions/zod-to-openapi) (thanks a lot!). For details on creating schemas and defining routes, please refer to [the "Zod to OpenAPI" resource](https://github.com/asteasolutions/zod-to-openapi). | ||
_This is not a real middleware but hosted on this monorepo._ | ||
_Note: This is not standalone middleware but is hosted on the monorepo "[github.com/honojs/middleware](https://github.com/honojs/middleware)"._ | ||
## Limitations | ||
- Currently, it does not support validation of _headers_ and _cookies_. | ||
- An instance of Zod OpenAPI Hono cannot be used as a "subApp" in conjunction with `rootApp.route('/api', subApp)`. | ||
## Usage | ||
@@ -14,3 +16,3 @@ | ||
You can install it via the npm. Should be installed with `hono` and `zod`. | ||
You can install it via npm. It should be installed alongside `hono` and `zod`. | ||
@@ -23,5 +25,5 @@ ```sh | ||
#### Write your application | ||
#### Setting up your application | ||
First, define schemas with Zod: | ||
First, define your schemas with Zod. The `z` object should be imported from `@hono/zod-openapi`: | ||
@@ -47,3 +49,3 @@ ```ts | ||
id: z.string().openapi({ | ||
example: 123, | ||
example: '123', | ||
}), | ||
@@ -60,3 +62,3 @@ name: z.string().openapi({ | ||
Next, create routes: | ||
Next, create a route: | ||
@@ -79,3 +81,3 @@ ```ts | ||
}, | ||
description: 'Get the user', | ||
description: 'Retrieve the user', | ||
}, | ||
@@ -86,3 +88,3 @@ }, | ||
Finally, create the App: | ||
Finally, set up the app: | ||
@@ -103,3 +105,3 @@ ```ts | ||
// OpenAPI document will be served on /doc | ||
// The OpenAPI documentation will be available at /doc | ||
app.doc('/doc', { | ||
@@ -114,8 +116,14 @@ openapi: '3.0.0', | ||
### Handling validation errors | ||
You can start your app just like you would with Hono. For Cloudflare Workers and Bun, use this entry point: | ||
You can handle the validation errors the following ways. | ||
```ts | ||
export default app | ||
``` | ||
Define the schema: | ||
### Handling Validation Errors | ||
Validation errors can be handled as follows: | ||
First, define the error schema: | ||
```ts | ||
@@ -132,3 +140,3 @@ const ErrorSchema = z.object({ | ||
Add the response: | ||
Then, add the error response: | ||
@@ -149,3 +157,3 @@ ```ts | ||
}, | ||
description: 'Return Error!', | ||
description: 'Returns an error', | ||
}, | ||
@@ -156,3 +164,3 @@ }, | ||
Add the hook: | ||
Finally, add the hook: | ||
@@ -176,3 +184,3 @@ ```ts | ||
code: 400, | ||
message: 'Validation Error!', | ||
message: 'Validation Error', | ||
}, | ||
@@ -188,3 +196,3 @@ 400 | ||
You can use Hono's middleware as same as using Hono because Zod OpenAPI is just extending Hono. | ||
Zod OpenAPI Hono is an extension of Hono, so you can use Hono's middleware in the same way: | ||
@@ -199,5 +207,5 @@ ```ts | ||
### RPC-mode | ||
### RPC Mode | ||
Zod OpenAPI Hono supports Hono's RPC-mode. You can create the types for passing Hono Client: | ||
Zod OpenAPI Hono supports Hono's RPC mode. You can define types for the Hono Client as follows: | ||
@@ -204,0 +212,0 @@ ```ts |
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
20992
258
225