From 53e1a2acfeb7c7f41cb10cf1dc97531d8859ea39 Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Wed, 11 Sep 2019 13:05:18 -0700 Subject: [PATCH 1/2] [BUGFIX BETA RELEASE] make fetch function JIT --- .../adapter/addon/-private/utils/fetch.ts | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/packages/adapter/addon/-private/utils/fetch.ts b/packages/adapter/addon/-private/utils/fetch.ts index c59d4838f28..2d4e0d6bdb2 100644 --- a/packages/adapter/addon/-private/utils/fetch.ts +++ b/packages/adapter/addon/-private/utils/fetch.ts @@ -2,19 +2,23 @@ import require, { has } from 'require'; type FetchFunction = (input: RequestInfo, init?: RequestInit | undefined) => Promise; -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 + _fetch = require('fetch').default; + } 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; +} From cd521ec2d07b942b13cfbcc7cc5ec8903623c49b Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Wed, 11 Sep 2019 13:15:38 -0700 Subject: [PATCH 2/2] ensure pretender can hijack the fetch fn --- packages/adapter/addon/-private/utils/fetch.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/adapter/addon/-private/utils/fetch.ts b/packages/adapter/addon/-private/utils/fetch.ts index 2d4e0d6bdb2..dd292082a08 100644 --- a/packages/adapter/addon/-private/utils/fetch.ts +++ b/packages/adapter/addon/-private/utils/fetch.ts @@ -2,23 +2,24 @@ import require, { has } from 'require'; type FetchFunction = (input: RequestInfo, init?: RequestInit | undefined) => Promise; -let _fetch: FetchFunction | null = null; +let _fetch: () => FetchFunction | null = null; export default function getFetchFunction(): FetchFunction { if (_fetch !== null) { - return _fetch; + return _fetch(); } if (has('fetch')) { // use `fetch` module by default, this is commonly provided by ember-fetch - _fetch = require('fetch').default; + let fetchFn = require('fetch').default; + _fetch = () => fetchFn; } else if (typeof fetch === 'function') { // fallback to using global fetch - _fetch = 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; + return _fetch(); }