1+ import {
2+ isArrayBuffer ,
3+ isBlob ,
4+ isBoolean ,
5+ isFormData ,
6+ isNumber ,
7+ isObject ,
8+ isUrlSearchParams ,
9+ } from '../shared' ;
110import { ReqBodySerializerReturn } from './types' ;
211
3- /**
4- * Safely assert whether the given value is an ArrayBuffer.
5- *
6- * In some execution environments ArrayBuffer is not defined.
7- */
8- function isArrayBuffer ( value : unknown ) : value is ArrayBuffer {
9- return typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer ;
10- }
11-
12- /**
13- * Safely assert whether the given value is a Blob.
14- *
15- * In some execution environments Blob is not defined.
16- */
17- function isBlob ( value : unknown ) : value is Blob {
18- return typeof Blob !== 'undefined' && value instanceof Blob ;
19- }
20-
21- /**
22- * Safely assert whether the given value is a FormData instance.
23- *
24- * In some execution environments FormData is not defined.
25- */
26- function isFormData ( value : unknown ) : value is FormData {
27- return typeof FormData !== 'undefined' && value instanceof FormData ;
28- }
29-
30- /**
31- * Safely assert whether the given value is a URLSearchParams instance.
32- *
33- * In some execution environments URLSearchParams is not defined.
34- */
35- function isUrlSearchParams ( value : unknown ) : value is URLSearchParams {
36- return typeof URLSearchParams !== 'undefined' && value instanceof URLSearchParams ;
37- }
38-
39- export function serializeRequestBody ( body : BodyInit ) : ReqBodySerializerReturn {
12+ export function serializeRequestBody ( body : unknown ) : ReqBodySerializerReturn {
4013 // If no body is present, no need to serialize it.
4114 if ( body === null || typeof body === 'undefined' ) {
4215 return null ;
@@ -54,11 +27,11 @@ export function serializeRequestBody(body: BodyInit): ReqBodySerializerReturn {
5427 return body ;
5528 }
5629
57- // Check whether the body is an object or array, and serialize with JSON if so.
58- if ( typeof body === 'object' || typeof body === 'boolean' || Array . isArray ( body ) ) {
30+ // Check whether the body is an object, an array, a boolean or a number , and serialize with JSON if so.
31+ if ( isObject ( body ) || isBoolean ( body ) || isNumber ( body ) || Array . isArray ( body ) ) {
5932 return JSON . stringify ( body ) ;
6033 }
6134
62- // Fall back to unmodified body.
63- return body ;
35+ // Fallback to body string literal .
36+ return ` ${ body } ` ;
6437}
0 commit comments