export interface RetryOptions { maxAttempts?: number; waitTime?: number; shouldRetry?: (error: Error | Response) => boolean; } export interface RetryFetchOptions extends RetryOptions { customFetch?: FetchFunction; } export interface RequestInitWithReason extends RequestInit { reason?: any; } export interface XhrFetchOptions extends RequestInit { /** * Callback for download progress (response). * @param loaded - Bytes received so far. * @param total - Total bytes expected (-1 if unknown). */ onProgress?: (loaded: number, total: number) => void; /** * Callback for upload progress (request body). * @param loaded - Bytes sent so far. * @param total - Total bytes to send (-1 if unknown). */ onUploadProgress?: (loaded: number, total: number) => void; /** * Minimum interval (ms) between progress callbacks. * @default 500 */ throttleInterval?: number; /** * Request timeout in milliseconds. */ timeout?: number; } export type FetchFunction = ( resource: RequestInfo | URL, options?: RequestInit ) => Promise; export type XhrFetchFunction = ( resource: RequestInfo | URL, options?: XhrFetchOptions ) => Promise; export interface SleepOptions { signal?: AbortSignal; } /* From common.d.ts */ export const FETCH_REQUEST_ABORTED: "Fetch request aborted"; export const DEFAULT_RETRY_COUNT: 3; export const DEFAULT_WAIT_TIME_MS: 1000; /* From Fetcher.d.ts */ export class Fetcher { /** * Cancels all ongoing fetches created by this Fetcher instance. */ cancel(): void; /** * Creates a singleton fetch function with retries, tied to this Fetcher's cancellation. * @param {object} [options] * @param {number} [options.maxAttempts] * @param {number} [options.waitTime] * @param {(error: Error | Response) => boolean} [options.shouldRetry] * @param {FetchFunction} [options.customFetch] * @returns {{ fetch: FetchFunction, cancel: () => void, cancelAndWait: () => Promise }} */ createFetchFunction(options?: { maxAttempts?: number | undefined; waitTime?: number | undefined; shouldRetry?: ((error: Error | Response) => boolean) | undefined; customFetch?: FetchFunction | undefined; }): { fetch: FetchFunction; cancel: () => void; cancelAndWait: () => Promise; }; /** * Performs a one‑off fetch request that can be cancelled via Fetcher.cancel(). * @param {RequestInfo | URL} resource * @param {RequestInit} [options] * @returns {Promise} */ fetch(resource: RequestInfo | URL, options?: RequestInit): Promise; /** * Creates a singleton fetch function with retries using XMLHttpRequest. * Supports progress callbacks (onProgress, onUploadProgress) and timeout. * @param {object} [options] * @param {number} [options.maxAttempts] * @param {number} [options.waitTime] * @param {(error: Error | Response) => boolean} [options.shouldRetry] * @param {() => XMLHttpRequest} [xhrFactory] * @returns {{ fetch: XhrFetchFunction, cancel: () => void, cancelAndWait: () => Promise }} */ createXhrFetchFunction(options?: { maxAttempts?: number | undefined; waitTime?: number | undefined; shouldRetry?: ((error: Error | Response) => boolean) | undefined; }, xhrFactory?: () => XMLHttpRequest): { fetch: XhrFetchFunction; cancel: () => void; cancelAndWait: () => Promise; }; /** * Destroys the Fetcher instance, aborting any ongoing requests. * After destroy, the instance should not be used anymore. */ destroy(): void; #private; } /* From retry.d.ts */ /** * Wraps any fetch-like function with retry logic. * * @template {(...args: any[]) => Promise} T * @param {T} fetcher - The function that performs the request (e.g., fetch, xhrFetch). * @param {RetryOptions} [options] * @returns {T} A function with the same signature as `fetcher` but with retry capability. */ export function withRetry Promise>(fetcher: T, options?: RetryOptions): T; /* From retryFetch.d.ts */ /** * Creates a fetch function with retries using the native fetch. * @param {RetryFetchOptions} [options] * @returns {FetchFunction} */ export function createRetryFetch(options?: RetryFetchOptions): FetchFunction; /* From SingletonFetcher.d.ts */ /** * Creates a singleton fetch function. * @param {FetchFunction} [customFetch] * @returns {FetchFunction} */ export function createSingletonFetch(customFetch?: FetchFunction): FetchFunction; /** * A fetch wrapper that ensures only one request is active at a time. * When a new request is made while a previous one is still pending, * the previous one is automatically cancelled. */ export class SingletonFetcher { /** * Creates a new SingletonFetcher instance. * @param {FetchFunction} [customFetch] - Custom fetch implementation. Defaults to global fetch. */ constructor(customFetch?: FetchFunction); /** @type {boolean} */ get isLoading(): boolean; /** @type {AbortController | null} */ get controller(): AbortController | null; /** * Performs a fetch request, cancelling any ongoing request first. * @param {RequestInfo | URL} resource * @param {RequestInit} [options] * @returns {Promise} */ fetch(resource: RequestInfo | URL, options?: RequestInit): Promise; /** * Cancels the currently active request, if any. */ cancel(): void; /** * Cancels the current request and waits for it to fully settle (reject with AbortError). * Useful in tests or scenarios where you need the cancel to be complete before proceeding. * @returns {Promise} */ cancelAndWait(): Promise; #private; } /* From xhrFetch.d.ts */ /** * XMLHttpRequest-based fetch replacement with progress callbacks. * Supports both upload and download progress, abort signals, timeouts, and custom headers. * * @param {RequestInfo | URL} resource - The resource to fetch. * @param {XhrFetchOptions} [options] - Fetch options extended with progress callbacks. * @param {() => XMLHttpRequest} [xhrFactory] - XHR (for tests) * @returns {Promise} A Promise that resolves with a Response object. */ export function xhrFetch(resource: RequestInfo | URL, options?: XhrFetchOptions, xhrFactory?: () => XMLHttpRequest): Promise; /** * Creates a fetch function with retries using XMLHttpRequest (with progress support). * @param {RetryOptions} [options] * @param {() => XMLHttpRequest} [xhrFactory] * @returns {XhrFetchFunction} */ export function createRetryXhr(options?: RetryOptions, xhrFactory?: () => XMLHttpRequest): XhrFetchFunction; /** * Creates a singleton fetch function using XMLHttpRequest (with progress support). * Only one request will be active at a time; a new request cancels the previous one. * @param {() => XMLHttpRequest} [xhrFactory] * @returns {XhrFetchFunction} * * @example * const fetchXhr = createSingletonXhr(); * fetchXhr('/api/data', { onProgress: p => console.log(p) }); * fetchXhr('/api/data2'); // cancels the first request */ export function createSingletonXhr(xhrFactory?: () => XMLHttpRequest): XhrFetchFunction; /** * Creates a singleton fetch function with retries, using XMLHttpRequest. * Combines retry logic and singleton cancellation. * * @param {RetryOptions} [retryOptions] - Retry configuration (maxAttempts, waitTime, shouldRetry) * @param {() => XMLHttpRequest} [xhrFactory] * @returns {XhrFetchFunction} * * @example * const fetchXhr = createSingletonRetryXhr({ maxAttempts: 3 }); * const response = await fetchXhr('https://api.example.com/data', { * onProgress: (l, t) => console.log(`${l}/${t}`) * }); */ export function createSingletonRetryXhr(retryOptions?: RetryOptions, xhrFactory?: () => XMLHttpRequest): XhrFetchFunction;