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.

33 lines (32 loc) 1.14 kB
/** * @module Resilience */ import { type AsyncMiddlewareFn, type HookContext } from "../../../hooks/_module.js"; import { type FallbackSettings } from "../../../resilience/middlewares/fallback/fallback.types.js"; /** * The `fallback` middleware adds fallback value when an error occurs. * * IMPORT_PATH: `"@daiso-tech/core/resilience"` * @group Middlewares * * @example * ```ts * import { fallback } from "@daiso-tech/core/resilience"; * import { AsyncHooks } from "@daiso-tech/core/hooks"; * * const fetchData = new AsyncHooks(async (url: string): Promise<unknown> => { * const response = await fetch(url); * const json = await response.json(); * if (!response.ok) { * throw json * } * return json; * }, [ * fallback({ fallbackValue: null }) * ]); * * // Will return null when the fetch method throws an error. * console.log(await fetchData.invoke("URL_ENDPOINT")); * ``` */ export declare function fallback<TParameters extends unknown[], TReturn, TContext extends HookContext>(settings: NoInfer<FallbackSettings<TParameters, TReturn, TContext>>): AsyncMiddlewareFn<TParameters, TReturn, TContext>;