Skip to content
Permalink
Browse files

Use `globalThis` polyfill

So it will work in environments like React Native.

Fixes #53
  • Loading branch information...
sindresorhus committed Nov 1, 2018
1 parent 8481f5b commit f107e7b92d90e4b0287846a49ba3f30d9b9ca648
Showing with 20 additions and 3 deletions.
  1. +20 −3 index.js
@@ -1,3 +1,20 @@
// Polyfill for `globalThis`
const _globalThis = (() => {
if (typeof self !== 'undefined') {
return self;
}

/* istanbul ignore next */
if (typeof window !== 'undefined') {
return window;
}

/* istanbul ignore next */
if (typeof global !== 'undefined') {
return global;
}
})();

const isObject = value => value !== null && typeof value === 'object';

const deepMerge = (...sources) => {
@@ -125,7 +142,7 @@ class Ky {
}, hooks);
this._throwHttpErrors = throwHttpErrors;

const headers = new self.Headers(this._options.headers || {});
const headers = new _globalThis.Headers(this._options.headers || {});

if (json) {
headers.set('content-type', 'application/json');
@@ -148,7 +165,7 @@ class Ky {
// eslint-disable-next-line no-await-in-loop
const modifiedResponse = await hook(response.clone());

if (modifiedResponse instanceof self.Response) {
if (modifiedResponse instanceof _globalThis.Response) {
response = modifiedResponse;
}
}
@@ -227,7 +244,7 @@ class Ky {
await hook(this._options);
}

return timeout(self.fetch(this._input, this._options), this._timeout);
return timeout(_globalThis.fetch(this._input, this._options), this._timeout);
}
}

0 comments on commit f107e7b

Please sign in to comment.
You can’t perform that action at this time.