feat: Zod-Openapi-Middleware: Export types to allow handlers and hooks to be declared in more places. (#167)

* Exported Handler and Hook types

* Performed changeset
pull/169/head
Jakob Bergström (NotoriousSledge) 2023-09-21 22:59:22 +02:00 committed by GitHub
parent a8747ab6fe
commit dbebf747c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 2 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/zod-openapi': minor
---
Export types that allow for separate declaratins of route handlers and hooks

View File

@ -153,6 +153,30 @@ type HandlerResponse<O> = TypedResponse<O> | Promise<TypedResponse<O>>
type HonoInit = ConstructorParameters<typeof Hono>[0]
export 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>>>
export 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>>
export class OpenAPIHono<
E extends Env = Env,
S extends Schema = {},
@ -176,8 +200,8 @@ export class OpenAPIHono<
P extends string = ConvertPathType<R['path']>
>(
route: R,
handler: Handler<E, P, I, HandlerResponse<OutputType<R>>>,
hook?: Hook<I, E, P, OutputType<R>>
handler: RouteHandler<R, E, I, P>,
hook?: RouteHook<R, E, I, P>
): OpenAPIHono<E, ToSchema<R['method'], P, I['in'], OutputType<R>>, BasePath> => {
this.openAPIRegistry.registerPath(route)