@hono/zod-openapi
Advanced tools
Comparing version 0.6.0 to 0.7.0
@@ -59,3 +59,6 @@ import * as openapi3_ts_oas31 from 'openapi3-ts/oas31'; | ||
type HandlerResponse<O> = TypedResponse<O> | Promise<TypedResponse<O>>; | ||
type HonoInit = ConstructorParameters<typeof Hono>[0]; | ||
type OpenAPIHonoOptions<E extends Env> = { | ||
defaultHook?: Hook<any, E, any, any>; | ||
}; | ||
type HonoInit<E extends Env> = ConstructorParameters<typeof Hono>[0] & OpenAPIHonoOptions<E>; | ||
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>>>; | ||
@@ -65,4 +68,5 @@ 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>>; | ||
openAPIRegistry: OpenAPIRegistry; | ||
constructor(init?: HonoInit); | ||
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: RouteHandler<R, E, I, P>, hook?: RouteHook<R, E, I, P> | undefined) => OpenAPIHono<E, ToSchema<R["method"], P, I["in"], OutputType<R>>, BasePath>; | ||
defaultHook?: OpenAPIHonoOptions<E>['defaultHook']; | ||
constructor(init?: HonoInit<E>); | ||
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>; | ||
getOpenAPIDocument: (config: OpenAPIObjectConfig) => openapi3_ts_oas30.OpenAPIObject; | ||
@@ -74,2 +78,3 @@ getOpenAPI31Document: (config: OpenAPIObjectConfig) => openapi3_ts_oas31.OpenAPIObject; | ||
route<SubPath extends string>(path: SubPath): Hono<E, RemoveBlankRecord<S>, BasePath>; | ||
basePath<SubPath extends string>(path: SubPath): OpenAPIHono<E, S, MergePath<BasePath, SubPath>>; | ||
} | ||
@@ -83,2 +88,2 @@ type RoutingPath<P extends string> = P extends `${infer Head}/{${infer Param}}${infer Tail}` ? `${Head}/:${Param}${RoutingPath<Tail>}` : P; | ||
export { OpenAPIHono, RouteHandler, RouteHook, createRoute }; | ||
export { OpenAPIHono, OpenAPIHonoOptions, RouteHandler, RouteHook, createRoute }; |
@@ -13,7 +13,9 @@ // src/index.ts | ||
openAPIRegistry; | ||
defaultHook; | ||
constructor(init) { | ||
super(init); | ||
this.openAPIRegistry = new OpenAPIRegistry(); | ||
this.defaultHook = init?.defaultHook; | ||
} | ||
openapi = (route, handler, hook) => { | ||
openapi = (route, handler, hook = this.defaultHook) => { | ||
this.openAPIRegistry.registerPath(route); | ||
@@ -115,2 +117,5 @@ const validators = []; | ||
} | ||
basePath(path) { | ||
return new _OpenAPIHono(super.basePath(path)); | ||
} | ||
}; | ||
@@ -117,0 +122,0 @@ var createRoute = (routeConfig) => { |
{ | ||
"name": "@hono/zod-openapi", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "A wrapper class of Hono which supports OpenAPI.", | ||
@@ -34,3 +34,4 @@ "type": "module", | ||
"devDependencies": { | ||
"hono": "^3.6.3", | ||
"@hono/zod-validator": "^0.1.9", | ||
"hono": "^3.7.2", | ||
"zod": "^3.22.1" | ||
@@ -40,3 +41,3 @@ }, | ||
"@asteasolutions/zod-to-openapi": "^5.5.0", | ||
"@hono/zod-validator": "^0.1.8" | ||
"@hono/zod-validator": "^0.1.9" | ||
}, | ||
@@ -46,2 +47,2 @@ "engines": { | ||
} | ||
} | ||
} |
@@ -177,2 +177,53 @@ # Zod OpenAPI Hono | ||
### A DRY approach to handling validation errors | ||
In the case that you have a common error formatter, you can initialize the `OpenAPIHono` instance with a `defaultHook`. | ||
```ts | ||
const app = new OpenAPIHono({ | ||
defaultHook: (result, c) => { | ||
if (!result.success) { | ||
return c.jsonT( | ||
{ | ||
ok: false, | ||
errors: formatZodErrors(result), | ||
source: 'custom_error_handler', | ||
}, | ||
422 | ||
) | ||
} | ||
}, | ||
}) | ||
``` | ||
You can still override the `defaultHook` by providing the hook at the call site when appropriate. | ||
```ts | ||
// uses the defaultHook | ||
app.openapi(createPostRoute, (c) => { | ||
const { title } = c.req.valid('json') | ||
return c.jsonT({ title }) | ||
}) | ||
// override the defaultHook by passing in a hook | ||
app.openapi( | ||
createBookRoute, | ||
(c) => { | ||
const { title } = c.req.valid('json') | ||
return c.jsonT({ title }) | ||
}, | ||
(result, c) => { | ||
if (!result.success) { | ||
return c.jsonT( | ||
{ | ||
ok: false, | ||
source: 'routeHook' as const, | ||
}, | ||
400 | ||
) | ||
} | ||
} | ||
) | ||
``` | ||
### OpenAPI v3.1 | ||
@@ -215,3 +266,3 @@ | ||
app.use(route.getRoutingPath(), prettyJSON(), cache({ cacheName: "my-cache" })) | ||
app.use(route.getRoutingPath(), prettyJSON(), cache({ cacheName: 'my-cache' })) | ||
app.openapi(route, handler) | ||
@@ -218,0 +269,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
30493
376
306
3
Updated@hono/zod-validator@^0.1.9