import { type Maybe, type PromiseOrValue, type PromiseRateLimiter } from '@dereekb/util'; import { type FetchHandler } from './fetch'; import { type FetchResponseError } from './error'; /** * A FetchRequestFactory with PromiseRateLimiter */ export type RateLimitedFetchHandler = FetchHandler & { /** * Internal limiter used by the RateLimitedFetchRequestFactory. */ readonly _rateLimiter: T; }; export interface RateLimitedFetchHandlerConfig { /** * Rate limiter configuration. Should be based on the target API. */ readonly rateLimiter: T; /** * The maximum number of retries to try. * * Defaults to 1. */ readonly maxRetries?: number | undefined; /** * Update the fetch handler with the response, ideally setting/updating the time at which the rate limiter can be reset. * * Return true if the response should be retried. Should typically also only return true if the response is a throttle error by the remote server. * * The response is only allowed to be retried once not guranteed to be retried if it returns true on a second retry. * * Only called when there is a response: an error without one (an abort, a timeout, a network failure) is rethrown * to the caller untouched. * * @param response - The response, or the response of the thrown FetchResponseError. * @param fetchResponseError - The error the fetch threw for a non-ok response, if any. * @param request - The request that was sent, e.g. to only retry idempotent methods. */ updateWithResponse(response: Response, fetchResponseError?: Maybe, request?: Request): PromiseOrValue; } /** * Creates a FetchHandler that enforces rate limiting via the provided PromiseRateLimiter and supports * automatic retry when the server signals throttling. * * Each attempt sends a clone of the request that keeps the request's `timeout` (see {@link cloneRequestWithTimeout}), * so a configured `fetchTimeout` times every attempt on its own, from when that attempt is sent. The limiter's * waits are not timed. * * @param config - Configuration containing the rate limiter, retry settings, and response handler. * @returns A RateLimitedFetchHandler that rate-limits outgoing requests and retries on throttle responses. */ export declare function rateLimitedFetchHandler(config: RateLimitedFetchHandlerConfig): RateLimitedFetchHandler;