UNPKG

browser-cache-async

Version:

The browser-cache-async package provides a highly customizable, asynchronous caching system for client-side data management. Leveraging the power of the browser's IndexedDB, it enables efficient storage and retrieval of API responses, significantly reduci

41 lines (40 loc) 1.57 kB
import { IRecordID } from 'browser-keyval-stores'; /** * Cache If Function * Utility type to indicate the function that will be invoked to evaluate if the data should be * cached. */ type ICacheIfFn<T> = ((id: IRecordID, data: T) => Promise<boolean>) | ((id: IRecordID, data: T) => boolean); /** * String Value * Utility type to indicate that the value can be any template literal supported by the ms package. */ type IUnit = 'Years' | 'Year' | 'Yrs' | 'Yr' | 'Y' | 'Weeks' | 'Week' | 'W' | 'Days' | 'Day' | 'D' | 'Hours' | 'Hour' | 'Hrs' | 'Hr' | 'H' | 'Minutes' | 'Minute' | 'Mins' | 'Min' | 'M' | 'Seconds' | 'Second' | 'Secs' | 'Sec' | 's' | 'Milliseconds' | 'Millisecond' | 'Msecs' | 'Msec' | 'Ms'; type IUnitAnyCase = IUnit | Uppercase<IUnit> | Lowercase<IUnit>; type IStringValue = `${number}` | `${number}${IUnitAnyCase}` | `${number} ${IUnitAnyCase}`; /** * Query Options * Object in charge of controlling how the query is executed and cached. */ type IQueryOptions<T> = { id?: IRecordID; query: () => Promise<T>; cacheIf?: ICacheIfFn<T> | boolean; revalidate?: IStringValue | number; }; /** * Processed Query Options * The result of processing the query options object passed by the developer. */ type IProcessedQueryOptions<T> = IQueryOptions<T> & { revalidate: number; }; /** * Wrapped Data * The object that wraps the data in order to control its freshness. */ type IWrappedData<T> = { data: T; staleAt: number; }; export type { ICacheIfFn, IStringValue, IQueryOptions, IProcessedQueryOptions, IWrappedData, };