Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rednexie/node-fetch-patch
a nodejs fetch polyfill/patch, ensures the availability across environments and versions.
https://github.com/rednexie/node-fetch-patch
fetch fetch-api fetch-patch fetch-polyfill js node node-fetch node-fetch-polyfill node-js nodejs npm patch polyfill
Last synced: 17 days ago
JSON representation
a nodejs fetch polyfill/patch, ensures the availability across environments and versions.
- Host: GitHub
- URL: https://github.com/rednexie/node-fetch-patch
- Owner: Rednexie
- License: mit
- Created: 2023-11-13T15:36:50.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-02-02T23:10:20.000Z (9 months ago)
- Last Synced: 2024-02-03T01:26:32.549Z (9 months ago)
- Topics: fetch, fetch-api, fetch-patch, fetch-polyfill, js, node, node-fetch, node-fetch-polyfill, node-js, nodejs, npm, patch, polyfill
- Language: JavaScript
- Homepage:
- Size: 226 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# node-fetch-patch
a nodejs fetch polyfill/patch, ensures the availability across environments and versions.# How does it work?
import the main file (for v10.0 - 20.8.1)
```js
const fetch = require('./node-fetch-patch');
```
or the [other one](https://raw.githubusercontent.com/Rednexie/node-fetch-patch/main/node-fetch-patch-all.js) to use with nodejs versions below 10 (since async import is not available)
```js
const fetch = require('./node-fetch-patch-all');
```
- Firstly, the module checks if nodejs native fetch api (built-in) is supported.
- If not, it checks for a node-fetch installation(specifically below 2.6.1), and tries to do a CommonJS require.
- If it doesn't work, tries to import node-fetch with async import function. This works for the verions above 2.6.1.
- If any of those work, it installs node-fetch using node package manager, then restarts the script/console.# How do I know where 'fetch' was imported from?
node-fetch-patch will add a some properties to let you know this:
- environment variable: `process.env.PATCHTYPE`
- global variable: `global.patchType`
- along with a property: `fetch.patchType`For example, if the fetch function was imported from nodejs native fetch api:
```js
> console.log(fetch)
[AsyncFunction: fetch] { patchType: 0 }> console.log(global.patchType)
0> console.log(process.env.PATCHTYPE)
'0'
```From node-fetch versions below 2.6.1 using CommonJS require:
```js
> console.log(fetch)
[AsyncFunction: fetch] { patchType: 2 }> console.log(global.patchType)
2> console.log(process.env.PATCHTYPE)
'2'
```From node-fetch versions above 2.6.1 using async import (unavailable in `node-fetch-patch-all.js`):
```js
> console.log(fetch)
[AsyncFunction: fetch] { patchType: 3 }> console.log(global.patchType)
3> console.log(process.env.PATCHTYPE)
'3'
```
if no fetch function found (this means node-fetch-patch is installing it and will restart the main process or node console):
```js
> console.log(fetch)
{ patchType: undefined }> console.log(global.patchType)
undefined> console.log(process.env.PATCHTYPE)
undefined
```
Or if node-fetch-patch couldn't export the fetch function (mostly because of an error):
```js
> console.log(fetch)
{ patchType: null }> console.log(global.patchType)
null> console.log(process.env.PATCHTYPE)
'null'
```