UNPKG

@gorgonjs/gorgon

Version:

A simple caching library for async functions

50 lines (49 loc) 2 kB
import MemoryCache from './provider/memory'; export type asyncFunction = () => Promise<any> | (() => any); export type GorgonHookKey = 'settings' | 'addProvider' | 'put' | 'clear' | 'clearAll' | 'overwrite' | 'get' | 'valueError'; export type GorgonHook = (key: GorgonHookKey, input?: any, output?: any) => void; export type GorgonSettings = { debug: boolean; defaultProvider: string; retry: number; }; export type GorgonSettingsInput = { debug?: boolean; defaultProvider?: string; retry?: number; }; export type GorgonPolicy = { expiry: number | Date | false; provider: string; }; export type GorgonPolicyInput = GorgonPolicy | number | Date; export type GorgonPolicySanitized = { expiry: number | false; provider: string; }; export interface IGorgonCacheProvider { init: () => Promise<void>; get: (key: string) => Promise<any>; set: <R>(key: string, value: R, policy: GorgonPolicySanitized) => Promise<R>; clear: (key?: string) => Promise<boolean>; keys: () => Promise<string[]>; } declare const Gorgon: { providers: { [key: string]: IGorgonCacheProvider; }; hooks: { [key: string]: GorgonHook[]; }; _callHooks: (key: GorgonHookKey, input?: any, output?: any) => void; settings: (newSettings?: GorgonSettingsInput) => GorgonSettings; addHook: (key: GorgonHookKey, hook: GorgonHook | Array<GorgonHook>) => void; addProvider: (name: string, provider: IGorgonCacheProvider) => void; put: <R>(key: string, value: R, policy?: GorgonPolicyInput) => Promise<R>; clear: (key: string, provider?: string, hookIdentifier?: string) => Promise<boolean | boolean[]>; clearAll: (provider?: string, hookIdentifier?: string) => Promise<boolean>; overwrite: (key: string, asyncFunc: asyncFunction, policy?: GorgonPolicyInput) => Promise<any>; get: <R_1>(key: string, asyncFunc: () => R_1, policy?: GorgonPolicyInput) => Promise<R_1>; }; export { MemoryCache }; export default Gorgon;