-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathfetch.js
38 lines (34 loc) · 1.29 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
/* eslint-disable global-require, no-nested-ternary, no-undef */
function getGlobal() {
return typeof self !== 'undefined'
? self
: typeof window !== 'undefined'
? window
: typeof global !== 'undefined' ? global : null;
}
function hasAll(keys, obj) {
return keys.every(key => key in obj);
}
function isNode() {
return (
typeof process !== 'undefined' &&
typeof process.release !== 'undefined' &&
process.release.name === 'node'
);
}
const foundGlobal = getGlobal();
const fetchRelatedKeys = ['fetch', 'Headers', 'Request', 'Response'];
// See VET-634 internally. The idea here is to use the globally available
// (native) `fetch` when it is available. This allows for better response
// streaming and access to response headers when a response starts rather than
// when it ends.
// TODO: Remove the static dependency on 'fetch-ponyfill', given that many
// in-browser cases will now have no need for it. The current solution here is
// only a temporary, transitional way of using native `fetch` in such cases.
module.exports =
!isNode() && foundGlobal && hasAll(fetchRelatedKeys, foundGlobal)
? fetchRelatedKeys.reduce((fetchContainer, key) => {
fetchContainer[key] = foundGlobal[key];
return fetchContainer;
}, {})
: require('fetch-ponyfill')();