All Projects → lquixada → Cross Fetch

lquixada / Cross Fetch

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

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Cross Fetch

Ky Universal
Use Ky in both Node.js and browsers
Stars: ✭ 421 (-60.4%)
Mutual labels:  isomorphic, http-client, fetch
fetch-wrap
extend WHATWG fetch wrapping it with middlewares
Stars: ✭ 21 (-98.02%)
Mutual labels:  fetch, http-client, fetch-api
Node Fetch
A light-weight module that brings the Fetch API to Node.js
Stars: ✭ 7,176 (+575.07%)
Mutual labels:  http-client, fetch, fetch-api
Php Fetch
A simple, type-safe, zero dependency port of the javascript fetch WebApi for PHP
Stars: ✭ 95 (-91.06%)
Mutual labels:  http-client, fetch, fetch-api
Create Request
Apply interceptors to `fetch` and create a custom request function.
Stars: ✭ 34 (-96.8%)
Mutual labels:  http-client, fetch, fetch-api
fetchx
Beautiful way to fetch data in React
Stars: ✭ 71 (-93.32%)
Mutual labels:  fetch, http-client
re-frame-fetch-fx
js/fetch Effect Handler for re-frame
Stars: ✭ 24 (-97.74%)
Mutual labels:  fetch, fetch-api
http4s-dom
http4s, in a browser near you
Stars: ✭ 13 (-98.78%)
Mutual labels:  fetch, http-client
Gretchen
Making fetch happen in TypeScript.
Stars: ✭ 301 (-71.68%)
Mutual labels:  http-client, fetch
fitch.js
A lightweight Promise based HTTP client, using Fetch API.
Stars: ✭ 35 (-96.71%)
Mutual labels:  fetch, http-client
electron-request
Zero-dependency, Lightweight HTTP request client for Electron or Node.js
Stars: ✭ 45 (-95.77%)
Mutual labels:  fetch, http-client
Redux Requests
Declarative AJAX requests and automatic network state management for single-page applications
Stars: ✭ 330 (-68.96%)
Mutual labels:  http-client, fetch
axios-endpoints
Axios endpoints helps you to create a more concise endpoint mapping with axios.
Stars: ✭ 41 (-96.14%)
Mutual labels:  fetch, fetch-api
wobbuffetch
Reactive wrapper for Fetch API
Stars: ✭ 28 (-97.37%)
Mutual labels:  http-client, fetch-api
node-fetch-har
Generate HAR entries for requests made with node-fetch
Stars: ✭ 23 (-97.84%)
Mutual labels:  fetch, fetch-api
React Fetch Hook
React hook for conveniently use Fetch API
Stars: ✭ 285 (-73.19%)
Mutual labels:  fetch, fetch-api
Fetch Suspense
A React hook compatible with React 16.6's Suspense component.
Stars: ✭ 479 (-54.94%)
Mutual labels:  fetch, fetch-api
legible
the cleanest way to make http requests in js / node
Stars: ✭ 49 (-95.39%)
Mutual labels:  fetch, fetch-api
bestfetch
fetch ⭐️caching ⭐️deduplication
Stars: ✭ 44 (-95.86%)
Mutual labels:  fetch, fetch-api
Fetch Examples
A repository of Fetch examples. See https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API for the corresponding documentation.
Stars: ✭ 431 (-59.45%)
Mutual labels:  fetch, fetch-api

cross-fetch
NPM Version Downloads Per Week License: MIT CI 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
  • TypeScript support: better development experience with types.

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, 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 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 unlikely to be fixed since there haven't been any new commits to it since 2016. 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?

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].