UNPKG

@daiso-tech/core

Version:

The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.

118 lines (117 loc) 3.74 kB
/** * @module Async */ import { TimeSpan, type Invokable, type HookContext, type AsyncMiddlewareFn } from "../../../utilities/_module-exports.js"; import { type BackoffPolicy } from "../../../async/backof-policies/_module.js"; import { type ErrorPolicy } from "../../../async/middlewares/_shared.js"; /** * * IMPORT_PATH: `"@daiso-tech/core/async"` * @group Middlewares */ export type OnRetryAttemptData<TParameters extends unknown[] = unknown[], TContext extends HookContext = HookContext> = { attempt: number; args: TParameters; context: TContext; }; /** * * IMPORT_PATH: `"@daiso-tech/core/async"` * @group Middlewares */ export type OnRetryAttempt<TParameters extends unknown[] = unknown[], TContext extends HookContext = HookContext> = Invokable<[data: OnRetryAttemptData<TParameters, TContext>]>; /** * * IMPORT_PATH: `"@daiso-tech/core/async"` * @group Middlewares */ export type OnRetryDelayData<TParameters extends unknown[] = unknown[], TContext extends HookContext = HookContext> = { error: unknown; attempt: number; waitTime: TimeSpan; args: TParameters; context: TContext; }; /** * * IMPORT_PATH: `"@daiso-tech/core/async"` * @group Middlewares */ export type OnRetryDelay<TParameters extends unknown[] = unknown[], TContext extends HookContext = HookContext> = Invokable<[data: OnRetryDelayData<TParameters, TContext>]>; /** * * IMPORT_PATH: `"@daiso-tech/core/async"` * @group Middlewares */ export type RetryCallbacks<TParameters extends unknown[] = unknown[], TContext extends HookContext = HookContext> = { /** * Callback function that will be called before execution attempt. */ onExecutionAttempt?: OnRetryAttempt<TParameters, TContext>; /** * Callback function that will be called when the retry delay starts. */ onRetryDelay?: OnRetryDelay<TParameters, TContext>; }; /** * * IMPORT_PATH: `"@daiso-tech/core/async"` * @group Middlewares */ export type RetrySettings<TParameters extends unknown[] = unknown[], TContext extends HookContext = HookContext> = RetryCallbacks<TParameters, TContext> & { /** * You can decide maximal times you can retry. * @default {4} */ maxAttempts?: number; /** * @default * ```ts * import { exponentialBackoffPolicy } from "@daiso-tech/core/async"; * * exponentialBackoffPolicy(); * ``` */ backoffPolicy?: BackoffPolicy; /** * You can choose what errors you want to retry. By default all erros will be retried. * * @default * ```ts * (_error: unknown) => true * ``` */ retryPolicy?: ErrorPolicy; }; /** * The `retry` middleware enables automatic retries for all errors or specific errors, with configurable backoff policies. * An error will be thrown when all retry attempts fail. * * IMPORT_PATH: `"@daiso-tech/core/async"` * @group Middleware * @throws {RetryAsyncError} {@link RetryAsyncError} * * @example * ```ts * import { retry } from "@daiso-tech/core/async"; * import { AsyncHooks, TimeSpan } from "@daiso-tech/core/utilities"; * * const data = await new AsyncHooks( * async (url: string, signal?: AbortSignal): Promise<unknown> => { * const response = await fetch(url, { signal }); * return await response.json(); * }, * [retry()], * { * signalBinder: { * getSignal: (args) => args[1], * forwardSignal: (args, signal) => { * args[1] = signal; * } * } * } * ) * .invoke("URL"); * ``` */ export declare function retry<TParameters extends unknown[], TReturn, TContext extends HookContext>(settings?: NoInfer<RetrySettings<TParameters, TContext>>): AsyncMiddlewareFn<TParameters, TReturn, TContext>;