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.

69 lines (68 loc) 2.15 kB
/** * @module Async */ import { type HookContext, type AsyncMiddlewareFn } from "../../../utilities/_module-exports.js"; import type { RetrySettings } from "../../../async/middlewares/retry/retry.types.js"; /** * 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 * * @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 }); * const json = await response.json(); * if (!response.ok) { * return json; * } * return json; * }, * [retry()], * { * signalBinder: { * getSignal: (args) => args[1], * forwardSignal: (args, signal) => { * args[1] = signal; * } * } * } * ) * .invoke("URL"); * ``` * * The middleware works also when the function returns a {@link Result | `Result`} type. * @example * ```ts * import { retry } from "@daiso-tech/core/async"; * import { AsyncHooks, TimeSpan, Result, resultFailure, resultSuccess } from "@daiso-tech/core/utilities"; * * const data = await new AsyncHooks( * async (url: string, signal?: AbortSignal): Promise<Result> => { * const response = await fetch(url, { signal }); * const json = await response.json(); * if (!response.ok) { * return resultFailure(json); * } * return resultSuccess(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, TReturn>>): AsyncMiddlewareFn<TParameters, TReturn, TContext>;