1.1.0 • Published 5 years ago

fetch-extended v1.1.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

npm version

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';

// default GET method
fetch('https://www.google.com')
  .then(response => handleResponse(response));

// change method to POST
fetch('https://www.google.com', { method: 'PUT' })
  .then(response => handleResponse(response));

// and more...

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' });
// and more...

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 })
// The command will call: `https://www.google.com?filter=cats&sort=age`

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})
// http://localhost?foo=uno&foo=dos'

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')
// equivalent of fetchx('http://google.com', { headers: defaultHeaders });

const fetchTwo = getFetchx({}, defaultOptions);
await fetchTwo('http://google.com')
// equivalent of fetchx('http://google.com', defaultOptions);

const fetchThree = getFetchx(defaultHeaders, defaultOptions);
await fetchThree('http://google.com')
/* equivalent of calling with options:
{
  timeout: null,
  method: 'POST',
  headers: {
    'api-token': 'my-secret-api-token',
    'sessions-id': 'session',
  },
}
*/

await fetchThree('http://google.com', { timeout: 10000, headers: { 'api-token': 'my-token' }})
/* equivalent of calling with options:
{
  timeout: 10000,
  method: 'POST',
  headers: {
    'api-token': 'my-token',
    'sessions-id': 'session',
  },
}
*/
1.1.0

5 years ago

1.0.2-es6

6 years ago

1.0.2

6 years ago

1.0.0

6 years ago

1.0.1

6 years ago

1.0.1-es6

6 years ago

1.0.0-es6

6 years ago

0.0.2

7 years ago

0.0.1

7 years ago