@@ -73,4 +73,77 @@ describe('ffetch fetchHandler option', () => {
7373 expect ( global . fetch ) . toHaveBeenCalled ( )
7474 global . fetch = originalFetch
7575 } )
76+
77+ it ( 'allows per-request fetchHandler override' , async ( ) => {
78+ const clientFetchSpy = vi . fn ( mockFetch )
79+ const requestFetchSpy = vi . fn ( ( ) =>
80+ Promise . resolve (
81+ new Response ( JSON . stringify ( { from : 'request' } ) , { status : 200 } )
82+ )
83+ )
84+
85+ const client = createClient ( { fetchHandler : clientFetchSpy } )
86+ const res = await client ( 'https://example.com' , {
87+ fetchHandler : requestFetchSpy ,
88+ } )
89+ const json = await res . json ( )
90+
91+ expect ( clientFetchSpy ) . not . toHaveBeenCalled ( )
92+ expect ( requestFetchSpy ) . toHaveBeenCalled ( )
93+ expect ( json . from ) . toBe ( 'request' )
94+ } )
95+
96+ it ( 'uses client fetchHandler when no per-request override is provided' , async ( ) => {
97+ const clientFetchSpy = vi . fn ( ( ) =>
98+ Promise . resolve (
99+ new Response ( JSON . stringify ( { from : 'client' } ) , { status : 200 } )
100+ )
101+ )
102+
103+ const client = createClient ( { fetchHandler : clientFetchSpy } )
104+ const res = await client ( 'https://example.com' )
105+ const json = await res . json ( )
106+
107+ expect ( clientFetchSpy ) . toHaveBeenCalled ( )
108+ expect ( json . from ) . toBe ( 'client' )
109+ } )
110+
111+ it ( 'supports per-request fetchHandler without client-level handler' , async ( ) => {
112+ const requestFetchSpy = vi . fn ( ( ) =>
113+ Promise . resolve (
114+ new Response ( JSON . stringify ( { from : 'request-only' } ) , { status : 200 } )
115+ )
116+ )
117+
118+ const client = createClient ( { retries : 0 } )
119+ const res = await client ( 'https://example.com' , {
120+ fetchHandler : requestFetchSpy ,
121+ } )
122+ const json = await res . json ( )
123+
124+ expect ( requestFetchSpy ) . toHaveBeenCalled ( )
125+ expect ( json . from ) . toBe ( 'request-only' )
126+ } )
127+
128+ it ( 'allows different fetchHandlers for different requests on same client' , async ( ) => {
129+ const fetch1 = vi . fn ( ( ) =>
130+ Promise . resolve ( new Response ( JSON . stringify ( { id : 1 } ) , { status : 200 } ) )
131+ )
132+ const fetch2 = vi . fn ( ( ) =>
133+ Promise . resolve ( new Response ( JSON . stringify ( { id : 2 } ) , { status : 200 } ) )
134+ )
135+
136+ const client = createClient ( { retries : 0 } )
137+
138+ const res1 = await client ( 'https://test1.com' , { fetchHandler : fetch1 } )
139+ const json1 = await res1 . json ( )
140+
141+ const res2 = await client ( 'https://test2.com' , { fetchHandler : fetch2 } )
142+ const json2 = await res2 . json ( )
143+
144+ expect ( fetch1 ) . toHaveBeenCalledTimes ( 1 )
145+ expect ( fetch2 ) . toHaveBeenCalledTimes ( 1 )
146+ expect ( json1 . id ) . toBe ( 1 )
147+ expect ( json2 . id ) . toBe ( 2 )
148+ } )
76149} )
0 commit comments