1 | isomorphic-fetch [](https://travis-ci.org/matthew-andrews/isomorphic-fetch)
|
2 | ================
|
3 |
|
4 | Fetch for node and Browserify. Built on top of [GitHub's WHATWG Fetch polyfill](https://github.com/github/fetch).
|
5 |
|
6 | ## Warnings
|
7 |
|
8 | - This adds `fetch` as a global so that its API is consistent between client and server.
|
9 |
|
10 | For [ease-of-maintenance and backward-compatibility reasons][why polyfill], this library will always be a polyfill. As a "safe" alternative, which does not modify the global, consider [fetch-ponyfill][].
|
11 |
|
12 | [why polyfill]: https://github.com/matthew-andrews/isomorphic-fetch/issues/31#issuecomment-149668361
|
13 | [fetch-ponyfill]: https://github.com/qubyte/fetch-ponyfill
|
14 |
|
15 | ## Why Use Isomorphic Fetch
|
16 |
|
17 | The Fetch API is currently [not implemented consistently](http://caniuse.com/#search=fetch) across browsers. This module will enable you to use `fetch` in your Node code in a cross-browser compliant fashion. The Fetch API is part of the Web platform API defined by the standards bodies WHATWG and W3C.
|
18 |
|
19 | ## Installation
|
20 |
|
21 | ### NPM
|
22 |
|
23 | ```sh
|
24 | npm install --save isomorphic-fetch
|
25 | ```
|
26 |
|
27 | ### Bower
|
28 |
|
29 | ```sh
|
30 | bower install --save isomorphic-fetch
|
31 | ```
|
32 |
|
33 | ## Usage
|
34 |
|
35 | ```js
|
36 | require('isomorphic-fetch');
|
37 |
|
38 | fetch('//offline-news-api.herokuapp.com/stories')
|
39 | .then(function(response) {
|
40 | if (response.status >= 400) {
|
41 | throw new Error("Bad response from server");
|
42 | }
|
43 | return response.json();
|
44 | })
|
45 | .then(function(stories) {
|
46 | console.log(stories);
|
47 | });
|
48 | ```
|
49 |
|
50 | ## License
|
51 |
|
52 | All open source code released by FT Labs is licenced under the MIT licence. Based on [the fine work by](https://github.com/github/fetch/pull/31) **[jxck](https://github.com/Jxck)**.
|
53 |
|
54 | ## Alternatives
|
55 |
|
56 | - [cross-fetch](https://github.com/lquixada/cross-fetch#why-not-isomorphic-fetch)
|
57 | - Using [node-fetch](https://github.com/node-fetch/node-fetch) and the [Fetch polyfill](https://github.com/github/fetch) directly (or from [polyfill.io](https://polyfill.io), or relying on [the browser's implementation of the Fetch API](https://caniuse.com/fetch)).
|