Ben Hsieh vor 7 Jahren
Ursprung
Commit
4d58919100
3 geänderte Dateien mit 79 neuen und 73 gelöschten Zeilen
  1. 1
    1
      src/index.js
  2. 73
    57
      src/polyfill/Fetch.js
  3. 5
    15
      test/test-0.8.2.js

+ 1
- 1
src/index.js Datei anzeigen

261
      * @return {object} Parsed javascript object.
261
      * @return {object} Parsed javascript object.
262
      */
262
      */
263
     this.json = ():any => {
263
     this.json = ():any => {
264
-      return JSON.parse(base64.decode(this.data))
264
+      return JSON.parse(decodeURIComponent(base64.decode(this.data)))
265
     }
265
     }
266
     /**
266
     /**
267
      * Return BASE64 string directly.
267
      * Return BASE64 string directly.

+ 73
- 57
src/polyfill/Fetch.js Datei anzeigen

7
 
7
 
8
 log.level(3)
8
 log.level(3)
9
 
9
 
10
-
11
 export default class Fetch {
10
 export default class Fetch {
12
 
11
 
13
-  provider:RNFetchBlobFetch;
14
-
15
   constructor(config:RNFetchBlobConfig) {
12
   constructor(config:RNFetchBlobConfig) {
16
-    this.provider = new RNFetchBlobFetch(config)
13
+    Object.assign(this, new RNFetchBlobFetchPolyfill(config))
17
   }
14
   }
18
 
15
 
19
 }
16
 }
20
 
17
 
21
-class RNFetchBlobFetch {
18
+class RNFetchBlobFetchPolyfill {
22
 
19
 
23
   constructor(config:RNFetchBlobConfig) {
20
   constructor(config:RNFetchBlobConfig) {
24
-    this._fetch = (url, options) => {
25
-      let bodyUsed = false
21
+    this.build = () => (url, options) => {
26
       options.headers = options.headers || {}
22
       options.headers = options.headers || {}
27
       options['Content-Type'] = options.headers['Content-Type'] || options.headers['content-type']
23
       options['Content-Type'] = options.headers['Content-Type'] || options.headers['content-type']
28
       options['content-type'] = options.headers['Content-Type'] || options.headers['content-type']
24
       options['content-type'] = options.headers['Content-Type'] || options.headers['content-type']
30
         .fetch(options.method, url, options.headers, options.body)
26
         .fetch(options.method, url, options.headers, options.body)
31
         .then((resp) => {
27
         .then((resp) => {
32
           let info = resp.info()
28
           let info = resp.info()
33
-          return Promise.resolve({
34
-            headers : info.headers,
35
-            ok : info.status >= 200 && info.status <= 299,
36
-            status : info.status,
37
-            type : 'basic',
38
-            bodyUsed,
39
-            arrayBuffer : () => {
40
-              log.verbose('to arrayBuffer', info)
41
-
42
-            },
43
-            text : () => {
44
-              log.verbose('to text', resp, info)
45
-              switch (info.rnfbEncode) {
46
-                case 'base64':
47
-                  let result = unicode(resp.text())
48
-                  return Promise.resolve(result)
49
-                  break
50
-                case 'path':
51
-                  return resp.readFile('utf8').then((data) => {
52
-                    data = unicode(data)
53
-                    return Promise.resolve(data)
54
-                  })
55
-                  break
56
-                case '':
57
-                default:
58
-                  return Promise.resolve(resp.data)
59
-                  break
60
-              }
61
-            },
62
-            json : () => {
63
-              log.verbose('to json', resp, info)
64
-              switch (info.rnfbEncode) {
65
-                case 'base64':
66
-                  return Promise.resolve(resp.json())
67
-                case 'path':
68
-                  return resp.readFile('utf8').then((data) => {
69
-                    return Promise.resolve(JSON.parse(data))
70
-                  })
71
-                case '':
72
-                default:
73
-                  return Promise.resolve(JSON.parse(resp.data))
74
-              }
75
-            },
76
-            formData : () => {
77
-              log.verbose('to formData', resp, info)
78
-
79
-            }
80
-          })
29
+          return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
81
         })
30
         })
82
     }
31
     }
83
   }
32
   }
84
 
33
 
85
-  get fetch() {
86
-    return this._fetch
34
+}
35
+
36
+class RNFetchBlobFetchRepsonse {
37
+
38
+  constructor(resp:FetchBlobResponse) {
39
+    let info = resp.info()
40
+    this.headers = info.headers
41
+    this.ok = info.status >= 200 && info.status <= 299,
42
+    this.status = info.status
43
+    this.type = 'basic'
44
+    this.bodyUsed = false
45
+    this.resp = resp
46
+    this.rnfbRespInfo = info
47
+    this.rnfbResp = resp
48
+  }
49
+
50
+  arrayBuffer(){
51
+    log.verbose('to arrayBuffer', this.rnfbRespInfo)
52
+    return readArrayBuffer(this.rnfbResp, this.rnfbRespInfo)
53
+  }
54
+
55
+  text() {
56
+    log.verbose('to text', this.rnfbResp, this.rnfbRespInfo)
57
+    return readText(this.rnfbResp, this.rnfbRespInfo)
58
+  }
59
+
60
+  json() {
61
+    log.verbose('to json', this.rnfbResp, this.rnfbRespInfo)
62
+    return readJSON(this.rnfbResp, this.rnfbRespInfo)
87
   }
63
   }
88
 
64
 
65
+  formData() {
66
+    log.verbose('to formData', this.rnfbResp, this.rnfbRespInfo)
67
+    return readFormData(this.rnfbResp, this.rnfbRespInfo)
68
+  }
69
+
70
+  blob() {
71
+    log.verbose('to blob', this.rnfbResp, this.rnfbRespInfo)
72
+    return readBlob(this.rnfbResp, this.rnfbRespInfo)
73
+  }
74
+}
75
+
76
+
77
+function readText(resp, info):Promise<string> {
78
+  switch (info.rnfbEncode) {
79
+    case 'base64':
80
+      return Promise.resolve(resp.text())
81
+      break
82
+    case 'path':
83
+      return resp.readFile('utf8').then((data) => {
84
+        data = unicode(data)
85
+        return Promise.resolve(data)
86
+      })
87
+      break
88
+    default:
89
+      return Promise.resolve(resp.text())
90
+      break
91
+  }
92
+}
93
+
94
+function readJSON(resp, info):Promise<object> {
95
+  switch (info.rnfbEncode) {
96
+    case 'base64':
97
+      return Promise.resolve(resp.json())
98
+    case 'path':
99
+      return resp.readFile('utf8').then((data) => {
100
+        return Promise.resolve(JSON.parse(data))
101
+      })
102
+    default:
103
+      return Promise.resolve(JSON.parse(resp.data))
104
+  }
89
 }
105
 }

+ 5
- 15
test/test-0.8.2.js Datei anzeigen

17
 window.fetch = new RNFetchBlob.polyfill.Fetch({
17
 window.fetch = new RNFetchBlob.polyfill.Fetch({
18
   auto : true,
18
   auto : true,
19
   binaryContentTypes : ['image/', 'video/', 'audio/']
19
   binaryContentTypes : ['image/', 'video/', 'audio/']
20
-}).provider.fetch
20
+}).build()
21
 
21
 
22
 const fs = RNFetchBlob.fs
22
 const fs = RNFetchBlob.fs
23
 const { Assert, Comparer, Info, prop } = RNTest
23
 const { Assert, Comparer, Info, prop } = RNTest
32
 
32
 
33
 let prefix = ((Platform.OS === 'android') ? 'file://' : '')
33
 let prefix = ((Platform.OS === 'android') ? 'file://' : '')
34
 
34
 
35
-describe('unicode file access', (report, done) => {
36
-  let path = dirs.DocumentDir + '/chinese.tmp'
37
-  fs.writeFile(path, '你好!', 'utf8')
38
-    .then(() => fs.readFile(path, 'utf8'))
39
-    .then((data) => {
40
-      console.log(data)
41
-      done()
42
-    })
43
-})
44
-
45
 describe('whatwg-fetch - GET should work correctly', (report, done) => {
35
 describe('whatwg-fetch - GET should work correctly', (report, done) => {
46
   console.log(fetch)
36
   console.log(fetch)
47
   fetch(`${TEST_SERVER_URL}/unicode`, {
37
   fetch(`${TEST_SERVER_URL}/unicode`, {
48
     method : 'GET'
38
     method : 'GET'
49
   })
39
   })
50
   .then((res) => {
40
   .then((res) => {
51
-    console.log('fetch resp',res)
52
-    return res.text()
41
+    return res.json()
53
   })
42
   })
54
-  .then((blob) => {
55
-    console.log(blob)
43
+  .then((data) => {
44
+    console.log(data)
45
+    report(<Assert key="data should correct" expect={'你好!'} actual={data.data}/>)
56
     done()
46
     done()
57
   })
47
   })
58
 })
48
 })