UNPKG

undici-retry

Version:

Library for handling retry logic with undici HTTP client

31 lines (30 loc) 1.44 kB
import type { Dispatcher } from 'undici'; import type { IncomingHttpHeaders } from 'undici/types/header'; import { type InternalRequestError } from './UndiciRetryRequestError'; import type { Either } from './either'; export type RequestResult<T> = { body: T; headers: IncomingHttpHeaders; statusCode: number; requestLabel?: string; }; export type DelayResolver = (response: Dispatcher.ResponseData) => number | undefined; export type RetryConfig = { maxAttempts: number; delayBetweenAttemptsInMsecs?: number; delayResolver?: DelayResolver; statusCodesToRetry: readonly number[]; retryOnTimeout: boolean; respectRetryAfter?: boolean; maxRetryAfterInMsecs?: number; }; export type RequestParams = { blobBody?: boolean; safeParseJson?: boolean; requestLabel?: string; throwOnInternalError?: boolean; }; export declare const DEFAULT_RETRY_CONFIG: RetryConfig; export declare const NO_RETRY_CONFIG: RetryConfig; export declare const DEFAULT_REQUEST_PARAMS: RequestParams; export declare function sendWithRetry<T, const ConfigType extends RequestParams = RequestParams>(client: Dispatcher, request: Dispatcher.RequestOptions, retryConfig?: RetryConfig, requestParams?: ConfigType): Promise<Either<ConfigType['throwOnInternalError'] extends true ? RequestResult<unknown> : RequestResult<unknown> | InternalRequestError, RequestResult<ConfigType['blobBody'] extends true ? Blob : T>>>;