ソースを参照

Add Fetch response reader function

Ben Hsieh 8 年 前
コミット
fef20b52be
共有1 個のファイルを変更した53 個の追加9 個の削除を含む
  1. 53
    9
      src/polyfill/Fetch.js

+ 53
- 9
src/polyfill/Fetch.js ファイルの表示

@@ -6,7 +6,8 @@ import Blob from './Blob'
6 6
 
7 7
 const log = new Log('FetchPolyfill')
8 8
 
9
-log.level(3)
9
+// log.level(3)
10
+log.disable()
10 11
 
11 12
 export default class Fetch {
12 13
 
@@ -20,16 +21,41 @@ class RNFetchBlobFetchPolyfill {
20 21
 
21 22
   constructor(config:RNFetchBlobConfig) {
22 23
     this.build = () => (url, options = {}) => {
24
+
25
+      let body = options.body
26
+      let promise = null
27
+      let blobCache = null
28
+
23 29
       options.headers = options.headers || {}
24 30
       options['Content-Type'] = options.headers['Content-Type'] || options.headers['content-type']
25 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,7 +101,12 @@ class RNFetchBlobFetchRepsonse {
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 110
 function readText(resp, info):Promise<string> {
80 111
   switch (info.rnfbEncode) {
81 112
     case 'base64':
@@ -93,7 +124,14 @@ function readText(resp, info):Promise<string> {
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 135
   log.verbose('readBlob', resp, info)
98 136
   let cType = info.headers['Content-Type']
99 137
   switch (info.rnfbEncode) {
@@ -106,6 +144,12 @@ function readBlob(resp, info):Promise<object> {
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 153
 function readJSON(resp, info):Promise<object> {
110 154
   log.verbose('readJSON', resp, info)
111 155
   switch (info.rnfbEncode) {