HarshvardhanCn01/Voice-Assistant
0
1cross-fetch<br>2[](https://www.npmjs.com/package/cross-fetch)3[](https://www.npmjs.com/package/cross-fetch)4[](https://opensource.org/licenses/MIT)5[](https://github.com/lquixada/cross-fetch/actions/workflows/ci.yml)6[](https://codecov.io/gh/lquixada/cross-fetch)7================8 9Universal 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.10 11- **Platform agnostic**: browsers, Node or React Native12- **Optional polyfill**: it's up to you if something is going to be added to the global object or not13- **Simple interface**: no instantiation, no configuration and no extra dependency14- **WHATWG compliant**: it works the same way wherever your code runs15- **TypeScript support**: better development experience with types.16- **Worker support**: works on different types of workers such as Service Workers and CloudFlare Workers17 18 19* * *20 21## Table of Contents22 23- [Table of Contents](#table-of-contents)24- [Install](#install)25- [Usage](#usage)26- [Demo \& API](#demo--api)27- [FAQ](#faq)28 - [Yet another fetch library?](#yet-another-fetch-library)29 - [Why polyfill might not be a good idea?](#why-polyfill-might-not-be-a-good-idea)30 - [How does cross-fetch work?](#how-does-cross-fetch-work)31- [Who's Using It?](#whos-using-it)32- [Thanks](#thanks)33- [License](#license)34- [Author](#author)35 36* * *37 38## Install39 40```sh41npm install --save cross-fetch42```43 44As a [ponyfill](https://github.com/sindresorhus/ponyfill) (imports locally):45 46```javascript47// Using ES6 modules with Babel or TypeScript48import fetch from 'cross-fetch';49 50// Using CommonJS modules51const fetch = require('cross-fetch');52```53 54As a polyfill (installs globally):55 56```javascript57// Using ES6 modules58import 'cross-fetch/polyfill';59 60// Using CommonJS modules61require('cross-fetch/polyfill');62```63 64 65The CDN build is also available on unpkg:66 67```html68<script src="//unpkg.com/cross-fetch/dist/cross-fetch.js"></script>69```70 71This adds the fetch function to the window object. Note that this is not UMD compatible.72 73 74* * *75 76## Usage77 78With [promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise):79 80```javascript81import fetch from 'cross-fetch';82// Or just: import 'cross-fetch/polyfill';83 84fetch('//api.github.com/users/lquixada')85 .then(res => {86 if (res.status >= 400) {87 throw new Error("Bad response from server");88 }89 return res.json();90 })91 .then(user => {92 console.log(user);93 })94 .catch(err => {95 console.error(err);96 });97```98 99With [async/await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function):100 101```javascript102import fetch from 'cross-fetch';103// Or just: import 'cross-fetch/polyfill';104 105(async () => {106 try {107 const res = await fetch('//api.github.com/users/lquixada');108 109 if (res.status >= 400) {110 throw new Error("Bad response from server");111 }112 113 const user = await res.json();114 115 console.log(user);116 } catch (err) {117 console.error(err);118 }119})();120```121 122## Demo & API123 124You can find a comprehensive doc at [Github's fetch](https://github.github.io/fetch/) page. If you want to play with cross-fetch, check our [**JSFiddle playground**](https://jsfiddle.net/lquixada/3ypqgacp/).125 126> **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.127 128 129## FAQ130 131#### Yet another fetch library?132 133I 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.134 135#### Why polyfill might not be a good idea?136 137In a word? Risk. If the spec changes in the future, it might be problematic to debug. Read more about it on [sindresorhus's ponyfill](https://github.com/sindresorhus/ponyfill#how-are-ponyfills-better-than-polyfills) page. It's up to you if you're fine with it or not.138 139#### How does cross-fetch work?140 141Just like isomorphic-fetch, it is just a proxy. If you're in node, it delivers you the [node-fetch](https://github.com/bitinn/node-fetch/) library, if you're in a browser or React Native, it delivers you the github's [whatwg-fetch](https://github.com/github/fetch/). The same strategy applies whether you're using polyfill or ponyfill.142 143 144## Who's Using It?145 146|[](https://www.nytimes.com/)|[](https://github.com/apollographql/apollo-client/)|[](https://github.com/facebook/fbjs/)|[](https://swagger.io/)|[](http://vulcanjs.org)|[](https://github.com/prisma/graphql-request)|147|:---:|:---:|:---:|:---:|:---:|:---:|148|The New York Times|Apollo GraphQL|Facebook|Swagger|VulcanJS|graphql-request|149 150 151## Thanks152 153Heavily inspired by the works of [matthew-andrews](https://github.com/matthew-andrews). Kudos to him!154 155 156## License157 158cross-fetch is licensed under the [MIT license](https://github.com/lquixada/cross-fetch/blob/main/LICENSE) © [Leonardo Quixadá](https://twitter.com/lquixada/)159 160 161## Author162 163|[](https://github.com/lquixada)|164|:---:|165|[@lquixada](http://www.github.com/lquixada)|166 