-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.js
103 lines (94 loc) · 3.02 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
define(['./io', './FauxXHR'], function (io, FauxXHR) {
'use strict';
// fetch() handler
// This is a browser-only module.
var isJson = /^application\/json\b/;
function fetchTransport (options, prep) {
var headers = new Headers(options.headers || {}),
req = {
method: options.method,
mode: typeof options.fetchMode == 'string' || io.fetch.defaultMode,
cache: typeof options.fetchCache == 'string' || io.fetch.defaultCache,
redirect: typeof options.fetchRedirect == 'string' || io.fetch.defaultRedirect,
referrer: typeof options.fetchReferrer == 'string' || io.fetch.defaultReferrer,
referrerPolicy: typeof options.fetchReferrerPolicy == 'string' || io.fetch.defaultReferrerPolicy,
credentials: typeof options.fetchCredentials == 'string' || (('withCredentials' in options) &&
(options.withCredentials ? 'include' : 'same-origin')) || io.fetch.defaultCredentials
}, response;
if (options.fetchIntegrity) req.integrity = options.fetchIntegrity;
req.body = io.processData({setRequestHeader: function (key, value) {
headers.append(key, value);
}}, options, prep.data);
if (typeof Document !== 'undefined' && typeof XMLSerializer !== 'undefined' && req.body instanceof Document) {
if (!headers.has('Content-Type')) {
headers.append('Content-Type', 'application/xml');
}
req.body = new XMLSerializer().serializeToString(req.body);
}
req.headers = headers;
return fetch(prep.url, req).catch(function (err) {
return Promise.reject(new io.FailedIO(null, options, err));
}).then(function (res) {
response = res;
return res.text();
}).then(function (body) {
return new io.Result(new FauxXHR({
status: response.status,
statusText: response.statusText,
headers: getAllResponseHeaders(response.headers, options.mime),
responseType: options.responseType,
responseText: body
}), options);
});
}
io.fetch = {
attach: attach,
detach: detach,
// defaults
defaultMode: 'cors',
defaultCache: 'default',
defaultRedirect: 'follow',
defaultReferrer: 'client',
defaultReferrerPolicy: 'no-referrer-when-downgrade',
defaultCredentials: 'same-origin'
};
return io;
var oldTransport;
function attach () {
if (io.defaultTransport !== fetchTransport) {
oldTransport = io.defaultTransport;
io.defaultTransport = fetchTransport;
return true;
}
return false;
}
function detach () {
if (oldTransport && io.defaultTransport === fetchTransport) {
io.defaultTransport = oldTransport;
oldTransport = null;
return true;
}
return false;
}
function getAllResponseHeaders (headers, mime) {
try {
var h = [];
if (mime) {
for (var pair of headers) {
if (pair[0].toLowerCase() !== 'content-type') {
h.push(pair[0] + ': ' + pair[1]);
}
}
h.push('Content-Type: ' + mime);
} else {
for (var pair of headers) {
h.push(pair[0] + ': ' + pair[1]);
}
}
return h.join('\n');
} catch (e) {
// suppress
}
return headers.has('Content-Type') ? 'Content-Type: ' + headers.get('Content-Type') : '';
}
});