Universal WHATWG Fetch API for Node, Browsers and React Native.
Installations
npm install cross-fetch
Developer
lquixada
Developer Guide
Module System
CommonJS, UMD
Min. Node Version
Typescript Support
Yes
Node Version
14.21.3
NPM Version
6.14.18
Statistics
1,671 Stars
666 Commits
105 Forks
13 Watching
5 Branches
19 Contributors
Updated on 27 Nov 2024
Languages
JavaScript (53%)
TypeScript (31.2%)
HTML (8.47%)
Makefile (3.74%)
Shell (3.59%)
Total Downloads
Cumulative downloads
Total Downloads
2,475,449,378
Last day
-1.8%
3,263,106
Compared to previous day
Last week
4.2%
17,395,993
Compared to previous week
Last month
10.1%
71,631,578
Compared to previous month
Last year
19.5%
805,031,219
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
30
cross-fetch
Universal WHATWG Fetch API for Node, Browsers, Workers and React Native. The scenario that cross-fetch really shines is when the same JavaScript codebase needs to run on different platforms.
- Platform agnostic: browsers, Node or React Native
- Optional polyfill: it's up to you if something is going to be added to the global object or not
- Simple interface: no instantiation, no configuration and no extra dependency
- WHATWG compliant: it works the same way wherever your code runs
- TypeScript support: better development experience with types.
- Worker support: works on different types of workers such as Service Workers and CloudFlare Workers
Table of Contents
Install
1npm install --save cross-fetch
As a ponyfill (imports locally):
1// Using ES6 modules with Babel or TypeScript 2import fetch from 'cross-fetch'; 3 4// Using CommonJS modules 5const fetch = require('cross-fetch');
As a polyfill (installs globally):
1// Using ES6 modules 2import 'cross-fetch/polyfill'; 3 4// Using CommonJS modules 5require('cross-fetch/polyfill');
The CDN build is also available on unpkg:
1<script src="//unpkg.com/cross-fetch/dist/cross-fetch.js"></script>
This adds the fetch function to the window object. Note that this is not UMD compatible.
Usage
With promises:
1import fetch from 'cross-fetch'; 2// Or just: import 'cross-fetch/polyfill'; 3 4fetch('//api.github.com/users/lquixada') 5 .then(res => { 6 if (res.status >= 400) { 7 throw new Error("Bad response from server"); 8 } 9 return res.json(); 10 }) 11 .then(user => { 12 console.log(user); 13 }) 14 .catch(err => { 15 console.error(err); 16 });
With async/await:
1import fetch from 'cross-fetch'; 2// Or just: import 'cross-fetch/polyfill'; 3 4(async () => { 5 try { 6 const res = await fetch('//api.github.com/users/lquixada'); 7 8 if (res.status >= 400) { 9 throw new Error("Bad response from server"); 10 } 11 12 const user = await res.json(); 13 14 console.log(user); 15 } catch (err) { 16 console.error(err); 17 } 18})();
Demo & API
You can find a comprehensive doc at Github's fetch page. If you want to play with cross-fetch, check our JSFiddle playground.
Tip: Run the fiddle on various browsers and with different settings (for instance: cross-domain requests, wrong urls or text requests). Don't forget to open the console in the test suite page and play around.
FAQ
Yet another fetch library?
I did a lot of research in order to find a fetch library that could be simple, cross-platform and provide polyfill as an option. There's a plethora of libs out there but none could match those requirements.
Why polyfill might not be a good idea?
In a word? Risk. If the spec changes in the future, it might be problematic to debug. Read more about it on sindresorhus's ponyfill page. It's up to you if you're fine with it or not.
How does cross-fetch work?
Just like isomorphic-fetch, it is just a proxy. If you're in node, it delivers you the node-fetch library, if you're in a browser or React Native, it delivers you the github's whatwg-fetch. The same strategy applies whether you're using polyfill or ponyfill.
Who's Using It?
The New York Times | Apollo GraphQL | Swagger | VulcanJS | graphql-request |
Thanks
Heavily inspired by the works of matthew-andrews. Kudos to him!
License
cross-fetch is licensed under the MIT license © Leonardo Quixadá
Author
@lquixada |
Stable Version
The latest stable version of the package.
Stable Version
4.0.0
MODERATE
2
6.1/10
Summary
Incorrect Authorization in cross-fetch
Affected Versions
< 2.2.6
Patched Versions
2.2.6
6.1/10
Summary
Incorrect Authorization in cross-fetch
Affected Versions
>= 3.0.0, < 3.1.5
Patched Versions
3.1.5
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
- Info: security policy file detected: SECURITY.md:1
- Info: Found linked content: SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: SECURITY.md:1
- Info: Found text in security policy: SECURITY.md:1
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 existing vulnerabilities detected
Reason
binaries present in source code
Details
- Warn: binary detected: examples/reactnative/android/gradle/wrapper/gradle-wrapper.jar:1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/branch.yml:1
- Warn: no topLevel permission defined: .github/workflows/checks.yml:1
- Warn: no topLevel permission defined: .github/workflows/pr.yml:1
- Warn: no topLevel permission defined: .github/workflows/release.yml:1
- Info: no jobLevel write permissions found
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/branch.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/branch.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/branch.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/branch.yml/v4.x?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/branch.yml:28: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/branch.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/branch.yml:42: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/branch.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/branch.yml:43: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/branch.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/checks.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/checks.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/checks.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/checks.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/checks.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/checks.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/checks.yml:31: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/checks.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/checks.yml:32: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/checks.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/checks.yml:37: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/checks.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/checks.yml:47: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/checks.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/checks.yml:48: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/checks.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/checks.yml:59: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/checks.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/checks.yml:60: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/checks.yml/v4.x?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/pr.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/pr.yml/v4.x?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/pr.yml:25: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/pr.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/pr.yml:31: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/pr.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/pr.yml:34: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/pr.yml/v4.x?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/release.yml:34: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/release.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:48: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/release.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:49: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/release.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:63: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/release.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:64: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/release.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:69: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/release.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/release.yml/v4.x?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/lquixada/cross-fetch/release.yml/v4.x?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/branch.yml:23
- Warn: npmCommand not pinned by hash: .github/workflows/branch.yml:48
- Warn: npmCommand not pinned by hash: .github/workflows/checks.yml:25
- Warn: npmCommand not pinned by hash: .github/workflows/checks.yml:41
- Warn: npmCommand not pinned by hash: .github/workflows/checks.yml:53
- Warn: npmCommand not pinned by hash: .github/workflows/checks.yml:65
- Warn: npmCommand not pinned by hash: .github/workflows/pr.yml:41
- Warn: npmCommand not pinned by hash: .github/workflows/release.yml:29
- Warn: npmCommand not pinned by hash: .github/workflows/release.yml:54
- Warn: npmCommand not pinned by hash: .github/workflows/release.yml:74
- Info: 0 out of 23 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 4 third-party GitHubAction dependencies pinned
- Info: 0 out of 10 npmCommand dependencies pinned
Score
4.4
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to cross-fetch
response-iterator
Creates an async iterator for a variety of inputs in the browser and node. Supports fetch, node-fetch, and cross-fetch
@fetch-impl/cross-fetch
User Configurable Fetch Implementation. cross-fetch adapter.
@based/fetch
CF worker compatible cross-fetch
retry-my-fetch
It's a decorator for `Fetch` API ([fetch](https://fetch.spec.whatwg.org/), [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch), [cross-fetch](https://github.com/lquixada/cross-fetch), etc.) which allows resending (retrying) your reques