1- import { z } from "zod" ;
2- import { fromZodError } from "zod-validation-error" ;
1+ import { z as z3 } from "zod/v3" ;
2+ import { z } from "zod/v4" ;
3+ import { fromZodError , ValidationError } from "zod-validation-error/v4" ;
34import type { RetryOptions } from "../schemas/index.js" ;
45import { calculateNextRetryDelay } from "../utils/retries.js" ;
56import { ApiConnectionError , ApiError , ApiSchemaValidationError } from "./errors.js" ;
@@ -12,6 +13,10 @@ import { SemanticInternalAttributes } from "../semanticInternalAttributes.js";
1213import type { TriggerTracer } from "../tracer.js" ;
1314import { randomUUID } from "../utils/crypto.js" ;
1415import { accessoryAttributes } from "../utils/styleAttributes.js" ;
16+ import type {
17+ AnyZodSchema ,
18+ inferZodSchemaOutput ,
19+ } from "../types/schemas.js" ;
1520import type {
1621 CursorPageParams ,
1722 CursorPageResponse ,
@@ -71,16 +76,16 @@ interface FetchOffsetLimitPageParams extends OffsetLimitPageParams {
7176 query ?: URLSearchParams ;
7277}
7378
74- export function zodfetch < TResponseBodySchema extends z . ZodTypeAny > (
79+ export function zodfetch < TResponseBodySchema extends AnyZodSchema > (
7580 schema : TResponseBodySchema ,
7681 url : string ,
7782 requestInit ?: RequestInit ,
78- options ?: ZodFetchOptions < z . output < TResponseBodySchema > >
79- ) : ApiPromise < z . output < TResponseBodySchema > > {
83+ options ?: ZodFetchOptions < inferZodSchemaOutput < TResponseBodySchema > >
84+ ) : ApiPromise < inferZodSchemaOutput < TResponseBodySchema > > {
8085 return new ApiPromise ( _doZodFetch ( schema , url , requestInit , options ) ) ;
8186}
8287
83- export function zodfetchCursorPage < TItemSchema extends z . ZodTypeAny > (
88+ export function zodfetchCursorPage < TItemSchema extends AnyZodSchema > (
8489 schema : TItemSchema ,
8590 url : string ,
8691 params : FetchCursorPageParams ,
@@ -101,13 +106,21 @@ export function zodfetchCursorPage<TItemSchema extends z.ZodTypeAny>(
101106 query . set ( "page[before]" , params . before ) ;
102107 }
103108
104- const cursorPageSchema = z . object ( {
105- data : z . array ( schema ) ,
106- pagination : z . object ( {
107- next : z . string ( ) . optional ( ) ,
108- previous : z . string ( ) . optional ( ) ,
109- } ) ,
110- } ) ;
109+ const cursorPageSchema = "_zod" in schema
110+ ? z . object ( {
111+ data : z . array ( schema as any ) ,
112+ pagination : z . object ( {
113+ next : z . string ( ) . optional ( ) ,
114+ previous : z . string ( ) . optional ( ) ,
115+ } ) ,
116+ } )
117+ : z3 . object ( {
118+ data : z3 . array ( schema as any ) ,
119+ pagination : z3 . object ( {
120+ next : z3 . string ( ) . optional ( ) ,
121+ previous : z3 . string ( ) . optional ( ) ,
122+ } ) ,
123+ } ) ;
111124
112125 const $url = new URL ( url ) ;
113126 $url . search = query . toString ( ) ;
@@ -117,7 +130,7 @@ export function zodfetchCursorPage<TItemSchema extends z.ZodTypeAny>(
117130 return new CursorPagePromise ( fetchResult , schema , url , params , requestInit , options ) ;
118131}
119132
120- export function zodfetchOffsetLimitPage < TItemSchema extends z . ZodTypeAny > (
133+ export function zodfetchOffsetLimitPage < TItemSchema extends AnyZodSchema > (
121134 schema : TItemSchema ,
122135 url : string ,
123136 params : FetchOffsetLimitPageParams ,
@@ -134,22 +147,33 @@ export function zodfetchOffsetLimitPage<TItemSchema extends z.ZodTypeAny>(
134147 query . set ( "page" , String ( params . page ) ) ;
135148 }
136149
137- const offsetLimitPageSchema = z . object ( {
138- data : z . array ( schema ) ,
139- pagination : z . object ( {
140- currentPage : z . coerce . number ( ) ,
141- totalPages : z . coerce . number ( ) ,
142- count : z . coerce . number ( ) ,
143- } ) ,
144- } ) ;
150+ const offsetLimitPageSchema = "_zod" in schema
151+ ? z . object ( {
152+ data : z . array ( schema as any ) ,
153+ pagination : z . object ( {
154+ currentPage : z . coerce . number ( ) ,
155+ totalPages : z . coerce . number ( ) ,
156+ count : z . coerce . number ( ) ,
157+ } ) ,
158+ } )
159+ : z3 . object ( {
160+ data : z3 . array ( schema as any ) ,
161+ pagination : z3 . object ( {
162+ currentPage : z3 . coerce . number ( ) ,
163+ totalPages : z3 . coerce . number ( ) ,
164+ count : z3 . coerce . number ( ) ,
165+ } ) ,
166+ } ) ;
145167
146168 const $url = new URL ( url ) ;
147169 $url . search = query . toString ( ) ;
148170
149171 const fetchResult = _doZodFetch ( offsetLimitPageSchema , $url . href , requestInit , options ) ;
150172
151173 return new OffsetLimitPagePromise (
152- fetchResult as Promise < ZodFetchResult < OffsetLimitPageResponse < z . output < TItemSchema > > > > ,
174+ fetchResult as Promise <
175+ ZodFetchResult < OffsetLimitPageResponse < inferZodSchemaOutput < TItemSchema > > >
176+ > ,
153177 schema ,
154178 url ,
155179 params ,
@@ -195,12 +219,12 @@ async function traceZodFetch<T>(
195219 ) ;
196220}
197221
198- async function _doZodFetch < TResponseBodySchema extends z . ZodTypeAny > (
222+ async function _doZodFetch < TResponseBodySchema extends AnyZodSchema > (
199223 schema : TResponseBodySchema ,
200224 url : string ,
201225 requestInit ?: PromiseOrValue < RequestInit > ,
202- options ?: ZodFetchOptions < z . output < TResponseBodySchema > >
203- ) : Promise < ZodFetchResult < z . output < TResponseBodySchema > > > {
226+ options ?: ZodFetchOptions < inferZodSchemaOutput < TResponseBodySchema > >
227+ ) : Promise < ZodFetchResult < inferZodSchemaOutput < TResponseBodySchema > > > {
204228 let $requestInit = await requestInit ;
205229
206230 return traceZodFetch ( { url, requestInit : $requestInit , options } , async ( span ) => {
@@ -223,13 +247,13 @@ async function _doZodFetch<TResponseBodySchema extends z.ZodTypeAny>(
223247 } ) ;
224248}
225249
226- async function _doZodFetchWithRetries < TResponseBodySchema extends z . ZodTypeAny > (
250+ async function _doZodFetchWithRetries < TResponseBodySchema extends AnyZodSchema > (
227251 schema : TResponseBodySchema ,
228252 url : string ,
229253 requestInit ?: RequestInit ,
230254 options ?: ZodFetchOptions ,
231255 attempt = 1
232- ) : Promise < ZodFetchResult < z . output < TResponseBodySchema > > > {
256+ ) : Promise < ZodFetchResult < inferZodSchemaOutput < TResponseBodySchema > > > {
233257 try {
234258 const response = await context . with ( suppressTracing ( context . active ( ) ) , ( ) =>
235259 fetch ( url , requestInitWithCache ( requestInit ) )
@@ -467,12 +491,12 @@ export class ApiPromise<T> extends Promise<T> {
467491 }
468492}
469493
470- export class CursorPagePromise < TItemSchema extends z . ZodTypeAny >
471- extends ApiPromise < CursorPage < z . output < TItemSchema > > >
472- implements AsyncIterable < z . output < TItemSchema > >
494+ export class CursorPagePromise < TItemSchema extends AnyZodSchema >
495+ extends ApiPromise < CursorPage < inferZodSchemaOutput < TItemSchema > > >
496+ implements AsyncIterable < inferZodSchemaOutput < TItemSchema > >
473497{
474498 constructor (
475- result : Promise < ZodFetchResult < CursorPageResponse < z . output < TItemSchema > > > > ,
499+ result : Promise < ZodFetchResult < CursorPageResponse < inferZodSchemaOutput < TItemSchema > > > > ,
476500 private schema : TItemSchema ,
477501 private url : string ,
478502 private params : FetchCursorPageParams ,
@@ -487,7 +511,9 @@ export class CursorPagePromise<TItemSchema extends z.ZodTypeAny>
487511 ) ;
488512 }
489513
490- #fetchPage( params : Omit < CursorPageParams , "limit" > ) : Promise < CursorPage < z . output < TItemSchema > > > {
514+ #fetchPage(
515+ params : Omit < CursorPageParams , "limit" >
516+ ) : Promise < CursorPage < inferZodSchemaOutput < TItemSchema > > > {
491517 return zodfetchCursorPage (
492518 this . schema ,
493519 this . url ,
@@ -512,12 +538,12 @@ export class CursorPagePromise<TItemSchema extends z.ZodTypeAny>
512538 }
513539}
514540
515- export class OffsetLimitPagePromise < TItemSchema extends z . ZodTypeAny >
516- extends ApiPromise < OffsetLimitPage < z . output < TItemSchema > > >
517- implements AsyncIterable < z . output < TItemSchema > >
541+ export class OffsetLimitPagePromise < TItemSchema extends AnyZodSchema >
542+ extends ApiPromise < OffsetLimitPage < inferZodSchemaOutput < TItemSchema > > >
543+ implements AsyncIterable < inferZodSchemaOutput < TItemSchema > >
518544{
519545 constructor (
520- result : Promise < ZodFetchResult < OffsetLimitPageResponse < z . output < TItemSchema > > > > ,
546+ result : Promise < ZodFetchResult < OffsetLimitPageResponse < inferZodSchemaOutput < TItemSchema > > > > ,
521547 private schema : TItemSchema ,
522548 private url : string ,
523549 private params : FetchOffsetLimitPageParams ,
@@ -538,7 +564,7 @@ export class OffsetLimitPagePromise<TItemSchema extends z.ZodTypeAny>
538564
539565 #fetchPage(
540566 params : Omit < FetchOffsetLimitPageParams , "limit" >
541- ) : Promise < OffsetLimitPage < z . output < TItemSchema > > > {
567+ ) : Promise < OffsetLimitPage < inferZodSchemaOutput < TItemSchema > > > {
542568 return zodfetchOffsetLimitPage (
543569 this . schema ,
544570 this . url ,
@@ -643,17 +669,18 @@ function injectRequestIdempotencyKey(
643669 } ;
644670}
645671
646- export type ZodFetchSSEMessageValueSchema <
647- TDiscriminatedUnion extends z . ZodDiscriminatedUnion < any , any > ,
648- > = z . ZodFirstPartySchemaTypes | TDiscriminatedUnion ;
672+ export type ZodFetchSSEMessageValueSchema < TSchema extends AnyZodSchema = AnyZodSchema > =
673+ TSchema ;
649674
650675export interface ZodFetchSSEMessageCatalogSchema {
651- [ key : string ] : ZodFetchSSEMessageValueSchema < any > ;
676+ [ key : string ] : AnyZodSchema ;
652677}
653678
654679export type ZodFetchSSEMessageHandlers < TCatalogSchema extends ZodFetchSSEMessageCatalogSchema > =
655680 Partial < {
656- [ K in keyof TCatalogSchema ] : ( payload : z . infer < TCatalogSchema [ K ] > ) => Promise < void > | void ;
681+ [ K in keyof TCatalogSchema ] : (
682+ payload : inferZodSchemaOutput < TCatalogSchema [ K ] >
683+ ) => Promise < void > | void ;
657684 } > ;
658685
659686export type ZodFetchSSEOptions < TMessageCatalog extends ZodFetchSSEMessageCatalogSchema > = {
@@ -698,6 +725,10 @@ export class ZodFetchSSEResult<TMessageCatalog extends ZodFetchSSEMessageCatalog
698725
699726 const schema = this . options . messages [ type ] ;
700727
728+ if ( ! schema ) {
729+ return ;
730+ }
731+
701732 const result = schema . safeParse ( payload ) ;
702733
703734 if ( result . success ) {
@@ -733,12 +764,12 @@ export type ApiResult<TSuccessResult> =
733764 statusCode ?: number ;
734765 } ;
735766
736- export async function wrapZodFetch < T extends z . ZodTypeAny > (
767+ export async function wrapZodFetch < T extends AnyZodSchema > (
737768 schema : T ,
738769 url : string ,
739770 requestInit ?: RequestInit ,
740- options ?: ZodFetchOptions < z . output < T > >
741- ) : Promise < ApiResult < z . infer < T > > > {
771+ options ?: ZodFetchOptions < inferZodSchemaOutput < T > >
772+ ) : Promise < ApiResult < inferZodSchemaOutput < T > > > {
742773 try {
743774 const response = await zodfetch ( schema , url , requestInit , {
744775 retry : {
0 commit comments