@@ -5,7 +5,7 @@ import { calculateNextRetryDelay } from "../utils/retries.js";
55import { ApiConnectionError , ApiError , ApiSchemaValidationError } from "./errors.js" ;
66
77import { Attributes , context , propagation , Span } from "@opentelemetry/api" ;
8- import { suppressTracing } from "@opentelemetry/core"
8+ import { suppressTracing } from "@opentelemetry/core" ;
99import { SemanticInternalAttributes } from "../semanticInternalAttributes.js" ;
1010import type { TriggerTracer } from "../tracer.js" ;
1111import { accessoryAttributes } from "../utils/styleAttributes.js" ;
@@ -27,14 +27,14 @@ export const defaultRetryOptions = {
2727 randomize : false ,
2828} satisfies RetryOptions ;
2929
30- export type ZodFetchOptions < T = unknown > = {
30+ export type ZodFetchOptions < TInput = unknown , TOutput = TInput > = {
3131 retry ?: RetryOptions ;
3232 tracer ?: TriggerTracer ;
3333 name ?: string ;
3434 attributes ?: Attributes ;
3535 icon ?: string ;
36- onResponseBody ?: ( body : T , span : Span ) => void ;
37- prepareData ?: ( data : T ) => Promise < T > | T ;
36+ onResponseBody ?: ( body : TInput , span : Span ) => void ;
37+ prepareData ?: ( data : TInput , response : Response ) => Promise < TOutput > | TOutput ;
3838} ;
3939
4040export type AnyZodFetchOptions = ZodFetchOptions < any > ;
@@ -67,12 +67,15 @@ interface FetchOffsetLimitPageParams extends OffsetLimitPageParams {
6767 query ?: URLSearchParams ;
6868}
6969
70- export function zodfetch < TResponseBodySchema extends z . ZodTypeAny > (
70+ export function zodfetch <
71+ TResponseBodySchema extends z . ZodTypeAny ,
72+ TOutput = z . output < TResponseBodySchema > ,
73+ > (
7174 schema : TResponseBodySchema ,
7275 url : string ,
7376 requestInit ?: RequestInit ,
74- options ?: ZodFetchOptions < z . output < TResponseBodySchema > >
75- ) : ApiPromise < z . output < TResponseBodySchema > > {
77+ options ?: ZodFetchOptions < z . output < TResponseBodySchema > , TOutput >
78+ ) : ApiPromise < TOutput > {
7679 return new ApiPromise ( _doZodFetch ( schema , url , requestInit , options ) ) ;
7780}
7881
@@ -110,7 +113,14 @@ export function zodfetchCursorPage<TItemSchema extends z.ZodTypeAny>(
110113
111114 const fetchResult = _doZodFetch ( cursorPageSchema , $url . href , requestInit , options ) ;
112115
113- return new CursorPagePromise ( fetchResult , schema , url , params , requestInit , options ) ;
116+ return new CursorPagePromise (
117+ fetchResult as Promise < ZodFetchResult < CursorPageResponse < z . output < TItemSchema > > > > ,
118+ schema ,
119+ url ,
120+ params ,
121+ requestInit ,
122+ options
123+ ) ;
114124}
115125
116126export function zodfetchOffsetLimitPage < TItemSchema extends z . ZodTypeAny > (
@@ -144,7 +154,14 @@ export function zodfetchOffsetLimitPage<TItemSchema extends z.ZodTypeAny>(
144154
145155 const fetchResult = _doZodFetch ( offsetLimitPageSchema , $url . href , requestInit , options ) ;
146156
147- return new OffsetLimitPagePromise ( fetchResult , schema , url , params , requestInit , options ) ;
157+ return new OffsetLimitPagePromise (
158+ fetchResult as Promise < ZodFetchResult < OffsetLimitPageResponse < z . output < TItemSchema > > > > ,
159+ schema ,
160+ url ,
161+ params ,
162+ requestInit ,
163+ options
164+ ) ;
148165}
149166
150167type ZodFetchResult < T > = {
@@ -184,12 +201,15 @@ async function traceZodFetch<T>(
184201 ) ;
185202}
186203
187- async function _doZodFetch < TResponseBodySchema extends z . ZodTypeAny > (
204+ async function _doZodFetch <
205+ TResponseBodySchema extends z . ZodTypeAny ,
206+ TOutput = z . output < TResponseBodySchema > ,
207+ > (
188208 schema : TResponseBodySchema ,
189209 url : string ,
190210 requestInit ?: PromiseOrValue < RequestInit > ,
191- options ?: ZodFetchOptions
192- ) : Promise < ZodFetchResult < z . output < TResponseBodySchema > > > {
211+ options ?: ZodFetchOptions < z . output < TResponseBodySchema > , TOutput >
212+ ) : Promise < ZodFetchResult < TOutput > > {
193213 let $requestInit = await requestInit ;
194214
195215 return traceZodFetch ( { url, requestInit : $requestInit , options } , async ( span ) => {
@@ -202,7 +222,7 @@ async function _doZodFetch<TResponseBodySchema extends z.ZodTypeAny>(
202222 }
203223
204224 if ( options ?. prepareData ) {
205- result . data = await options . prepareData ( result . data ) ;
225+ result . data = await options . prepareData ( result . data , result . response ) ;
206226 }
207227
208228 return result ;
@@ -707,7 +727,7 @@ export async function wrapZodFetch<T extends z.ZodTypeAny>(
707727 schema : T ,
708728 url : string ,
709729 requestInit ?: RequestInit ,
710- options ?: ZodFetchOptions < z . output < T > >
730+ options ?: ZodFetchOptions < z . output < T > , z . infer < T > >
711731) : Promise < ApiResult < z . infer < T > > > {
712732 try {
713733 const response = await zodfetch ( schema , url , requestInit , {
0 commit comments