# @vercel/fetch-retry

> [![Build Status](https://github.com/vercel/fetch/workflows/CI/badge.svg)](https://github.com/vercel/fetch/actions?workflow=CI)

Latest version **5.1.3** (published 2022-02-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install @vercel/fetch-retry
pnpm add @vercel/fetch-retry
yarn add @vercel/fetch-retry
bun add @vercel/fetch-retry
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.1.3 |
| Published | 2022-02-28 |
| First published | 2020-10-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 12.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | swarnavasengupta, lydiahallie, ethan_arrowood, mitchellwright, kwonoj, kakadiadarpan, amanhimself_, endangeredmassa, nick.tracey, crowterli, gsoltis, schlez, epallerols, domysee, nwienert, arbw, gudmundur, marcgreenstock, stephdietz, vvo, falcoagustin, nabsul, bmealey, brethudson, maedahbatool, matt.straka, jasongullickson, f3d0r, gaspar09, jtaylor0196, pieparker, kelly, dferber90, healeycodes, broph123, codybrouwers, ebb-tide, gdborton, msimulcik, jeffreyarneson, jaredpalmer, kikobeats, domeccleston, nutlope, hungrybearstudio, codetaromiura, mattcleg, gkaragkiaouris, geovanisouza92, dglsparsons, lostinpatterns, pralhad-vercel, vercel-release-bot, williamli, southpolesteve, goncy, chibicode, ijjk, andybitz, iamevilrabbit, tootallnate, styfle, zeit-bot, quietshu, hankvercel, mglagola, javivelasco, igorklopov, dav-is, timer, lucleray, leerobinson, matheuss, nkzawa, anatrajkovska, timneutkens, elsigh, cl3arglass, samsisle, chriswdmr, rizbizkits, ernestd, ismaelrumzan, jhoch, mrmckeb, kuvos, creationix, rauchg, huozhi, aadityajshah, cmvnk, lfades, paulogdm, ktcarter, okbel, padmaia, msweeneydev, delba, catsaremlg, steventey, sokra, gsandhu, dbredvick, kdy1 |

## Links

- npm: https://www.npmjs.com/package/@vercel/fetch-retry
- Repository: https://github.com/vercel/fetch
- npm.io page: https://npm.io/package/@vercel/fetch-retry

## Dependencies (2)

- [debug](https://npm.io/package/debug.md) ^4.3.3
- [async-retry](https://npm.io/package/async-retry.md) ^1.3.3

## Recent versions

- 5.1.3 (latest) — 2022-02-28
- 5.0.3 — 2020-10-15
- 5.0.2 — 2020-10-15

## README

# @vercel/fetch-retry

[![Build Status](https://github.com/vercel/fetch/workflows/CI/badge.svg)](https://github.com/vercel/fetch/actions?workflow=CI)

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.

## How to use

`fetch-retry` is a drop-in replacement for `fetch`:

```js
const fetch = require('@vercel/fetch-retry')(require('node-fetch'))

module.exports = async () => {
  const res = await fetch('http://localhost:3000')
  console.log(res.status);
}
```

Make sure to `yarn add @vercel/fetch-retry` in your main package.

Note that you can pass [retry options](https://github.com/vercel/async-retry) to using `opts.retry`.
We also provide a `opts.onRetry` and `opts.retry.maxRetryAfter` options.

`opts.onRetry` is a customized version of `opts.retry.onRetry` and passes
not only the `error` object in each retry but also the current `opts` object.

`opts.retry.maxRetryAfter` is the max wait time according to the `Retry-After` header.
If it exceeds the option value, stop retrying and returns the error response. It defaults to `20`.

## Rationale

Some errors are very common in production (like the underlying `Socket`
yielding `ECONNRESET`), and can easily and instantly be remediated
by retrying.

The default behavior of `fetch-retry` is to attempt retries **10**, **60**
**360**, **2160** and **12960** milliseconds (a total of 5 retries) after
a *network error*, *429* or *5xx* error occur.

The idea is to provide a sensible default: most applications should
continue to perform correctly with a worst case scenario of a given
request having an additional 15550ms overhead.

On the other hand, most applications that use `fetch-retry` instead of
vanilla `fetch` should see lower rates of common errors and fewer 'glitches'
in production.

## Tests

To run rests, execute

```console
npm test
```

---
_Source: https://npm.io/package/@vercel/fetch-retry · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
