cross-fetch vs isomorphic-fetch vs node-fetch
HTTPリクエストライブラリ
cross-fetchisomorphic-fetchnode-fetch類似パッケージ:

HTTPリクエストライブラリ

これらのライブラリは、Node.jsおよびブラウザ環境でのHTTPリクエストを簡素化するために設計されています。これにより、開発者はAPIとの通信を容易に行うことができ、コードの可読性と保守性を向上させることができます。各ライブラリは異なるユースケースや環境に特化しており、選択肢を提供します。

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
cross-fetch01,69893.3 kB271年前MIT
isomorphic-fetch06,924-576年前MIT
node-fetch08,862107 kB2413年前MIT

機能比較: cross-fetch vs isomorphic-fetch vs node-fetch

環境サポート

  • cross-fetch:

    cross-fetchは、ブラウザとNode.jsの両方で動作するため、クロスプラットフォームのアプリケーションに最適です。これにより、同じコードを使用して異なる環境でHTTPリクエストを行うことができます。

  • isomorphic-fetch:

    isomorphic-fetchもブラウザとNode.jsの両方をサポートしており、サーバーサイドレンダリングを行う際に特に便利です。これにより、クライアントとサーバーで同じAPI呼び出しを簡単に行うことができます。

  • node-fetch:

    node-fetchはNode.js専用であり、ブラウザ環境では動作しませんが、Node.jsでのHTTPリクエストを効率的に処理するために最適化されています。

APIの一貫性

  • cross-fetch:

    cross-fetchは、ブラウザのfetch APIと同じインターフェースを持っているため、開発者は異なる環境での使用において一貫したAPIを利用できます。

  • isomorphic-fetch:

    isomorphic-fetchもfetch APIに準拠しており、開発者は異なる環境での使用において同じAPIを利用できるため、コードの可読性が向上します。

  • node-fetch:

    node-fetchは、Node.jsのfetch APIの実装を提供し、Node.js環境での一貫したHTTPリクエストのインターフェースを確保します。

軽量性

  • cross-fetch:

    cross-fetchは、必要な機能を提供しつつ、軽量であるため、パフォーマンスに優れています。

  • isomorphic-fetch:

    isomorphic-fetchも軽量で、必要な機能を提供しつつ、サーバーサイドレンダリングに特化しています。

  • node-fetch:

    node-fetchは、Node.js専用に設計されており、軽量で効率的なHTTPリクエストを提供します。

エラーハンドリング

  • cross-fetch:

    cross-fetchは、fetch APIのエラーハンドリングをサポートしており、HTTPエラーを簡単に処理できます。

  • isomorphic-fetch:

    isomorphic-fetchも同様に、fetch APIのエラーハンドリングをサポートし、エラーを一貫して処理することができます。

  • node-fetch:

    node-fetchは、Node.js環境に特化したエラーハンドリング機能を提供し、HTTPリクエストの失敗を適切に処理できます。

使用の簡便さ

  • cross-fetch:

    cross-fetchは、fetch APIに基づいているため、使い慣れたAPIを利用することができ、学習コストが低いです。

  • isomorphic-fetch:

    isomorphic-fetchもfetch APIに基づいており、開発者は簡単に使い始めることができます。

  • node-fetch:

    node-fetchは、シンプルなAPIを提供しており、Node.js開発者にとって使いやすい選択肢です。

選び方: cross-fetch vs isomorphic-fetch vs node-fetch

  • cross-fetch:

    クロスプラットフォームでの使用を考慮する場合、特にNode.jsとブラウザの両方で動作する必要がある場合は、cross-fetchを選択してください。これは、標準のfetch APIをラップしており、ブラウザとNode.jsの両方で一貫したインターフェースを提供します。

  • isomorphic-fetch:

    サーバーサイドレンダリングを行うアプリケーションを構築している場合、isomorphic-fetchが適しています。このライブラリは、Node.jsとブラウザの両方で動作し、同じコードベースでHTTPリクエストを行うことができるため、開発の一貫性を保つことができます。

  • node-fetch:

    Node.js専用の軽量なHTTPリクエストライブラリを必要とする場合は、node-fetchを選択してください。このライブラリは、Node.js環境でのfetch APIの実装を提供し、シンプルで効率的なHTTPリクエストを可能にします。

cross-fetch のREADME

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 TimesApollo GraphQLFacebookSwaggerVulcanJSgraphql-request
The New York TimesApollo GraphQLFacebookSwaggerVulcanJSgraphql-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