cross-fetch
Universal WHATWG Fetch API for Node, Browsers and React Native
Last updated 4 years ago by lquixada .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install cross-fetch 
SYNC missed versions from official npm registry.

cross-fetch
NPM Version Downloads Per Week License: MIT CI codecov

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

npm install --save cross-fetch

As a ponyfill (imports locally):

// Using ES6 modules with Babel or TypeScript
import fetch from 'cross-fetch';

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

As a polyfill (installs globally):

// 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

With promises:

import fetch from 'cross-fetch';
// Or just: import 'cross-fetch/polyfill';

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);
  })
  .catch(err => {
    console.error(err);
  });

With async/await:

import fetch from 'cross-fetch';
// Or just: import 'cross-fetch/polyfill';

(async () => {
  try {
    const res = await fetch('//api.github.com/users/lquixada');
    
    if (res.status >= 400) {
      throw new Error("Bad response from server");
    }
    
    const user = await res.json();
  
    console.log(user);
  } catch (err) {
    console.error(err);
  }
})();

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 Facebook Swagger VulcanJS graphql-request
The New York Times Apollo GraphQL Facebook 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
@lquixada

Current Tags

  • 4.0.0                                ...           latest (a year ago)
  • 2.2.6                                ...           latest-v2.x (3 years ago)
  • 3.1.8                                ...           latest-v3.x (a year ago)
  • 4.0.0                                ...           latest-v4.x (a year ago)

49 Versions

  • 4.0.0                                ...           a year ago
  • 4.0.0-alpha.13                                ...           a year ago
  • 3.1.8                                ...           a year ago
  • 4.0.0-alpha.10                                ...           a year ago
  • 3.1.7                                ...           a year ago
  • 3.1.7-test.0 [deprecated]           ...           2 years ago
  • 4.0.0-alpha.5                                ...           2 years ago
  • 4.0.0-alpha.4                                ...           2 years ago
  • 3.1.6                                ...           2 years ago
  • 2.2.6                                ...           3 years ago
  • 3.1.5                                ...           3 years ago
  • 3.2.0-alpha.2                                ...           3 years ago
  • 3.2.0-alpha.1                                ...           3 years ago
  • 3.2.0-alpha.0                                ...           3 years ago
  • 2.2.5                                ...           3 years ago
  • 2.2.4                                ...           3 years ago
  • 3.1.4                                ...           4 years ago
  • 3.1.4-alpha.0                                ...           4 years ago
  • 3.1.3                                ...           4 years ago
  • 3.1.3-alpha.6                                ...           4 years ago
  • 3.1.3-alpha.5                                ...           4 years ago
  • 3.1.3-alpha.4                                ...           4 years ago
  • 3.1.2                                ...           4 years ago
  • 3.1.1                                ...           4 years ago
  • 3.1.0                                ...           4 years ago
  • 3.0.6                                ...           4 years ago
  • 3.0.5                                ...           4 years ago
  • 3.0.4                                ...           6 years ago
  • 3.0.3                                ...           6 years ago
  • 3.0.2                                ...           6 years ago
  • 3.0.1                                ...           6 years ago
  • 3.0.0                                ...           6 years ago
  • 2.2.3                                ...           6 years ago
  • 2.2.2                                ...           6 years ago
  • 2.2.1                                ...           7 years ago
  • 2.2.0                                ...           7 years ago
  • 2.1.1                                ...           7 years ago
  • 2.1.0                                ...           7 years ago
  • 2.0.0                                ...           7 years ago
  • 1.1.1                                ...           7 years ago
  • 1.1.0                                ...           7 years ago
  • 1.0.0                                ...           7 years ago
  • 0.0.8                                ...           7 years ago
  • 0.0.7                                ...           7 years ago
  • 0.0.6                                ...           7 years ago
  • 0.0.5                                ...           7 years ago
  • 0.0.4                                ...           7 years ago
  • 0.0.3                                ...           7 years ago
  • 0.0.2                                ...           7 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 18
Last Day 0
Last Week 18
Last Month 0
Dependencies (1)
Dev Dependencies (18)

Copyright 2013 - present © cnpmjs.org | Home |