UNPKG

wretch-middlewares

Version:

Middlewares for the wretch library

76 lines (75 loc) 2.69 kB
import { ConfiguredMiddleware, WretcherOptions } from 'wretch'; export declare type DelayRampFunction = (delay: number, nbOfAttempts: number) => number; export declare type UntilFunction = (response?: Response, error?: Error) => boolean | Promise<boolean>; export declare type OnRetryFunctionResponse = { url?: string; options?: WretcherOptions; } | undefined; export declare type OnRetryFunction = (args: { response?: Response; error?: Error; url: string; options: WretcherOptions; }) => OnRetryFunctionResponse | Promise<OnRetryFunctionResponse>; export declare type ResolverFunction = (response: Response) => Response; export declare type RetryOptions = { delayTimer?: number; delayRamp?: DelayRampFunction; maxAttempts?: number; until?: UntilFunction; onRetry?: OnRetryFunction; retryOnNetworkError?: boolean; resolver?: ResolverFunction; }; export declare type RetryMiddleware = (options?: RetryOptions) => ConfiguredMiddleware; /** * ## Retry middleware * * #### Retries a request multiple times in case of an error (or until a custom condition is true). * * **Options** * * - *delayTimer* `milliseconds` * * > The timer between each attempt. * * > *(default: 500)* * * - *delayRamp* `(delay, nbOfAttempts) => milliseconds` * * > The custom function that is used to calculate the actual delay based on the the timer & the number of attemps. * * > *(default: delay * nbOfAttemps)* * * - *maxAttempts* `number` * * > The maximum number of retries before resolving the promise with the last error. Specifying 0 means infinite retries. * * > *(default: 10)* * * - *until* `(response, error) => boolean || Promise<boolean>` * * > The request will be retried until that condition is satisfied. * * > *(default: response && response.ok)* * * - *onRetry* `({ response, error, url, options }) => { url?, options? } || Promise<{url?, options?}>` * * > Callback that will get executed before retrying the request. If this function returns an object having url and/or options properties, they will override existing values in the retried request. * * > *(default: null)* * * - *retryOnNetworkError* `boolean` * * > If true, will retry the request if a network error was thrown. Will also provide an 'error' argument to the `onRetry` and `until` methods. * * > *(default: false)* * * - *resolver* `(response: Response) => Response` * * > This function is called when resolving the fetch response from duplicate calls. * By default it clones the response to allow reading the body from multiple sources. * * > *(default: response => response.clone())* */ export declare const retry: RetryMiddleware;