undici-retry is a JavaScript/TypeScript library designed to integrate robust retry logic with the undici HTTP client. As of version 7.0.0, it provides mechanisms to automatically re-attempt failed HTTP requests based on configurable criteria such as status codes, timeouts, and custom delay resolvers. It is tightly coupled with `undici`, requiring `undici` version 7.0.0 or higher as a peer dependency, and is compatible with Node.js environments version 20 or newer. The library offers two primary functions: `sendWithRetry` for operations where the response body is consumed directly (e.g., JSON parsing), and `sendWithRetryReturnStream` for scenarios requiring stream-based body handling, which is crucial for large responses or custom stream processing. Its release cadence typically aligns with major `undici` releases, adapting to `undici`'s API changes and Node.js version support. Key differentiators include its tight integration with `undici`'s `Dispatcher` and `Client` types, and its `Either` pattern for error handling.
npm install undici-retryVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to perform an HTTP GET request with retry logic using `sendWithRetry`, configure retry parameters, and handle the `Either` result for both success and failure scenarios.
Always ensure your installed `undici` version matches the peer dependency requirement of `undici-retry` (e.g., `npm install undici@7`). Check the `package.json` for precise peer dependency ranges.
Upgrade your Node.js environment to version 20 or higher. Using `nvm` or a similar tool is recommended for managing Node.js versions.
Always ensure the returned `Readable` stream from `result.result.body` is fully consumed or explicitly `await result.result.body.dump()` if the content is not needed, to release the underlying connection.
If you prefer internal errors to be thrown as exceptions (e.g., for standard try-catch blocks), set `throwOnInternalError: true` in your `requestParams` or `streamParams`.
Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`) and use `import { sendWithRetry } from 'undici-retry';`. If strictly bound to CommonJS, you might need a dynamic `import()` or transpilation.Install the correct version of `undici` that matches the peer dependency declared by your `undici-retry` version. For `undici-retry@7.x`, run `npm install undici@7`.
Review all calls to `sendWithRetryReturnStream` and ensure that `result.result.body` is either fully consumed (e.g., `await stream.text()`) or explicitly dumped (`await stream.dump()`) after use.