@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.
66 lines • 2.21 kB
JavaScript
/**
* @module Resilience
*/
import {} from "../../../hooks/_module.js";
import {} from "../../../resilience/middlewares/fallback/fallback.types.js";
import { resolveAsyncLazyable, callInvokable, callErrorPolicyOnValue, callErrorPolicyOnThrow, } from "../../../utilities/_module.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 function fallback(settings) {
const { fallbackValue, errorPolicy, onFallback = () => { } } = settings;
return async (args, next, { context }) => {
try {
const value = await next(...args);
if (!callErrorPolicyOnValue(errorPolicy, value)) {
return value;
}
const resolvedFallbackValue = await resolveAsyncLazyable(fallbackValue);
callInvokable(onFallback, {
error: value,
fallbackValue: resolvedFallbackValue,
args,
context,
});
return resolvedFallbackValue;
// Handle fallback value if an error is thrown
}
catch (error) {
if (!(await callErrorPolicyOnThrow(errorPolicy, error))) {
throw error;
}
const resolvedFallbackValue = await resolveAsyncLazyable(fallbackValue);
callInvokable(onFallback, {
error,
fallbackValue: resolvedFallbackValue,
args,
context,
});
return resolvedFallbackValue;
}
};
}
//# sourceMappingURL=fallback.middleware.js.map