|
@@ -7,22 +7,18 @@ const log = new Log('FetchPolyfill')
|
7
|
7
|
|
8
|
8
|
log.level(3)
|
9
|
9
|
|
10
|
|
-
|
11
|
10
|
export default class Fetch {
|
12
|
11
|
|
13
|
|
- provider:RNFetchBlobFetch;
|
14
|
|
-
|
15
|
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
|
20
|
constructor(config:RNFetchBlobConfig) {
|
24
|
|
- this._fetch = (url, options) => {
|
25
|
|
- let bodyUsed = false
|
|
21
|
+ this.build = () => (url, options) => {
|
26
|
22
|
options.headers = options.headers || {}
|
27
|
23
|
options['Content-Type'] = options.headers['Content-Type'] || options.headers['content-type']
|
28
|
24
|
options['content-type'] = options.headers['Content-Type'] || options.headers['content-type']
|
|
@@ -30,60 +26,80 @@ class RNFetchBlobFetch {
|
30
|
26
|
.fetch(options.method, url, options.headers, options.body)
|
31
|
27
|
.then((resp) => {
|
32
|
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
|
}
|