@@ -3,8 +3,12 @@ import { is, Table } from 'drizzle-orm'
33import type { IsNever , Simplify , ValueOf } from 'type-fest'
44import type { ZodError , ZodObject , ZodOptional , ZodType } from 'zod'
55
6- import type { MaybePromise } from './collection'
7- import type { ApiHttpStatus , ApiRouteHandlerPayloadWithContext , ApiRouteSchema } from './endpoint'
6+ import type {
7+ ApiHttpStatus ,
8+ ApiRouteHandler ,
9+ ApiRouteHandlerPayloadWithContext ,
10+ ApiRouteSchema ,
11+ } from './endpoint'
812import type { Field , FieldRelation , Fields , FieldsInitial , FieldsWithFieldName } from './field'
913
1014export function isRelationField ( field : Field ) : field is FieldRelation {
@@ -184,11 +188,11 @@ export function withValidator<
184188 TContext extends Record < string , unknown > ,
185189> (
186190 schema : TApiRouteSchema ,
187- handler : (
191+ handler : ApiRouteHandler < TContext , TApiRouteSchema >
192+ ) : ApiRouteHandler < TContext , TApiRouteSchema > {
193+ const wrappedHandler = async (
188194 payload : ApiRouteHandlerPayloadWithContext < TApiRouteSchema , TContext >
189- ) => MaybePromise < any >
190- ) : ( payload : ApiRouteHandlerPayloadWithContext < TApiRouteSchema , TContext > ) => MaybePromise < any > {
191- return async ( payload : ApiRouteHandlerPayloadWithContext < TApiRouteSchema , TContext > ) => {
195+ ) => {
192196 const zodErrors = await validateRequestBody ( schema , payload )
193197 if ( zodErrors ) {
194198 return {
@@ -202,7 +206,11 @@ export function withValidator<
202206
203207 const response = await handler ( payload )
204208
205- const validationError = validateResponseBody ( schema , response . status , response . body )
209+ const validationError = validateResponseBody (
210+ schema ,
211+ response . status as ApiHttpStatus ,
212+ response . body
213+ )
206214
207215 if ( validationError ) {
208216 return {
@@ -216,6 +224,8 @@ export function withValidator<
216224
217225 return response
218226 }
227+
228+ return wrappedHandler as ApiRouteHandler < TContext , TApiRouteSchema >
219229}
220230
221231export type JoinArrays < T extends any [ ] > = Simplify <
0 commit comments