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.

121 lines (120 loc) 4.15 kB
/** * @module Resilience */ import { type BackoffPolicy } from "../../../backoff-policies/contracts/_module.js"; import { type IContext, type IReadableContext } from "../../../execution-context/contracts/_module.js"; import { type MiddlewareFn } from "../../../middleware/contracts/_module.js"; import { type ITimeSpan } from "../../../time-span/contracts/_module.js"; import { TimeSpan } from "../../../time-span/implementations/_module.js"; import { type Invokable, type ErrorPolicySettings } from "../../../utilities/_module.js"; /** * IMPORT_PATH: `"@daiso-tech/core/resilience"` * @group Middlewares */ export type OnRetryAttemptData<TParameters extends Array<unknown> = Array<unknown>> = { attempt: number; args: TParameters; context: IReadableContext; }; /** * IMPORT_PATH: `"@daiso-tech/core/resilience"` * @group Middlewares */ export type OnExecutionAttempt<TParameters extends Array<unknown> = Array<unknown>> = Invokable<[data: OnRetryAttemptData<TParameters>]>; /** * IMPORT_PATH: `"@daiso-tech/core/resilience"` * @group Middlewares */ export type OnRetryDelayData<TParameters extends Array<unknown> = Array<unknown>> = { error: unknown; attempt: number; waitTime: TimeSpan; args: TParameters; context: IReadableContext; }; /** * IMPORT_PATH: `"@daiso-tech/core/resilience"` * @group Middlewares */ export type OnRetryDelay<TParameters extends Array<unknown> = Array<unknown>> = Invokable<[data: OnRetryDelayData<TParameters>]>; /** * Lifecycle callbacks for the `retry` middleware. * Invoked at key points during retry attempts: before execution and before the delay. * * IMPORT_PATH: `"@daiso-tech/core/resilience"` * @group Middlewares */ export type RetryCallbacks<TParameters extends Array<unknown> = Array<unknown>> = { /** * Callback {@link Invokable | `Invokable`} that will be called before execution attempt. */ onExecutionAttempt?: OnExecutionAttempt<TParameters>; /** * Callback {@link Invokable | `Invokable`} that will be called before the retry delay starts. */ onRetryDelay?: OnRetryDelay<TParameters>; }; /** * Configuration for the `retry` resilience middleware. * Retries the wrapped function up to a maximum number of attempts. * Supports configurable backoff policies and lifecycle callbacks. * * IMPORT_PATH: `"@daiso-tech/core/resilience"` * @group Middlewares */ export type RetrySettings<TParameters extends Array<unknown> = Array<unknown>> = RetryCallbacks<TParameters> & ErrorPolicySettings & { /** * You can decide maximal times you can retry. * @default 4 */ maxAttempts?: number; /** * @default * ```ts * import { exponentialBackoff } from "@daiso-tech/core/backoff-policies"; * * exponentialBackoff(); * ``` */ backoffPolicy?: BackoffPolicy; /** * If true last error will be thrown otherwise an {@link RetryResilienceError | `RetryResilienceError`} will be thrown. * * @default false */ throwLastError?: boolean; }; /** * @internal */ export type HandleOnExecutionAttemptSettings<TParameters extends Array<unknown>> = { onExecutionAttempt: OnExecutionAttempt<TParameters>; attempt: number; args: TParameters; context: IContext; }; /** * @internal */ export declare function handleOnExecutionAttempt<TParameters extends Array<unknown>>(settings: HandleOnExecutionAttemptSettings<TParameters>): void; /** * @internal */ export type HandleOnRetryDelaySettings<TParameters extends Array<unknown>> = { onRetryDelay: OnRetryDelay<TParameters>; error: unknown; waitTime: ITimeSpan; attempt: number; args: TParameters; context: IContext; }; /** * @internal */ export declare function handleOnRetryDelay<TParameters extends Array<unknown>>(settings: HandleOnRetryDelaySettings<TParameters>): void; /** * IMPORT_PATH: `@daiso-tech/core/resilience` * @group Middlewares * @throws {RetryResilienceError} */ export declare function retry<TParameters extends Array<unknown>, TReturn>(settings?: NoInfer<RetrySettings<TParameters>>): MiddlewareFn<TParameters, Promise<TReturn>>;