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
Changes from 3 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
87 changes: 52 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,63 @@
/*! 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 => {
/* istanbul ignore next */
if (typeof self !== 'undefined' && self && property in self) {
return self[property];
}

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

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

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

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 = {};

for (const source of sources) {
if (Array.isArray(source)) {
if (!(Array.isArray(returnValue))) {
if (!(Array.isArray(returnValue))) {
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
returnValue = [];
}

Expand Down Expand Up @@ -259,9 +276,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 +305,7 @@ class Ky {
response.clone()
);

if (modifiedResponse instanceof Response) {
if (modifiedResponse instanceof globals.Response) {
response = modifiedResponse;
}
}
Expand Down Expand Up @@ -387,19 +404,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