Gathering detailed insights and metrics for fetch-retry
Gathering detailed insights and metrics for fetch-retry
Gathering detailed insights and metrics for fetch-retry
Gathering detailed insights and metrics for fetch-retry
Extend any fetch library with retry functionality.
npm install fetch-retry
56.6
Supply Chain
99.5
Quality
77
Maintenance
100
Vulnerability
100
License
Permissive wrapping, fix global pollution and export types
Published on 17 Mar 2024
Type definition of the defaults parameter
Published on 18 May 2023
Fix type definition to support URL instance
Published on 03 May 2023
Adds example on how to use with Node.js fetch and fixes failing tests
Published on 26 Feb 2023
Security updates: minimist
Published on 01 Jul 2022
Bug fixes and security fixes
Published on 04 Mar 2022
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
301 Stars
192 Commits
56 Forks
4 Watching
1 Branches
17 Contributors
Updated on 31 Oct 2024
Minified
Minified + Gzipped
JavaScript (99.81%)
Shell (0.19%)
Cumulative downloads
Total Downloads
Last day
-8.8%
745,537
Compared to previous day
Last week
0.8%
4,175,187
Compared to previous week
Last month
0.4%
17,932,231
Compared to previous month
Last year
19.8%
241,668,967
Compared to previous year
Adds retry functionality to the Fetch API.
It wraps any fetch
API package (eg: isomorphic-fetch, cross-fetch, isomorphic-unfetch, or Node.js native's fetch implementation) and retries requests that fail due to network issues. It can also be configured to retry requests on specific HTTP status codes.
1npm install fetch-retry --save
fetch-retry
is used the same way as fetch
, but also accepts retries
, retryDelay
, and retryOn
on the options
object.
These properties are optional, and unless different defaults have been specified when requiring fetch-retry
, these will default to 3 retries, with a 1000ms retry delay, and to only retry on network errors.
1const originalFetch = require('isomorphic-fetch'); 2const fetch = require('fetch-retry')(originalFetch); 3 4// fetch-retry can also wrap Node.js's native fetch API implementation: 5const fetch = require('fetch-retry')(global.fetch);
1fetch(url, { 2 retries: 3, 3 retryDelay: 1000 4 }) 5 .then(function(response) { 6 return response.json(); 7 }) 8 .then(function(json) { 9 // do something with the result 10 console.log(json); 11 });
or passing your own defaults:
1const originalFetch = require('isomorphic-fetch'); 2const fetch = require('fetch-retry')(originalFetch, { 3 retries: 5, 4 retryDelay: 800 5 });
fetch-retry
uses promises and requires you to polyfill the Promise API in order to support Internet Explorer.
The default behavior of fetch-retry
is to wait a fixed amount of time between attempts, but it is also possible to customize this by passing a function as the retryDelay
option. The function is supplied three arguments: attempt
(starting at 0), error
(in case of a network error), and response
. It must return a number indicating the delay.
1fetch(url, { 2 retryDelay: function(attempt, error, response) { 3 return Math.pow(2, attempt) * 1000; // 1000, 2000, 4000 4 } 5 }).then(function(response) { 6 return response.json(); 7 }).then(function(json) { 8 // do something with the result 9 console.log(json); 10 });
The default behavior of fetch-retry
is to only retry requests on network related issues, but it is also possible to configure it to retry on specific HTTP status codes. This is done by using the retryOn
property, which expects an array of HTTP status codes.
1fetch(url, { 2 retryOn: [503] 3 }) 4 .then(function(response) { 5 return response.json(); 6 }) 7 .then(function(json) { 8 // do something with the result 9 console.log(json); 10 });
The retryOn
option may also be specified as a function, in which case it will be supplied three arguments: attempt
(starting at 0), error
(in case of a network error), and response
. Return a truthy value from this function in order to trigger a retry, any falsy value will result in the call to fetch either resolving (in case the last attempt resulted in a response), or rejecting (in case the last attempt resulted in an error).
1fetch(url, { 2 retryOn: function(attempt, error, response) { 3 // retry on any network error, or 4xx or 5xx status codes 4 if (error !== null || response.status >= 400) { 5 console.log(`retrying, attempt number ${attempt + 1}`); 6 return true; 7 } 8 }) 9 .then(function(response) { 10 return response.json(); 11 }).then(function(json) { 12 // do something with the result 13 console.log(json); 14 });
The retryOn
option may also be used with async and await for calling asyncronous functions:
1fetch(url, { 2 retryOn: async function(attempt, error, response) { 3 if (attempt > 3) return false; 4 5 if (error !== null) { 6 var json = await response.json(); 7 if (json.property !== undefined) { 8 return true; 9 } 10 } 11 }) 12 .then(function(response) { 13 return response.json(); 14 }).then(function(json) { 15 // do something with the result 16 console.log(json); 17 });
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 5/19 approved changesets -- score normalized to 2
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
14 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More@vercel/fetch-retry
[![Build Status](https://github.com/vercel/fetch/workflows/CI/badge.svg)](https://github.com/vercel/fetch/actions?workflow=CI)
node-fetch-retry
Retry library for node-fetch
@zeit/fetch-retry
A layer on top of `fetch` (via [node-fetch](https://www.npmjs.com/package/node-fetch)) with sensible defaults for retrying to prevent common errors.
fetch-retry-ts
Adds retry functionality to fetch()