real-isomorphic-fetch
Isomorphic fetch() with cookie 🍪 and redirect ⏩ support for all environments
Installation
npm install real-isomorphic-fetch --save
Usage
const IsomorphicFetch = require('real-isomorphic-fetch');
const fetch = require('node-fetch');
const fetchInstance = new IsomorphicFetch(fetch)
fetchInstance('https://example.com/123')
.then(response => response.text())
.then(text => console.log(text))
.catch(err => console.error(err));
The problem
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.
Heavy inspiration also from isomorphic-fetch.
License
MIT © Jonny Burger