UNPKG

@hyper-fetch/core

Version:

Cache, Queue and Persist your requests no matter if you are online or offline!

92 lines 3.71 kB
import { AdapterInstance, ResponseType } from '../adapter'; import { ResponseDetailsType } from '../managers'; import { ClientInstance } from '../client'; import { CacheOptionsType, CacheAsyncStorageType, CacheStorageType, CacheValueType, CacheSetState, RequestCacheType, getCacheEvents } from '.'; import { RequestInstance } from '../request'; import { ExtractAdapterType, ExtractErrorType, ExtractResponseType } from '../types'; import { EventEmitter } from '../utils'; /** * Cache class handles the data exchange with the dispatchers. * * @note * Keys used to save the values are created dynamically on the Request class * */ export declare class Cache<Adapter extends AdapterInstance> { options?: CacheOptionsType | undefined; emitter: EventEmitter; events: ReturnType<typeof getCacheEvents>; storage: CacheStorageType; lazyStorage?: CacheAsyncStorageType; version: string; garbageCollectors: Map<string, NodeJS.Timeout>; private logger; private client; constructor(options?: CacheOptionsType | undefined); /** Initialize the cache with a client reference, set up logging, and start garbage collection. */ initialize: (client: ClientInstance<{ adapter: Adapter; }>) => this; /** * Set the cache data to the storage * @param request * @param response * @returns */ set: <Request extends RequestCacheType<RequestInstance>>(request: Request, response: CacheSetState<ResponseType<ExtractResponseType<Request>, ExtractErrorType<Request>, ExtractAdapterType<Request>> & ResponseDetailsType> & { hydrated?: boolean; }) => void; /** * Update the cache data with partial response data * @param request * @param partialResponse * @returns */ update: <Request extends RequestCacheType<RequestInstance>>(request: Request, partialResponse: CacheSetState<Partial<ResponseType<ExtractResponseType<Request>, ExtractErrorType<Request>, ExtractAdapterType<Request>> & ResponseDetailsType>>) => void; /** * Get particular record from storage by cacheKey. It will trigger lazyStorage to emit lazy load event for reading it's data. * @param cacheKey * @returns */ get: <Response, Error>(cacheKey: string) => CacheValueType<Response, Error, Adapter> | undefined; /** * Get sync storage keys, lazyStorage keys will not be included * @returns */ keys: () => string[]; /** * Delete record from storages and trigger invalidation event * @param cacheKey */ delete: (cacheKey: string) => void; /** * Invalidate cache by cacheKey or partial matching with RegExp * It emits invalidation event for each matching cacheKey and sets staleTime to 0 to indicate out of time cache * @param key - cacheKey or Request instance or RegExp for partial matching */ invalidate: (cacheKeys: string | RegExp | RequestInstance | Array<string | RegExp | RequestInstance>) => void; /** * Used to receive data from lazy storage * @param cacheKey */ getLazyResource: <Response, Error>(cacheKey: string) => Promise<CacheValueType<Response, Error, Adapter> | undefined>; /** * Used to receive keys from lazy storage only */ getLazyKeys: () => Promise<string[]>; /** * Used to receive deduplicated keys from both sync storage and lazy storage */ getAllKeys: () => Promise<string[]>; /** * Schedule garbage collection for given key * @param cacheKey * @returns */ scheduleGarbageCollector: (cacheKey: string) => void; /** * Clear cache storages */ clear: () => Promise<void>; } //# sourceMappingURL=cache.d.ts.map