Skip to content

Universal WHATWG Fetch API for Node, Browsers and React Native

License

Notifications You must be signed in to change notification settings

rubythonode/cross-fetch

 
 

Repository files navigation

cross-fetch Build Status NPM Version License: MIT

Universal WHATWG Fetch API for Node, Browsers 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 import: no configuration required
  • WHATWG compliant: it should be working the same way wherever your code runs
  • Updated: lastest version of whatwg-fetch and node-fetch used
  • Small: 2.5kb gzipped

Table of Contents


Install

npm install --save cross-fetch

As a ponyfill:

// Using ES6 modules
import { fetch } from 'cross-fetch';

// Using CommonJS modules
const { fetch } = require('cross-fetch');

As a polyfill:

// Using ES6 modules
import 'cross-fetch/polyfill';

// Using CommonJS modules
require('cross-fetch/polyfill');

The CDN build is also available on unpkg:

<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

As a ponyfill:

const { fetch } = require('cross-fetch');

fetch('//api.github.com/users/lquixada')
  .then(res => {
    if (res.status >= 400) {
      throw new Error("Bad response from server");
    }
    return res.json();
  })
  .then(user => {
    console.log(user);
  });

As a polyfill:

require('cross-fetch');

fetch('//api.github.com/users/lquixada')
  .then(res => {
    if (res.status >= 400) {
      throw new Error("Bad response from server");
    }
    return res.json();
  })
  .then(user => {
    console.log(user);
  });

⚠️ Warning: If you're in an environment that doesn't support Promises such as Internet Explorer, you must install an ES6 Promise compatible polyfill. es6-promise is suggested.

Demo & API

You can find a comprehensive doc at Github's fetch page. Use the JSFiddle playground ➡️ for tinkering.

FAQ

Yet another fetch library?

I did a lot of research in order to find a fetch library that could be simple, cross-platorm and provide polyfill as an option. There's a plethora of libs out there but none could match those requirements.

Why not isomorphic-fetch?

My preferred library used to be isomorphic-fetch but it has this bug that prevents it from running in a react native environment. It seems it will never be fixed since the author hasn't been commiting for more than a year. That means dependencies are outdated as well.

Why polyfill might not be a good ideia?

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 ou React Native, it delivers you the github's whatwg-fetch. The same strategy applies whether you're using polyfill or ponyfill.

Supported environments

  • Node 4+
  • React-Native
  • Browsers
    • Chrome
    • Firefox
    • Safari 6.1+
    • Internet Explorer 10+

Thanks

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

License

cross-fetch is licenced under the MIT licence © Leonardo Quixadá

Author

@lquixada
@lquixada

About

Universal WHATWG Fetch API for Node, Browsers and React Native

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%