fetch-extended
Wrapper of fetch, adding few extra features:
- Simplifies work with searchParams, instead handcrafting url, just pass
options.query
and done - Timeout requests after 60s by default and allows to change this behaviour
- Allow to create fetch with default headers and options
Install
// To install es5 version of the package just
npm install fetch-extended
// To install es6/es2015 version of the package just
npm install fetch-extended@es6
// Each version > 1.0.0 is available as es5 and es6 version
npm instlal fetch-extended@1.0.0
// and
npm instlal fetch-extended@1.0.0-es6
Usage
Standard fetch
Features:
import fetch from 'fetch-extended';
fetch('https://www.google.com')
.then(response => handleResponse(response));
fetch('https://www.google.com', { method: 'PUT' })
.then(response => handleResponse(response));
For more parameters and configuration to go to Mozzila docs.
You can also use named export like this:
import { fetchx } from 'fetch-extended';
const response = await fetchx('https://www.google.com');
const postResponse = await fetchx('https://www.google.com', { method: 'PUT' });
Query Parameters
To simplify generating searchParams you can just pass query
object with options
.
import { fetchx } from 'fetch-extended';
const query = {
filter: 'cats',
sort: 'age',
};
await fetchx('https://www.google.com', { query })
Timeout
With timeout
key you can pass the number of milliseconds of timeout for the request. By default the timeout equals 60000 ms (60 s).
Setting timeout
as any not numerical value like undefined
or null
will turn off this feature and behaviour is the same as standard fetch
.
import fetch, { TimeoutError } from 'fetch-extended';
const timeout = 15000;
fetch('https://www.reject-after-15s.com', { timeout })
.then(response => handleResponse(response))
.catch(error => if(error instanceof TimeoutError) { console.log('Request timeoued out') });
queryParser
With queryParser
option you can construct URL search parameter sting however you like. By default its string is build with URLSearchParms
class. queryParser
function accepts single parameter which is options.query
from the call and returns string that represent search params (those after ?
character).
import fetch from 'fetch-extended';
function queryParser(query) {
const searchParams = new URLSearchParams();
Object.keys(query)
.forEach(key => {
if(Array.isArray(query[key])) {
query[key].forEach(value => searchParams.append(key, value));
return
}
searchParams.append(key, query[key])
});
return searchParams.toString();
}
const query = {
foo: ['uno', 'dos']
};
fetch('http://localhost', { queryParser, query})
Defaults
To overwrite or set your own default headers and options use getFetchx
.
import { getFetchx } from 'fetch-extended';
const defaultHeaders = {
'api-token': 'my-secret-api-token',
};
const defaultOptions = {
timeout: null,
method: 'POST',
headers: {
'api-token': 'secondary-token',
'sessions-id': 'session',
},
};
const fetchOne = getFetchx(defaultHeaders);
await fetchOne('http://google.com')
const fetchTwo = getFetchx({}, defaultOptions);
await fetchTwo('http://google.com')
const fetchThree = getFetchx(defaultHeaders, defaultOptions);
await fetchThree('http://google.com')
await fetchThree('http://google.com', { timeout: 10000, headers: { 'api-token': 'my-token' }})