From 3de3d7cd1bfc064f4f2c38da35f46da4998dac35 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Fri, 18 Aug 2023 23:56:39 +0900 Subject: [PATCH] fix(zod-validator): support `TypedResponse` in hook (#114) * fix(zod-validator): support `TypedResponse` in hook * changeset --- .changeset/modern-dancers-lick.md | 5 +++++ packages/zod-validator/src/index.ts | 20 ++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 .changeset/modern-dancers-lick.md diff --git a/.changeset/modern-dancers-lick.md b/.changeset/modern-dancers-lick.md new file mode 100644 index 00000000..88bf31a0 --- /dev/null +++ b/.changeset/modern-dancers-lick.md @@ -0,0 +1,5 @@ +--- +'@hono/zod-validator': patch +--- + +fix(zod-validator): support TypedResponse in hook diff --git a/packages/zod-validator/src/index.ts b/packages/zod-validator/src/index.ts index 8b1e6227..8e0bbda7 100644 --- a/packages/zod-validator/src/index.ts +++ b/packages/zod-validator/src/index.ts @@ -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 = ( +export type Hook = ( result: { success: true; data: T } | { success: false; error: ZodError; data: T }, c: Context -) => Response | Promise | void | Promise +) => + | Response + | Promise + | void + | Promise + | TypedResponse + | Promise> + | Promise | void> export const zValidator = < T extends ZodSchema, @@ -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 } }