Skip to content

Commit 23997ad

Browse files
committed
drop node-fetch and simplify codebase
1 parent be4f900 commit 23997ad

13 files changed

Lines changed: 23 additions & 160 deletions

README.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
# @exodus/fetch
22

3-
A small wrapper around global fetch and `node-fetch`:
4-
5-
1. In React Native, global `fetch` is used without importing `node-fetch`
6-
7-
2. In Browser, global `fetch` is used without importing `node-fetch`
8-
9-
3. Otherwise `node-fetch` is imported, but when `window.fetch` is present (e.g. in Electron
10-
`renderer` process), it is used.
11-
12-
4. Otherwise (e.g. in Electron `browser` process and Node.js), `node-fetch` is used.
3+
Using this as a wrapper for `node-fetch` is deprecated,
4+
since all active Node.js versions implement global `fetch`

create-fetchival.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function query(params) {
88
.join('&')}`
99
}
1010

11-
function createFetchival({ fetch }) {
11+
function createFetchival({ fetch = require('./fetch') } = {}) {
1212
async function _fetch(method, url, opts, data) {
1313
// Unlike fetchival, don't silently ignore and override
1414
if (opts.body) throw new Error('unexpected pre-set body option')

fetch.browser.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

fetch.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
11
'use strict'
22

3-
// This file is structured this way to allow webpack to optimize out
4-
// the require calls based on hardcoded process.type per bundle.
5-
// Do not refactor or deduplicate.
3+
// This is deprecated, just use global fetch instead
4+
// All supported Node.js versions have it, and the only fallback here was node-fetch
65

7-
// Try to use global browser APIs (e.g. if in Electron), otherwise require impls
8-
if (typeof process !== 'undefined' && process) {
9-
// Node.js or Electron with Node.js integration
10-
if (process.type === 'renderer' || process.type === 'worker') {
11-
// Electron renderer with Node.js integration
12-
module.exports = fetch
13-
} else {
14-
// Node.js or Electron browser process
15-
module.exports = typeof fetch === 'undefined' ? require('node-fetch') : fetch.bind(globalThis)
16-
}
17-
} else {
18-
// Browser or Electron without Node.js integration
19-
module.exports = fetch
20-
}
6+
module.exports = globalThis.fetch.bind(globalThis)

fetch.native.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

fetchival.browser.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

fetchival.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
const fetchival = require('./fetchival.browser')
2-
const fetch = require('./fetch')
1+
const createFetchival = require('./create-fetchival')
32

4-
if (!fetchival.fetch) fetchival.fetch = fetch
5-
6-
module.exports = fetchival
3+
module.exports = createFetchival()

index.browser.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict'
22

3-
// Don't need to require node-fetch here, global fetch is defined
4-
// Same for global WebSocket
3+
// NOTE: use targeted imports in new code, main import is deprecated
54

6-
const fetchival = require('./fetchival.browser.js')
7-
const fetch = require('./fetch.browser.js')
5+
// Don't need to require ws here, global WebSocket is defined
6+
7+
const fetchival = require('./fetchival.js')
8+
const fetch = require('./fetch.js')
89

910
module.exports = { fetch, WebSocket, fetchival }

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
// NOTE: use targeted imports in new code
3+
// NOTE: use targeted imports in new code, main import is deprecated
44

55
const fetch = require('./fetch')
66
const WebSocket = require('./websocket')

package-lock.json

Lines changed: 0 additions & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)