@@ -190,37 +190,38 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch {
190
190
...context . options . query ,
191
191
} ) ;
192
192
}
193
- if ( context . options . body && isPayloadMethod ( context . options . method ) ) {
194
- if ( isJSONSerializable ( context . options . body ) ) {
195
- // JSON Body
196
- // Automatically JSON stringify request bodies, when not already a string.
197
- context . options . body =
198
- typeof context . options . body === "string"
199
- ? context . options . body
200
- : JSON . stringify ( context . options . body ) ;
201
-
202
- // Set Content-Type and Accept headers to application/json by default
203
- // for JSON serializable request bodies.
204
- // Pass empty object as older browsers don't support undefined.
205
- context . options . headers = new Headers ( context . options . headers || { } ) ;
206
- if ( ! context . options . headers . has ( "content-type" ) ) {
207
- context . options . headers . set ( "content-type" , "application/json" ) ;
208
- }
209
- if ( ! context . options . headers . has ( "accept" ) ) {
210
- context . options . headers . set ( "accept" , "application/json" ) ;
211
- }
212
- } else if (
213
- // ReadableStream Body
214
- ( "pipeTo" in ( context . options . body as ReadableStream ) &&
215
- typeof ( context . options . body as ReadableStream ) . pipeTo ===
216
- "function" ) ||
217
- // Node.js Stream Body
218
- typeof ( context . options . body as Readable ) . pipe === "function"
219
- ) {
220
- // eslint-disable-next-line unicorn/no-lonely-if
221
- if ( ! ( "duplex" in context . options ) ) {
222
- context . options . duplex = "half" ;
223
- }
193
+ }
194
+
195
+ if ( context . options . body && isPayloadMethod ( context . options . method ) ) {
196
+ if ( isJSONSerializable ( context . options . body ) ) {
197
+ // JSON Body
198
+ // Automatically JSON stringify request bodies, when not already a string.
199
+ context . options . body =
200
+ typeof context . options . body === "string"
201
+ ? context . options . body
202
+ : JSON . stringify ( context . options . body ) ;
203
+
204
+ // Set Content-Type and Accept headers to application/json by default
205
+ // for JSON serializable request bodies.
206
+ // Pass empty object as older browsers don't support undefined.
207
+ context . options . headers = new Headers ( context . options . headers || { } ) ;
208
+ if ( ! context . options . headers . has ( "content-type" ) ) {
209
+ context . options . headers . set ( "content-type" , "application/json" ) ;
210
+ }
211
+ if ( ! context . options . headers . has ( "accept" ) ) {
212
+ context . options . headers . set ( "accept" , "application/json" ) ;
213
+ }
214
+ } else if (
215
+ // ReadableStream Body
216
+ ( "pipeTo" in ( context . options . body as ReadableStream ) &&
217
+ typeof ( context . options . body as ReadableStream ) . pipeTo ===
218
+ "function" ) ||
219
+ // Node.js Stream Body
220
+ typeof ( context . options . body as Readable ) . pipe === "function"
221
+ ) {
222
+ // eslint-disable-next-line unicorn/no-lonely-if
223
+ if ( ! ( "duplex" in context . options ) ) {
224
+ context . options . duplex = "half" ;
224
225
}
225
226
}
226
227
}
@@ -257,14 +258,27 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch {
257
258
detectResponseType ( context . response . headers . get ( "content-type" ) || "" ) ;
258
259
259
260
// We override the `.json()` method to parse the body more securely with `destr`
260
- if ( responseType === "json" ) {
261
- const data = await context . response . text ( ) ;
262
- const parseFunction = context . options . parseResponse || destr ;
263
- context . response . _data = parseFunction ( data ) ;
264
- } else if ( responseType === "stream" ) {
265
- context . response . _data = context . response . body ;
266
- } else {
267
- context . response . _data = await context . response [ responseType ] ( ) ;
261
+ switch ( responseType ) {
262
+ case "json" : {
263
+ const data = await context . response . text ( ) ;
264
+ const parseFunction = context . options . parseResponse || destr ;
265
+ context . response . _data = parseFunction ( data ) ;
266
+
267
+ break ;
268
+ }
269
+ case "stream" : {
270
+ context . response . _data = context . response . body ;
271
+
272
+ break ;
273
+ }
274
+ case "raw" : {
275
+ context . response . _data = context . response . body ;
276
+
277
+ break ;
278
+ }
279
+ default : {
280
+ context . response . _data = await context . response [ responseType ] ( ) ;
281
+ }
268
282
}
269
283
}
270
284
0 commit comments