Browse Source

Add Fetch response reader function

Ben Hsieh 8 years ago
parent
commit
fef20b52be
1 changed files with 53 additions and 9 deletions
  1. 53
    9
      src/polyfill/Fetch.js

+ 53
- 9
src/polyfill/Fetch.js View File

6
 
6
 
7
 const log = new Log('FetchPolyfill')
7
 const log = new Log('FetchPolyfill')
8
 
8
 
9
-log.level(3)
9
+// log.level(3)
10
+log.disable()
10
 
11
 
11
 export default class Fetch {
12
 export default class Fetch {
12
 
13
 
20
 
21
 
21
   constructor(config:RNFetchBlobConfig) {
22
   constructor(config:RNFetchBlobConfig) {
22
     this.build = () => (url, options = {}) => {
23
     this.build = () => (url, options = {}) => {
24
+
25
+      let body = options.body
26
+      let promise = null
27
+      let blobCache = null
28
+
23
       options.headers = options.headers || {}
29
       options.headers = options.headers || {}
24
       options['Content-Type'] = options.headers['Content-Type'] || options.headers['content-type']
30
       options['Content-Type'] = options.headers['Content-Type'] || options.headers['content-type']
25
       options['content-type'] = options.headers['Content-Type'] || options.headers['content-type']
31
       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())
32
         })
39
         })
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
+          })
33
     }
59
     }
34
   }
60
   }
35
 
61
 
75
   }
101
   }
76
 }
102
 }
77
 
103
 
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
+ */
79
 function readText(resp, info):Promise<string> {
110
 function readText(resp, info):Promise<string> {
80
   switch (info.rnfbEncode) {
111
   switch (info.rnfbEncode) {
81
     case 'base64':
112
     case 'base64':
93
   }
124
   }
94
 }
125
 }
95
 
126
 
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> {
97
   log.verbose('readBlob', resp, info)
135
   log.verbose('readBlob', resp, info)
98
   let cType = info.headers['Content-Type']
136
   let cType = info.headers['Content-Type']
99
   switch (info.rnfbEncode) {
137
   switch (info.rnfbEncode) {
106
   }
144
   }
107
 }
145
 }
108
 
146
 
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
+ */
109
 function readJSON(resp, info):Promise<object> {
153
 function readJSON(resp, info):Promise<object> {
110
   log.verbose('readJSON', resp, info)
154
   log.verbose('readJSON', resp, info)
111
   switch (info.rnfbEncode) {
155
   switch (info.rnfbEncode) {