undici-retry

7.0.0 · active · verified Wed Apr 22

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

import { sendWithRetry, DEFAULT_RETRYABLE_STATUS_CODES } from 'undici-retry';
import type { RetryConfig, RequestParams} from 'undici-retry'
import { Client } from 'undici';
import type { Dispatcher } from 'undici';

const client = new Client('http://localhost:3000', {}); // Replace with your target URL

async function makeRetriableRequest() {
    const request: Dispatcher.RequestOptions = {
        method: 'GET',
        path: '/',
        bodyTimeout: 500,
        headersTimeout: 500,
    };

    const retryConfig: RetryConfig = {
        maxAttempts: 3,
        statusCodesToRetry: [429, 500, 502, 503, 504], // Customize retryable status codes
        retryOnTimeout: true,
        // delayResolver: (response, statusCodesToRetry) => { /* custom logic */ return 1000; }
    };

    const requestParams: RequestParams = {
        safeParseJson: true,
        blobBody: false,
        throwOnInternalError: false,
    };

    console.log('Attempting request with retry logic...');
    const result = await sendWithRetry(client, request, retryConfig, requestParams);

    if (result.error) {
        console.error('Request failed after all retries:');
        console.error(JSON.stringify({
            body: result.error.body, // The last error response body
            headers: result.error.headers,
            statusCode: result.error.statusCode,
            message: result.error.error?.message, // Error message if an internal error occurred
        }, null, 2));
    } else if (result.result) {
        console.log('Request successful:');
        console.log(JSON.stringify({
            body: result.result.body,
            headers: result.result.headers,
            statusCode: result.result.statusCode,
        }, null, 2));
    }

    await client.close(); // Important: Close the client when no longer needed
}

makeRetriableRequest();

view raw JSON →