Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/JonnyBurger/real-isomorphic-fetch
Isomorphic fetch() with cookie 🍪 and redirect ⏩ support for all environments
https://github.com/JonnyBurger/real-isomorphic-fetch
Last synced: 1 day ago
JSON representation
Isomorphic fetch() with cookie 🍪 and redirect ⏩ support for all environments
- Host: GitHub
- URL: https://github.com/JonnyBurger/real-isomorphic-fetch
- Owner: JonnyBurger
- License: mit
- Archived: true
- Created: 2016-07-06T13:37:50.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-07-28T21:49:56.000Z (over 5 years ago)
- Last Synced: 2024-10-04T13:46:17.529Z (about 1 month ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# real-isomorphic-fetch [![Build Status](https://travis-ci.org/JonnyBurger/real-isomorphic-fetch.svg?branch=master)](https://travis-ci.org/JonnyBurger/real-isomorphic-fetch)
> Isomorphic fetch() with cookie 🍪 and redirect ⏩ support for all environments
## Installation
```
npm install real-isomorphic-fetch --save
```## Usage
```js
const IsomorphicFetch = require('real-isomorphic-fetch');
const fetch = require('node-fetch'); // could also be window.fetch in the browser or global.fetch in react-native
const fetchInstance = new IsomorphicFetch(fetch) // cookies are shared between every IsomorphicFetch instancefetchInstance('https://example.com/123') // Cookies and redirects are handled automatically
.then(response => response.text())
.then(text => console.log(text))
.catch(err => console.error(err));
```## The problem
* There are libraries out there that [polyfill fetch in the browser](https://github.com/github/fetch) and [bring it to node](https://github.com/bitinn/node-fetch).
* However, [node-fetch](https://github.com/bitinn/node-fetch) library doesn't handle receiving cookies...
* ... so you can use [fetch-cookie](https://github.com/valeriangalliat/fetch-cookie) to use cookies with fetch() ...
* ... but then you have to intercept redirects manually because [fetch-cookie](https://github.com/valeriangalliat/fetch-cookie) doesn't handle them...```js
const fetchWithCookie = require('fetch-cookie')(require('node-fetch'));
fetchWithCookie('https://example.com/123', {
redirect: 'manual'
})
.then(response => response.headers.get('Location'))
.then(location => fetchWithCookie(location, {redirect: 'manual'}))
```* ... which then doesn't work in the browser anymore because you can't intercept redirects (and because it uses native dependencies)!
## The solution
This library handles cookies and redirects together and normalizes the behaviour of `node-fetch` to match the one of the browser, so you can use the same syntax in both environments.
## Credits
This is a rewrite of [fetch-cookie](https://github.com/valeriangalliat/fetch-cookie).
Heavy inspiration also from [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch).## License
MIT © [Jonny Burger](http://jonny.io)