@@ -6,8 +6,8 @@ import Blob from './Blob'
66
77const log = new Log ( 'FetchPolyfill' )
88
9- // log.level(3)
109log . disable ( )
10+ // log.level(3)
1111
1212export default class Fetch {
1313
@@ -23,43 +23,49 @@ class RNFetchBlobFetchPolyfill {
2323 this . build = ( ) => ( url , options = { } ) => {
2424
2525 let body = options . body
26- let promise = null
26+ let promise = Promise . resolve ( )
2727 let blobCache = null
2828
2929 options . headers = options . headers || { }
30- options [ 'Content-Type' ] = options . headers [ 'Content-Type' ] || options . headers [ 'content-type' ]
31- options [ 'content-type' ] = options . headers [ 'Content-Type' ] || options . headers [ 'content-type' ]
32-
33- // When the request body is an instance of FormData, create a Blob cache
34- // to upload the body.
35- if ( body instanceof FormData ) {
36- promise = Blob . build ( body ) . then ( ( b ) => {
37- blobCache = b
38- return Promise . resolve ( b . getRNFetchBlobRef ( ) )
39- } )
30+ let ctype = options [ 'Content-Type' ] || options [ 'content-type' ]
31+ let ctypeH = options . headers [ 'Content-Type' ] || options . headers [ 'content-type' ]
32+ options . headers [ 'Content-Type' ] = ctype || ctypeH
33+ options . headers [ 'content-type' ] = ctype || ctypeH
34+ options . method = options . method || 'GET'
35+
36+ if ( body ) {
37+ // When the request body is an instance of FormData, create a Blob cache
38+ // to upload the body.
39+ if ( body instanceof FormData ) {
40+ log . verbose ( 'convert FormData to blob body' )
41+ promise = Blob . build ( body ) . then ( ( b ) => {
42+ blobCache = b
43+ options . headers [ 'Content-Type' ] = 'multipart/form-data;boundary=' + b . multipartBoundary
44+ return Promise . resolve ( RNFetchBlob . wrap ( b . _ref ) )
45+ } )
46+ }
47+ // When request body is a Blob, use file URI of the Blob as request body.
48+ else if ( body . isRNFetchBlobPolyfill )
49+ promise = Promise . resolve ( RNFetchBlob . wrap ( body . blobPath ) )
50+ // send it as-is, leave the native module decide how to send the body.
51+ else
52+ promise = Promise . resolve ( body )
4053 }
41- // When request body is a Blob, use file URI of the Blob as request body.
42- else if ( body instanceof Blob )
43- promise = Promise . resolve ( RNFetchBlob . wrap ( body . getRNFetchBlobRef ( ) ) )
44- // send it as-is, leave the native module decide how to send the body.
45- else
46- promise = Promise . resolve ( body )
4754
4855 // task is a progress reportable and cancellable Promise, however,
4956 // task.then is not, so we have to extend task.then with progress and
5057 // cancel function
5158 let task = promise
5259 . then ( ( body ) => {
5360 return RNFetchBlob . config ( config )
54- . fetch ( options . method , url , options . headers , options . body )
61+ . fetch ( options . method , url , options . headers , body )
5562 } )
5663
5764 let statefulPromise = task . then ( ( resp ) => {
5865 log . verbose ( 'response' , resp )
5966 // release blob cache created when sending request
6067 if ( blobCache !== null && blobCache instanceof Blob )
6168 blobCache . close ( )
62- let info = resp . info ( )
6369 return Promise . resolve ( new RNFetchBlobFetchRepsonse ( resp ) )
6470 } )
6571
@@ -128,7 +134,6 @@ function readText(resp, info):Promise<string> {
128134 break
129135 case 'path' :
130136 return resp . readFile ( 'utf8' ) . then ( ( data ) => {
131- data = unicode ( data )
132137 return Promise . resolve ( data )
133138 } )
134139 break
0 commit comments