@epic-web/cachified
Version:
neat wrapper for various caches
102 lines (101 loc) • 3.78 kB
TypeScript
import { CacheMetadata, Context } from './common';
export type GetFreshValueStartEvent = {
name: 'getFreshValueStart';
};
export type GetFreshValueHookPendingEvent = {
name: 'getFreshValueHookPending';
};
export type GetFreshValueSuccessEvent<Value> = {
name: 'getFreshValueSuccess';
value: Value;
};
export type GetFreshValueErrorEvent = {
name: 'getFreshValueError';
error: unknown;
};
export type GetFreshValueCacheFallbackEvent = {
name: 'getFreshValueCacheFallback';
value: unknown;
};
/** @deprecated this event will be removed in favour of `CheckFreshValueErrorObjEvent` */
export type CheckFreshValueErrorEvent<Value> = {
name: 'checkFreshValueError';
reason: string;
};
export type CheckFreshValueErrorObjEvent = {
name: 'checkFreshValueErrorObj';
reason: unknown;
};
export type WriteFreshValueSuccessEvent<Value> = {
name: 'writeFreshValueSuccess';
metadata: CacheMetadata;
/**
* Value might not actually be written to cache in case getting fresh
* value took longer then ttl */
written: boolean;
migrated: boolean;
};
export type WriteFreshValueErrorEvent = {
name: 'writeFreshValueError';
error: unknown;
};
export type GetCachedValueStartEvent = {
name: 'getCachedValueStart';
};
export type GetCachedValueReadEvent = {
name: 'getCachedValueRead';
entry: unknown;
};
export type GetCachedValueEmptyEvent = {
name: 'getCachedValueEmpty';
};
export type GetCachedValueOutdatedEvent = {
name: 'getCachedValueOutdated';
value: unknown;
metadata: CacheMetadata;
};
export type GetCachedValueSuccessEvent<Value> = {
name: 'getCachedValueSuccess';
value: Value;
migrated: boolean;
};
/** @deprecated this event will be removed in favour of `CheckCachedValueErrorObjEvent` */
export type CheckCachedValueErrorEvent = {
name: 'checkCachedValueError';
reason: string;
};
export type CheckCachedValueErrorObjEvent = {
name: 'checkCachedValueErrorObj';
reason: unknown;
};
export type GetCachedValueErrorEvent = {
name: 'getCachedValueError';
error: unknown;
};
export type RefreshValueStartEvent = {
name: 'refreshValueStart';
};
export type RefreshValueSuccessEvent<Value> = {
name: 'refreshValueSuccess';
value: Value;
};
export type RefreshValueErrorEvent = {
name: 'refreshValueError';
error: unknown;
};
export type DoneEvent<Value> = {
name: 'done';
value: Value;
};
export type CacheEvent<Value> = GetFreshValueStartEvent | GetFreshValueHookPendingEvent | GetFreshValueSuccessEvent<Value> | GetFreshValueErrorEvent | GetFreshValueCacheFallbackEvent | CheckFreshValueErrorEvent<Value> | CheckFreshValueErrorObjEvent | WriteFreshValueSuccessEvent<Value> | WriteFreshValueErrorEvent | GetCachedValueStartEvent | GetCachedValueReadEvent | GetCachedValueEmptyEvent | GetCachedValueOutdatedEvent | GetCachedValueSuccessEvent<Value> | CheckCachedValueErrorEvent | CheckCachedValueErrorObjEvent | GetCachedValueErrorEvent | RefreshValueStartEvent | RefreshValueSuccessEvent<Value> | RefreshValueErrorEvent | DoneEvent<Value>;
export type Reporter<Value> = (event: CacheEvent<Value>) => void;
export type CreateReporter<Value> = (context: Omit<Context<Value>, 'report'>) => Reporter<Value>;
export type NoInfer<T> = [T][T extends any ? 0 : never];
interface ReporterOpts {
formatDuration?: (ms: number) => string;
logger?: Pick<typeof console, 'log' | 'warn' | 'error'>;
performance?: Pick<typeof Date, 'now'>;
}
export declare function verboseReporter<Value>({ formatDuration, logger, performance, }?: ReporterOpts): CreateReporter<Value>;
export declare function mergeReporters<Value = unknown>(...reporters: (CreateReporter<Value> | null | undefined)[]): CreateReporter<Value>;
export {};