Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move globals to getters to allow them to be mocked #153

Merged
merged 7 commits into from
Sep 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 68 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,70 @@
/*! MIT License © Sindre Sorhus */

const getGlobal = property => {
/* istanbul ignore next */
if (typeof self !== 'undefined' && self && property in self) {
return self[property];
}
const globals = {};

/* istanbul ignore next */
if (typeof window !== 'undefined' && window && property in window) {
return window[property];
}
{
const getGlobal = property => {
let parent;

if (typeof global !== 'undefined' && global && property in global) {
return global[property];
}
/* istanbul ignore next */
if (typeof self !== 'undefined' && self && property in self) {
parent = self;
}

/* istanbul ignore next */
if (typeof globalThis !== 'undefined' && globalThis) {
return globalThis[property];
/* istanbul ignore next */
if (typeof window !== 'undefined' && window && property in window) {
parent = window;
}

if (typeof global !== 'undefined' && global && property in global) {
parent = global;
}

/* istanbul ignore next */
if (typeof globalThis !== 'undefined' && globalThis) {
parent = globalThis;
}

if (typeof parent === 'undefined') {
return;
}

const globalProperty = parent[property];

if (typeof globalProperty === 'function') {
return globalProperty.bind(parent);
}

return globalProperty;
};

const globalProperties = [
'document',
'Headers',
'Request',
'Response',
'ReadableStream',
'fetch',
'AbortController',
'FormData'
];

const props = {};
for (const property of globalProperties) {
props[property] = {
get() {
return getGlobal(property);
}
};
}
};

const document = getGlobal('document');
const Headers = getGlobal('Headers');
const Request = getGlobal('Request');
const Response = getGlobal('Response');
const ReadableStream = getGlobal('ReadableStream');
const fetch = getGlobal('fetch');
const AbortController = getGlobal('AbortController');
const FormData = getGlobal('FormData');
Object.defineProperties(globals, props);
}

const isObject = value => value !== null && typeof value === 'object';
const supportsAbortController = typeof AbortController === 'function';
const supportsStreams = typeof ReadableStream === 'function';
const supportsFormData = typeof FormData === 'function';
const supportsAbortController = typeof globals.AbortController === 'function';
const supportsStreams = typeof globals.ReadableStream === 'function';
const supportsFormData = typeof globals.FormData === 'function';

const deepMerge = (...sources) => {
let returnValue = {};
Expand Down Expand Up @@ -200,7 +231,7 @@ class Ky {
...otherOptions
};

if (input instanceof Request) {
if (input instanceof globals.Request) {
this._input = input;

// `ky` options have precedence over `Request` options
Expand All @@ -226,7 +257,7 @@ class Ky {
this._input = this._options.prefixUrl + this._input;

if (searchParams) {
const url = new URL(this._input, document && document.baseURI);
const url = new URL(this._input, globals.document && globals.document.baseURI);
if (typeof searchParams === 'string' || (URLSearchParams && searchParams instanceof URLSearchParams)) {
url.search = searchParams;
} else if (Object.values(searchParams).every(param => typeof param === 'number' || typeof param === 'string')) {
Expand All @@ -240,7 +271,7 @@ class Ky {
}

if (supportsAbortController) {
this.abortController = new AbortController();
this.abortController = new globals.AbortController();
if (this._options.signal) {
this._options.signal.addEventListener('abort', () => {
this.abortController.abort();
Expand All @@ -259,9 +290,9 @@ class Ky {
}, hooks);
this._throwHttpErrors = throwHttpErrors;

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

if (((supportsFormData && this._options.body instanceof FormData) || this._options.body instanceof URLSearchParams) && headers.has('content-type')) {
if (((supportsFormData && this._options.body instanceof globals.FormData) || this._options.body instanceof URLSearchParams) && headers.has('content-type')) {
throw new Error(`The \`content-type\` header cannot be used with a ${this._options.body.constructor.name} body. It will be set automatically.`);
}

Expand All @@ -288,7 +319,7 @@ class Ky {
response.clone()
);

if (modifiedResponse instanceof Response) {
if (modifiedResponse instanceof globals.Response) {
response = modifiedResponse;
}
}
Expand Down Expand Up @@ -387,19 +418,19 @@ class Ky {
}

if (this._timeout === false) {
return fetch(this._input, this._options);
return globals.fetch(this._input, this._options);
}

return timeout(fetch(this._input, this._options), this._timeout, this.abortController);
return timeout(globals.fetch(this._input, this._options), this._timeout, this.abortController);
}

/* istanbul ignore next */
_stream(response, onDownloadProgress) {
const totalBytes = Number(response.headers.get('content-length')) || 0;
let transferredBytes = 0;

return new Response(
new ReadableStream({
return new globals.Response(
new globals.ReadableStream({
start(controller) {
const reader = response.body.getReader();

Expand Down
1 change: 1 addition & 0 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ test('throws TimeoutError even though it does not support AbortController', with
}, server.url);
t.is(error, 'TimeoutError: Request timed out');

// A note from @szmarczak: await server.close() hangs on my machine
await server.close();
});

Expand Down