Skip to content

Commit

Permalink
[BUGFIX BETA RELEASE] make fetch function JIT (#6430)
Browse files Browse the repository at this point in the history
* [BUGFIX BETA RELEASE] make fetch function JIT

* ensure pretender can hijack the fetch fn
  • Loading branch information
runspired committed Sep 11, 2019
1 parent 004c2a1 commit f98e75f
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions packages/adapter/addon/-private/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@ import require, { has } from 'require';

type FetchFunction = (input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>;

let _fetch: (() => FetchFunction) | null = null;
let _fetch: () => FetchFunction | null = null;

if (has('fetch')) {
// use `fetch` module by default, this is commonly provided by ember-fetch
let foundFetch = require('fetch').default;
_fetch = () => foundFetch;
} else if (typeof fetch === 'function') {
// fallback to using global fetch
_fetch = () => fetch;
} else {
throw new Error(
'cannot find the `fetch` module or the `fetch` global. Did you mean to install the `ember-fetch` addon?'
);
}
export default function getFetchFunction(): FetchFunction {
if (_fetch !== null) {
return _fetch();
}

export default _fetch;
if (has('fetch')) {
// use `fetch` module by default, this is commonly provided by ember-fetch
let fetchFn = require('fetch').default;
_fetch = () => fetchFn;
} else if (typeof fetch === 'function') {
// fallback to using global fetch
_fetch = () => fetch;
} else {
throw new Error(
'cannot find the `fetch` module or the `fetch` global. Did you mean to install the `ember-fetch` addon?'
);
}
return _fetch();
}

0 comments on commit f98e75f

Please sign in to comment.