This repository was archived by the owner on Mar 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathFetch.js
213 lines (188 loc) · 6.19 KB
/
Fetch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import RNFetchBlob from '../index.js'
import Log from '../utils/log.js'
import fs from '../fs'
import unicode from '../utils/unicode'
import Blob from './Blob'
const log = new Log('FetchPolyfill')
log.disable()
// log.level(3)
export default class Fetch {
constructor(config:RNFetchBlobConfig) {
Object.assign(this, new RNFetchBlobFetchPolyfill(config))
}
}
class RNFetchBlobFetchPolyfill {
constructor(config:RNFetchBlobConfig) {
this.build = () => (url, options = {}) => {
let body = options.body
let promise = Promise.resolve()
let blobCache = null
options.headers = options.headers || {}
let ctype = options['Content-Type'] || options['content-type']
let ctypeH = options.headers['Content-Type'] || options.headers['content-type']
options.headers['Content-Type'] = ctype || ctypeH
options.headers['content-type'] = ctype || ctypeH
options.method = options.method || 'GET'
if(body) {
// When the request body is an instance of FormData, create a Blob cache
// to upload the body.
if(body instanceof FormData) {
log.verbose('convert FormData to blob body')
promise = Blob.build(body).then((b) => {
blobCache = b
options.headers['Content-Type'] = 'multipart/form-data;boundary=' + b.multipartBoundary
return Promise.resolve(RNFetchBlob.wrap(b._ref))
})
}
// When request body is a Blob, use file URI of the Blob as request body.
else if (body.isRNFetchBlobPolyfill)
promise = Promise.resolve(RNFetchBlob.wrap(body.blobPath))
else if (typeof body !== 'object' && options.headers['Content-Type'] !== 'application/json')
promise = Promise.resolve(JSON.stringify(body))
else if (typeof body !== 'string')
promise = Promise.resolve(body.toString())
// send it as-is, leave the native module decide how to send the body.
else
promise = Promise.resolve(body)
}
// task is a progress reportable and cancellable Promise, however,
// task.then is not, so we have to extend task.then with progress and
// cancel function
let progressHandler, uploadHandler, cancelHandler
let statefulPromise = promise
.then((body) => {
let task = RNFetchBlob.config(config)
.fetch(options.method, url, options.headers, body)
if(progressHandler)
task.progress(progressHandler)
if(uploadHandler)
task.uploadProgress(uploadHandler)
if(cancelHandler)
task.cancel()
return task.then((resp) => {
log.verbose('response', resp)
// release blob cache created when sending request
if(blobCache !== null && blobCache instanceof Blob)
blobCache.close()
return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
})
})
// extend task.then progress with report and cancelling functions
statefulPromise.progress = (fn) => {
progressHandler = fn
}
statefulPromise.uploadProgress = (fn) => {
uploadHandler = fn
}
statefulPromise.cancel = () => {
cancelHandler = true
if(task.cancel)
task.cancel()
}
return statefulPromise
}
}
}
class RNFetchBlobFetchRepsonse {
constructor(resp:FetchBlobResponse) {
let info = resp.info()
this.headers = info.headers
this.ok = info.status >= 200 && info.status <= 299,
this.status = info.status
this.type = 'basic'
this.bodyUsed = false
this.resp = resp
this.rnfbRespInfo = info
this.rnfbResp = resp
}
rawResp() {
return Promise.resolve(this.rnfbResp)
}
arrayBuffer(){
log.verbose('to arrayBuffer', this.rnfbRespInfo)
this.bodyUsed = true
return readArrayBuffer(this.rnfbResp, this.rnfbRespInfo)
}
text() {
log.verbose('to text', this.rnfbResp, this.rnfbRespInfo)
this.bodyUsed = true
return readText(this.rnfbResp, this.rnfbRespInfo)
}
json() {
log.verbose('to json', this.rnfbResp, this.rnfbRespInfo)
this.bodyUsed = true
return readJSON(this.rnfbResp, this.rnfbRespInfo)
}
blob() {
log.verbose('to blob', this.rnfbResp, this.rnfbRespInfo)
this.bodyUsed = true
return readBlob(this.rnfbResp, this.rnfbRespInfo)
}
}
/**
* Get response data as array.
* @param {FetchBlobResponse} resp Response data object from RNFB fetch call.
* @param {RNFetchBlobResponseInfo} info Response informations.
* @return {Promise<Array>}
*/
function readArrayBuffer(resp, info):Promise<Array> {
switch (info.rnfbEncode) {
case 'path':
return resp.readFile('ascii')
break
default:
let buffer = []
let str = resp.text()
for (let i in str) {
buffer[i] = str.charCodeAt(i);
}
return Promise.resolve(buffer)
break
}
}
/**
* Get response data as string.
* @param {FetchBlobResponse} resp Response data object from RNFB fetch call.
* @param {RNFetchBlobResponseInfo} info Response informations.
* @return {Promise<string>}
*/
function readText(resp, info):Promise<string> {
switch (info.rnfbEncode) {
case 'base64':
return Promise.resolve(resp.text())
break
case 'path':
return resp.text()
break
default:
return Promise.resolve(resp.text())
break
}
}
/**
* Get response data as RNFetchBlob Blob polyfill object.
* @param {FetchBlobResponse} resp Response data object from RNFB fetch call.
* @param {RNFetchBlobResponseInfo} info Response informations.
* @return {Promise<Blob>}
*/
function readBlob(resp, info):Promise<Blob> {
log.verbose('readBlob', resp, info)
return resp.blob()
}
/**
* Get response data as JSON object.
* @param {FetchBlobResponse} resp Response data object from RNFB fetch call.
* @param {RNFetchBlobResponseInfo} info Response informations.
* @return {Promise<object>}
*/
function readJSON(resp, info):Promise<object> {
log.verbose('readJSON', resp, info)
switch (info.rnfbEncode) {
case 'base64':
return Promise.resolve(resp.json())
case 'path':
return resp.json()
default:
return Promise.resolve(resp.json())
}
}