gofor

gofor is a (Gofor) factory interface for a lean fetch decorator that deep reverse merges default options.
It means the headers you put in for each request will take precedence, but will supplemented with the defaults.
It's fetch is a fetch Promise
The index is a factory, returning the wrapped fetch. It is recommended to use the factory method.
Install
npm i -S @fiverr/gofor
Use
Create an instance:
const goforFactory = require('@fiverr/gofor');
const defaultHeaders = new Headers();
defaultHeaders.append('X-Requested-With', 'XMLHttpRequest');
defaultHeaders.append('Content-Type', 'application/json; charset=utf-8');
defaultHeaders.append('Accept', 'application/json');
const gofor = goforFactory({
credentials: 'same-origin',
headers: defaultHeaders
});
gofor('https://www.website.com').then(...);
gofor('/page', {
headers: {
'X-Custom-Header': 'Custom-Value'
}
}).then(...);
Default header keys will be run over if matched by passed in header keys. Other keys will be merged. This is made by design.
Example
const gofor = goforFactory({
credentials: 'same-origin',
headers: new Headers({
'Content-Type': 'application/json; charset=utf-8',
'X-Custom-Header': 'Custom-Value'
})
});
gofor('/page', {
headers: new Headers({
'Content-Type': 'text/plain',
})
});
Final headers will be:
'Content-Type': 'text/plain',
'X-Custom-Header': 'Custom-Value'
Node Runtime
Gofor is designed for the browser, and depends on the fetch API.
In order to use gofor in node, you must have a polyfill for fetch].
const fetch = require('node-fetch');
const {Headers, Request, Response} = fetch;
Object.assign(global, {fetch, Headers, Request, Response});
Troubleshooting
.entries is not a function
Object.entries
is available in node version >= 7.5.0
,
