File tree Expand file tree Collapse file tree 3 files changed +26
-8
lines changed Expand file tree Collapse file tree 3 files changed +26
-8
lines changed Original file line number Diff line number Diff line change @@ -127,9 +127,7 @@ export const betterFetch = async <
127
127
}
128
128
129
129
if ( options ?. throw ) {
130
- return {
131
- data : successContext . data ,
132
- } as any ;
130
+ return successContext . data as any ;
133
131
}
134
132
135
133
return {
Original file line number Diff line number Diff line change @@ -224,3 +224,27 @@ describe("fetch-error", () => {
224
224
await expect ( f ( "/ok" ) ) . rejects . toThrowError ( BetterFetchError ) ;
225
225
} ) ;
226
226
} ) ;
227
+
228
+ describe ( "fetch-error-throw" , ( ) => {
229
+ const f = createFetch ( {
230
+ baseURL : "http://localhost:4001" ,
231
+ customFetchImpl : async ( req , init ) => {
232
+ const url = new URL ( req . toString ( ) ) ;
233
+ if ( url . pathname . startsWith ( "/ok" ) ) {
234
+ return new Response ( JSON . stringify ( { message : "ok" } ) ) ;
235
+ }
236
+ return new Response ( null , {
237
+ status : 500 ,
238
+ } ) ;
239
+ } ,
240
+ throw : true ,
241
+ } ) ;
242
+ it ( "should throw if the response is not ok" , async ( ) => {
243
+ await expect ( f ( "/not-ok" ) ) . rejects . toThrowError ( BetterFetchError ) ;
244
+ } ) ;
245
+
246
+ it ( "should return data without error object" , async ( ) => {
247
+ const res = await f < { message : "ok" } > ( "/ok" ) ;
248
+ expect ( res ) . toEqual ( { message : "ok" } ) ;
249
+ } ) ;
250
+ } ) ;
Original file line number Diff line number Diff line change @@ -149,8 +149,4 @@ export type BetterFetchResponse<
149
149
T ,
150
150
E extends Record < string , unknown > | unknown = unknown ,
151
151
Throw extends boolean = false ,
152
- > = Throw extends true
153
- ? {
154
- data : T ;
155
- }
156
- : Data < T > | Error < E > ;
152
+ > = Throw extends true ? T : Data < T > | Error < E > ;
You can’t perform that action at this time.
0 commit comments