cross-fetch

用于 Node、浏览器和 React Native 的通用 WHATWG Fetch API。「Universal WHATWG Fetch API for Node, Browsers and React Native.」

Github星跟踪图

cross-fetch
NPM Version
Downloads Per Week
License: MIT
dependencies Status
Build Status
codecov

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 interface: no instantiation, no configuration and no extra dependency
  • WHATWG compliant: it works the same way wherever your code runs
  • Updated: latest version of whatwg-fetch and node-fetch used

Table of Contents


Install

npm install --save cross-fetch

As a ponyfill:

// Using ES6 modules with Babel or TypeScript
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

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);
  }
})();

⚠️ 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. If you want to play with cross-fetch, these resources can be useful:

Tip: Run theses resources 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 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 committing for more than a year. That means dependencies are outdated as well.

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?

Supported environments

  • Node 6+
  • React-Native

Build Status

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

主要指标

概览
名称与所有者lquixada/cross-fetch
主编程语言JavaScript
编程语言JavaScript (语言数: 5)
平台
许可证MIT License
所有者活动
创建于2017-09-02 18:36:08
推送于2025-04-15 12:40:26
最后一次提交
发布数59
最新版本名称v4.1.0 (发布于 2024-12-21 10:01:15)
第一版名称v0.0.1 (发布于 )
用户参与
星数1.7k
关注者数12
派生数105
提交数689
已启用问题?
问题数113
打开的问题数21
拉请求数43
打开的拉请求数4
关闭的拉请求数45
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?