-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Don't ever assign the internal `fetch` to `null`. Instead, throw immediately if fetch is not found. 2. Don't extract the global fetch into module state. Instead, export a function that looks up the global fetch each time. This allows libraries that mock `fetch` to do so after Ember Data has already been loaded.
- Loading branch information
1 parent
6e04daf
commit 939d4a3
Showing
2 changed files
with
18 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
import require, { has } from 'require'; | ||
|
||
type MaybeFetch = { | ||
(input: RequestInfo, init?: RequestInit | undefined): Promise<Response>; | ||
} | null; | ||
type FetchFunction = (input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>; | ||
|
||
let _fetch: MaybeFetch = null; | ||
let _fetch: (() => FetchFunction) | null = null; | ||
|
||
if (has('fetch')) { | ||
// use `fetch` module by default, this is commonly provided by ember-fetch | ||
_fetch = require('fetch').default; | ||
let foundFetch = require('fetch').default; | ||
_fetch = () => foundFetch; | ||
} 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?' | ||
); | ||
} | ||
|
||
export default _fetch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters