1.3.0 ā€¢ Published 6 months ago

@cooperwfloyd/fancyfetch v1.3.0

Weekly downloads
-
License
ISC
Repository
github
Last release
6 months ago

šŸ’… 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.
1.3.0

6 months ago

1.2.0

11 months ago

1.1.6-typescript.9

11 months ago

1.1.6-typescript.6

11 months ago

1.1.6-typescript.5

11 months ago

1.1.6-typescript.4

11 months ago

1.1.6-typescript.3

11 months ago

1.0.5

1 year ago

1.0.4

1 year ago

1.1.4-0

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

0.24.4

1 year ago

0.24.3

1 year ago

0.24.2

1 year ago

0.24.1

1 year ago

0.24.0

1 year ago

0.23.1

1 year ago

0.23.0

1 year ago

0.22.0

1 year ago

0.21.0

1 year ago

0.20.0

1 year ago

0.19.0

1 year ago

0.18.0

1 year ago

0.16.0

1 year ago

0.15.0

1 year ago

0.14.0

1 year ago

0.13.15-beta

1 year ago

0.13.14-beta

1 year ago

0.13.13-beta

1 year ago

0.13.12-beta

1 year ago

0.13.11-beta

1 year ago

0.13.10-beta

1 year ago

0.13.9-beta

1 year ago

0.13.8-beta

1 year ago

0.13.7-beta

1 year ago

0.13.6-beta

1 year ago

0.13.5-beta

1 year ago

0.13.4-beta

1 year ago

0.13.3-beta

1 year ago

0.13.2-beta

1 year ago

0.13.1-beta

1 year ago

0.13.0-beta

1 year ago

0.12.8

1 year ago

0.12.7-beta

1 year ago

0.12.6

1 year ago

0.12.5

1 year ago

0.12.4

1 year ago

0.12.3

1 year ago

0.12.2

1 year ago

0.12.1

1 year ago

0.12.0

1 year ago

0.11.1

1 year ago

0.11.0

1 year ago

0.10.0

1 year ago

0.8.0

1 year ago

0.7.0

1 year ago

0.6.0

1 year ago

0.5.0

1 year ago

0.4.0

1 year ago

0.3.0

1 year ago

0.1.0

1 year ago

0.0.1

1 year ago

0.0.0

1 year ago