Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit 4d58919

Browse files
committed
code refactor #70
1 parent 1e1a6b0 commit 4d58919

File tree

3 files changed

+79
-73
lines changed

3 files changed

+79
-73
lines changed

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ class FetchBlobResponse {
261261
* @return {object} Parsed javascript object.
262262
*/
263263
this.json = ():any => {
264-
return JSON.parse(base64.decode(this.data))
264+
return JSON.parse(decodeURIComponent(base64.decode(this.data)))
265265
}
266266
/**
267267
* Return BASE64 string directly.

src/polyfill/Fetch.js

Lines changed: 73 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,83 +7,99 @@ const log = new Log('FetchPolyfill')
77

88
log.level(3)
99

10-
1110
export default class Fetch {
1211

13-
provider:RNFetchBlobFetch;
14-
1512
constructor(config:RNFetchBlobConfig) {
16-
this.provider = new RNFetchBlobFetch(config)
13+
Object.assign(this, new RNFetchBlobFetchPolyfill(config))
1714
}
1815

1916
}
2017

21-
class RNFetchBlobFetch {
18+
class RNFetchBlobFetchPolyfill {
2219

2320
constructor(config:RNFetchBlobConfig) {
24-
this._fetch = (url, options) => {
25-
let bodyUsed = false
21+
this.build = () => (url, options) => {
2622
options.headers = options.headers || {}
2723
options['Content-Type'] = options.headers['Content-Type'] || options.headers['content-type']
2824
options['content-type'] = options.headers['Content-Type'] || options.headers['content-type']
2925
return RNFetchBlob.config(config)
3026
.fetch(options.method, url, options.headers, options.body)
3127
.then((resp) => {
3228
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))
8130
})
8231
}
8332
}
8433

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)
8763
}
8864

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+
}
89105
}

test/test-0.8.2.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ window.Blob = Blob
1717
window.fetch = new RNFetchBlob.polyfill.Fetch({
1818
auto : true,
1919
binaryContentTypes : ['image/', 'video/', 'audio/']
20-
}).provider.fetch
20+
}).build()
2121

2222
const fs = RNFetchBlob.fs
2323
const { Assert, Comparer, Info, prop } = RNTest
@@ -32,27 +32,17 @@ const dirs = RNFetchBlob.fs.dirs
3232

3333
let prefix = ((Platform.OS === 'android') ? 'file://' : '')
3434

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-
4535
describe('whatwg-fetch - GET should work correctly', (report, done) => {
4636
console.log(fetch)
4737
fetch(`${TEST_SERVER_URL}/unicode`, {
4838
method : 'GET'
4939
})
5040
.then((res) => {
51-
console.log('fetch resp',res)
52-
return res.text()
41+
return res.json()
5342
})
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}/>)
5646
done()
5747
})
5848
})

0 commit comments

Comments
 (0)