1
1
import type { RequestBody } from '../../../types' ;
2
2
3
- // TODO: a possible improvement in precision of conveying document type:
4
- // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#other_methods_of_conveying_document_type
5
3
const defaultRequestMimeTypes = {
6
4
string : 'application/json' ,
7
5
Blob : 'application/octet-stream' ,
6
+ File : 'application/octet-stream' ,
8
7
ArrayBuffer : 'application/octet-stream' ,
9
8
DataView : 'application/octet-stream' ,
10
9
ReadableStream : 'application/octet-stream' ,
11
10
URLSearchParams : 'application/x-www-form-urlencoded' ,
11
+ FormData : 'multipart/form-data' ,
12
12
} as const ;
13
13
14
14
type RequestBodyType = keyof typeof defaultRequestMimeTypes ;
@@ -18,7 +18,22 @@ function getBodyType(body: RequestBody): RequestBodyType {
18
18
return typeof body === 'string' ? 'string' : body [ Symbol . toStringTag ] ;
19
19
}
20
20
21
- export function getDefaultRequestMimeType ( body : RequestBody ) : RequestMimeType | undefined {
21
+ export function getDefaultRequestMimeType ( body : RequestBody ) : RequestMimeType | string | undefined {
22
22
const bodyType = getBodyType ( body ) ;
23
- return defaultRequestMimeTypes [ bodyType ] ;
23
+ const defaultMimeType = defaultRequestMimeTypes [ bodyType ] ;
24
+
25
+ switch ( bodyType ) {
26
+ case 'Blob' :
27
+ case 'File' :
28
+ const blob = body as Blob ;
29
+ return blob . type || defaultMimeType ;
30
+
31
+ case 'FormData' :
32
+ // Detect mime-type from the 1st form data value
33
+ const formData = body as FormData ;
34
+ const firstItem = formData . values ( ) . next ( ) . value ;
35
+ return getDefaultRequestMimeType ( firstItem ) ;
36
+ }
37
+
38
+ return defaultMimeType ;
24
39
}
0 commit comments