|
@@ -6,8 +6,8 @@ import Blob from './Blob'
|
6
|
6
|
|
7
|
7
|
const log = new Log('FetchPolyfill')
|
8
|
8
|
|
9
|
|
-// log.level(3)
|
10
|
9
|
log.disable()
|
|
10
|
+// log.level(3)
|
11
|
11
|
|
12
|
12
|
export default class Fetch {
|
13
|
13
|
|
|
@@ -23,27 +23,34 @@ class RNFetchBlobFetchPolyfill {
|
23
|
23
|
this.build = () => (url, options = {}) => {
|
24
|
24
|
|
25
|
25
|
let body = options.body
|
26
|
|
- let promise = null
|
|
26
|
+ let promise = Promise.resolve()
|
27
|
27
|
let blobCache = null
|
28
|
28
|
|
29
|
29
|
options.headers = options.headers || {}
|
30
|
|
- options['Content-Type'] = options.headers['Content-Type'] || options.headers['content-type']
|
31
|
|
- options['content-type'] = options.headers['Content-Type'] || options.headers['content-type']
|
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())
|
39
|
|
- })
|
|
30
|
+ let ctype = options['Content-Type'] || options['content-type']
|
|
31
|
+ let ctypeH = options.headers['Content-Type'] || options.headers['content-type']
|
|
32
|
+ options.headers['Content-Type'] = ctype || ctypeH
|
|
33
|
+ options.headers['content-type'] = ctype || ctypeH
|
|
34
|
+ options.method = options.method || 'GET'
|
|
35
|
+
|
|
36
|
+ if(body) {
|
|
37
|
+ // When the request body is an instance of FormData, create a Blob cache
|
|
38
|
+ // to upload the body.
|
|
39
|
+ if(body instanceof FormData) {
|
|
40
|
+ log.verbose('convert FormData to blob body')
|
|
41
|
+ promise = Blob.build(body).then((b) => {
|
|
42
|
+ blobCache = b
|
|
43
|
+ options.headers['Content-Type'] = 'multipart/form-data;boundary=' + b.multipartBoundary
|
|
44
|
+ return Promise.resolve(RNFetchBlob.wrap(b._ref))
|
|
45
|
+ })
|
|
46
|
+ }
|
|
47
|
+ // When request body is a Blob, use file URI of the Blob as request body.
|
|
48
|
+ else if (body.isRNFetchBlobPolyfill)
|
|
49
|
+ promise = Promise.resolve(RNFetchBlob.wrap(body.blobPath))
|
|
50
|
+ // send it as-is, leave the native module decide how to send the body.
|
|
51
|
+ else
|
|
52
|
+ promise = Promise.resolve(body)
|
40
|
53
|
}
|
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
|
54
|
|
48
|
55
|
// task is a progress reportable and cancellable Promise, however,
|
49
|
56
|
// task.then is not, so we have to extend task.then with progress and
|
|
@@ -51,7 +58,7 @@ class RNFetchBlobFetchPolyfill {
|
51
|
58
|
let task = promise
|
52
|
59
|
.then((body) => {
|
53
|
60
|
return RNFetchBlob.config(config)
|
54
|
|
- .fetch(options.method, url, options.headers, options.body)
|
|
61
|
+ .fetch(options.method, url, options.headers, body)
|
55
|
62
|
})
|
56
|
63
|
|
57
|
64
|
let statefulPromise = task.then((resp) => {
|
|
@@ -59,7 +66,6 @@ class RNFetchBlobFetchPolyfill {
|
59
|
66
|
// release blob cache created when sending request
|
60
|
67
|
if(blobCache !== null && blobCache instanceof Blob)
|
61
|
68
|
blobCache.close()
|
62
|
|
- let info = resp.info()
|
63
|
69
|
return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
|
64
|
70
|
})
|
65
|
71
|
|
|
@@ -128,7 +134,6 @@ function readText(resp, info):Promise<string> {
|
128
|
134
|
break
|
129
|
135
|
case 'path':
|
130
|
136
|
return resp.readFile('utf8').then((data) => {
|
131
|
|
- data = unicode(data)
|
132
|
137
|
return Promise.resolve(data)
|
133
|
138
|
})
|
134
|
139
|
break
|