# @netly/node-fetch

> Custom node fetch with retry and timeout

Latest version **1.1.1** (published 2024-07-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install @netly/node-fetch
pnpm add @netly/node-fetch
yarn add @netly/node-fetch
bun add @netly/node-fetch
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2024-07-17 |
| First published | 2022-11-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=18 |
| Dependencies | 0 |
| Unpacked size | 13 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Dmytro Mysak |
| Maintainers | netly |
| Keywords | node-fetch, request, retry, timeout |

## Links

- npm: https://www.npmjs.com/package/@netly/node-fetch
- Repository: https://github.com/DmytroMysak/node-fetch
- Homepage: https://github.com/DmytroMysak/node-fetch#readme
- Issues: https://github.com/DmytroMysak/node-fetch/issues
- npm.io page: https://npm.io/package/@netly/node-fetch

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2024-07-17
- 1.1.0 — 2023-07-25
- 1.0.6 — 2023-02-10
- 1.0.5 — 2022-11-17
- 1.0.4 — 2022-11-16
- 1.0.3 — 2022-11-16
- 1.0.2 — 2022-11-16
- 1.0.1 — 2022-11-15
- 1.0.0 — 2022-11-15

## README

# node-fetch with native fetch + retry + timeout

Inspired by node-fetch-retry-timeout.

Tiny replacement for native **fetch** with additional benefits.

## Installation

`npm i @netly/node-fetch`

## Parameters

Same as in native fetch, but with small extension. Everything is **optional**.

| parameter                                     | type   | description                                | default                                                |
| --------------------------------------------- | ------ | ------------------------------------------ | ------------------------------------------------------ |
| [`retry`](#retry)                             | number | number of times to retry the request       | 3                                                      |
| [`timeout`](#timeout)                         | number | number of ms to wait before request cancel | 20_000                                                 |
| [`retryStrategy`](#retryStrategy)             | Function | exponential backoff strategy               | (times) => Math.min((2 \*\* times - 1) \* 1000, 20000) |
| [`retryOnHttpResponse`](#retryOnHttpResponse) | Function | can retry based on http response statuses  | (response) => response.status >= 500;                  |

### retry

```ts
retry?: number
```

### timeout

```ts
timeout?: number
```

### retryStrategy

Calculates how long to wait before next retry. `times` - the number of retry.
To disable retry strategy: `() => 0`

```ts
retryStrategy: (times: number): number => Math.min((2 ** times - 1) * 1000, 20000);
```

### retryOnHttpResponse

Check if response is desired. If no - throw error and send request one more time.

```ts
retryOnHttpResponse: (response: globalThis.Response) => response.status >= 500;
```

## Example

### For `cjs`

```js
const { default: fetch } = require('@netly/node-fetch');
// or 
const { fetch } = require('@netly/node-fetch');

const response = await fetch('https://www.google.com');
```

### For `esm`

```js
import { fetch } from '@netly/node-fetch';
// or
import fetch from '@netly/node-fetch';

const response = await fetch('https://www.google.com');
```
### For `ts`

```ts
import { fetch } from '@netly/node-fetch';
// or 
import fetch from '@netly/node-fetch';

const response = await fetch('https://www.google.com');
```

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