@@ -6,7 +6,8 @@ import Blob from './Blob'
66
77const log = new Log ( 'FetchPolyfill' )
88
9- log . level ( 3 )
9+ // log.level(3)
10+ log . disable ( )
1011
1112export default class Fetch {
1213
@@ -20,16 +21,41 @@ class RNFetchBlobFetchPolyfill {
2021
2122 constructor ( config :RNFetchBlobConfig ) {
2223 this . build = ( ) => ( url , options = { } ) => {
24+
25+ let body = options . body
26+ let promise = null
27+ let blobCache = null
28+
2329 options . headers = options . headers || { }
2430 options [ 'Content-Type' ] = options . headers [ 'Content-Type' ] || options . headers [ 'content-type' ]
2531 options [ 'content-type' ] = options . headers [ 'Content-Type' ] || options . headers [ 'content-type' ]
26- return RNFetchBlob . config ( config )
27- . fetch ( options . method , url , options . headers , options . body )
28- . then ( ( resp ) => {
29- log . verbose ( 'response' , resp )
30- let info = resp . info ( )
31- return Promise . resolve ( new RNFetchBlobFetchRepsonse ( resp ) )
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 ( ) )
3239 } )
40+ }
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 )
47+
48+ return promise
49+ . then ( ( body ) => RNFetchBlob . config ( config )
50+ . fetch ( options . method , url , options . headers , options . body ) )
51+ . then ( ( resp ) => {
52+ log . verbose ( 'response' , resp )
53+ // release blob cache created when sending request
54+ if ( blobCache !== null && blobCache instanceof Blob )
55+ blobCache . close ( )
56+ let info = resp . info ( )
57+ return Promise . resolve ( new RNFetchBlobFetchRepsonse ( resp ) )
58+ } )
3359 }
3460 }
3561
@@ -75,7 +101,12 @@ class RNFetchBlobFetchRepsonse {
75101 }
76102}
77103
78-
104+ /**
105+ * Get response data as string.
106+ * @param {FetchBlobResponse } resp Response data object from RNFB fetch call.
107+ * @param {RNFetchBlobResponseInfo } info Response informations.
108+ * @return {Promise<string> }
109+ */
79110function readText ( resp , info ) :Promise < string > {
80111 switch ( info . rnfbEncode ) {
81112 case 'base64' :
@@ -93,7 +124,14 @@ function readText(resp, info):Promise<string> {
93124 }
94125}
95126
96- function readBlob ( resp , info ) :Promise < object > {
127+
128+ /**
129+ * Get response data as RNFetchBlob Blob polyfill object.
130+ * @param {FetchBlobResponse } resp Response data object from RNFB fetch call.
131+ * @param {RNFetchBlobResponseInfo } info Response informations.
132+ * @return {Promise<Blob> }
133+ */
134+ function readBlob ( resp , info ) :Promise < Blob > {
97135 log . verbose ( 'readBlob' , resp , info )
98136 let cType = info . headers [ 'Content-Type' ]
99137 switch ( info . rnfbEncode ) {
@@ -106,6 +144,12 @@ function readBlob(resp, info):Promise<object> {
106144 }
107145}
108146
147+ /**
148+ * Get response data as JSON object.
149+ * @param {FetchBlobResponse } resp Response data object from RNFB fetch call.
150+ * @param {RNFetchBlobResponseInfo } info Response informations.
151+ * @return {Promise<object> }
152+ */
109153function readJSON ( resp , info ) :Promise < object > {
110154 log . verbose ( 'readJSON' , resp , info )
111155 switch ( info . rnfbEncode ) {
0 commit comments