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

@cooperwfloyd/fancyfetch

v1.3.0

Published

A simple, lightweight and isomorphic extension of the Fetch API that allows for graceful error handling and response validation, automatic retries, and the ability to bring your own Fetch API package.

Downloads

873

Readme

💅 fancyfetch

fancyfetch is a simple, lightweight and isomorphic extension of the fetch API to allow for graceful error handling, response validation, automatic retries, and the ability to use any fetch API package.

🙂 Basic usage

Import using the default export from @cooperwfloyd/fancyfetch. fancyfetch can be used client-side in the browser or on the server just like the standard fetch API — fancyfetch will automatically locate and use Node's global fetch API or your browser's native fetch API.

import fancyfetch from '@cooperwfloyd/fancyfetch';

const data = await fancyfetch('https://www.example.com');

🥳 Fancy usage

The third argument is available for all of fancyfetch's extensions, including:

👇 The ability to use a custom fetch API instead of the global default

import fancyfetch from '@cooperwfloyd/fancyfetch';
import fetch from 'node-fetch';

const data = await fancyfetch(
  'https://www.example.com/a-large-server-side-request',
  {highWaterMark: 2048 * 2048},
  {fetch}
);

👇 The ability to gracefully auto-retry requests if they fail or if their responses don't return true in a custom validation callback

import fancyfetch from '@cooperwfloyd/fancyfetch';

const data = await fancyfetch(
  'https://www.example.com/json',
  {
    headers: {
      'Content-Type': 'application/json',
    },
  },
  {
    maxAttempts: 10,
    retryDelay: 1000,
    validateResponse: async (response) => {
      try {
        const json = await response?.json();
        return !!json?.data;
      } catch {
        return false;
      }
    },
    onRetrySuccess: () => console.log('Successful retry'),
    onRetryError: () => console.error('Failed retry'),
    onError: () => console.error('No successful attempts'),
  }
);

✏️ Reference

  • resource (required): fetch.resource
  • options: fetch.options
  • extras
    • fetch: function
      • The fetch API (ex. fetch, node-fetch, isomorphic-fetch) that requests should use (default, in order of specificity: fetch, global.fetch, window.fetch)
    • log: boolean
      • Dictates whether or not fancyfetch's console statements should be fired (default: true)
    • validateResponse: function
      • This callback function allows for checking the response to determine it's validity. It sends the response as an argument and should return a truthy or falsy value since fancyfetch will use a boolean to determine the response's validity.
    • maxAttempts: number
      • Specifies the maximum number of times that the request should be attempted (default: 1). The validateResponse callback should be used whenever maxAttempts is greater than one since fancyfetch will not know when to break out of the recursive retry loop without it.
    • retryDelay: number
      • Specifies the number of milliseconds that fancyfetch should wait before retrying a failed request.