cross-fetch

Universal WHATWG Fetch API for Node, Browsers and React Native.

README

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


Table of Contents
Install
Usage
Demo & API
FAQ
Yet another fetch library?
Why polyfill might not be a good idea?
How does cross-fetch work?

Who's Using It?
Thanks
License
Author

Install


  1. ``` shell
  2. npm install --save cross-fetch
  3. ```

As a ponyfill (imports locally):

  1. ``` js
  2. // Using ES6 modules with Babel or TypeScript
  3. import fetch from 'cross-fetch';

  4. // Using CommonJS modules
  5. const fetch = require('cross-fetch');
  6. ```

As a polyfill (installs globally):

  1. ``` js
  2. // Using ES6 modules
  3. import 'cross-fetch/polyfill';

  4. // Using CommonJS modules
  5. require('cross-fetch/polyfill');
  6. ```

The CDN build is also available on unpkg:

  1. ``` html
  2. <script src="//unpkg.com/cross-fetch/dist/cross-fetch.js"></script>
  3. ```

This adds the fetch function to the window object. Note that this is not UMD compatible.

Usage


With promises :

  1. ``` js
  2. import fetch from 'cross-fetch';
  3. // Or just: import 'cross-fetch/polyfill';

  4. fetch('//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.   });
  17. ```

With async/await :

  1. ``` js
  2. import fetch from 'cross-fetch';
  3. // Or just: import 'cross-fetch/polyfill';

  4. (async () => {
  5.   try {
  6.     const res = await fetch('//api.github.com/users/lquixada');

  7.     if (res.status >= 400) {
  8.       throw new Error("Bad response from server");
  9.     }

  10.     const user = await res.json();

  11.     console.log(user);
  12.   } catch (err) {
  13.     console.error(err);
  14.   }
  15. })();
  16. ```

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.

Thanks


Heavily inspired by the works of matthew-andrews . Kudos to him!

License


cross-fetch is licensed under the MIT license © Leonardo Quixadá