Cross-environment (nodejs/web) fetch APi
This package exports window.fetch
object in a browser and node-fetch module in a Node.js environment. Which is helpful when you develop a package compatible with both environments and use fetch
for HTTP requests. Allows to avoid conditional imports.
Also exports AbortController
(abort-controller module for Node.js) for both environments.
Installation
Install with npm:
npm install @js-bits/fetch
Install with yarn:
yarn add @js-bits/fetch
Import where you need it:
import { fetch, AbortController } from '@js-bits/fetch';
or require for CommonJS:
const { fetch, AbortController } = require('@js-bits/fetch');
How to use
fetch('http://example.com/movies.json')
.then(response => response.json())
.then(data => console.log(data));
Notes
- The package versions are aligned with node-fetch versions.
- v2 of this package supports v2 of node-fetch.
- Minimal size (bytes) in a browser since Fetch API is natively supported. Does not include any polyfills, which means that Internet Explorer is not supported.