npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fetch-extended

v1.1.0

Published

Wrapper of fetch, adding few extra features

Downloads

6

Readme

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 [email protected]
// and
npm instlal [email protected]

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',
  },
}
*/