Skip to content

Commit

Permalink
fix(zod-validator): support TypedResponse in hook (#114)
Browse files Browse the repository at this point in the history
* fix(zod-validator): support `TypedResponse` in hook

* changeset
  • Loading branch information
yusukebe authored Aug 18, 2023
1 parent 5eb29ae commit 3de3d7c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-dancers-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/zod-validator': patch
---

fix(zod-validator): support TypedResponse in hook
20 changes: 16 additions & 4 deletions packages/zod-validator/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import type { Context, MiddlewareHandler, Env, ValidationTargets } from 'hono'
import type { Context, MiddlewareHandler, Env, ValidationTargets, TypedResponse } from 'hono'
import { validator } from 'hono/validator'
import type { z, ZodSchema, ZodError } from 'zod'

type Hook<T, E extends Env, P extends string> = (
export type Hook<T, E extends Env, P extends string, O = {}> = (
result: { success: true; data: T } | { success: false; error: ZodError; data: T },
c: Context<E, P>
) => Response | Promise<Response> | void | Promise<Response | void>
) =>
| Response
| Promise<Response>
| void
| Promise<Response | void>
| TypedResponse<O>
| Promise<TypedResponse<O>>
| Promise<TypedResponse<O> | void>

export const zValidator = <
T extends ZodSchema,
Expand All @@ -29,7 +36,12 @@ export const zValidator = <

if (hook) {
const hookResult = hook({ data: value, ...result }, c)
if (hookResult instanceof Response || hookResult instanceof Promise) {
if (
hookResult &&
(hookResult instanceof Response ||
hookResult instanceof Promise ||
'response' in hookResult)
) {
return hookResult
}
}
Expand Down

0 comments on commit 3de3d7c

Please sign in to comment.