UNPKG

@composite-fetcher/with-caching

Version:

withCaching is a simple fetcher core plugin to manage request caching with different drivers

46 lines (40 loc) 1.72 kB
import { BasePlugin, PluginHandlerContext, PluginLifecycleHook } from '@composite-fetcher/core'; interface CacheDriver { has: (key: string) => Promise<boolean>; get: (key: string) => Promise<Response | null>; set: (key: string, response: Response, expiration?: Date) => Promise<void>; delete: (key: string) => Promise<void>; clear: () => Promise<void>; } interface withCachingOptions { cacheDriver?: CacheDriver; defaultTTL?: number; } declare class InMemoryCacheDriver implements CacheDriver { private cache; constructor(); has(key: string): Promise<boolean>; get(key: string): Promise<Response | null>; set(key: string, response: Response, expiration?: Date): Promise<void>; delete(key: string): Promise<void>; clear(): Promise<void>; private serializeResponse; private recreateResponse; } declare class SessionStorageDriver implements CacheDriver { set(key: string, response: Response, expirationDate?: Date): Promise<void>; get(key: string): Promise<Response | null>; has(key: string): Promise<boolean>; delete(key: string): Promise<void>; clear(): Promise<void>; } declare class withCachingPlugin extends BasePlugin { private readonly cacheDriver; private readonly defaultTTL; constructor(options?: withCachingOptions); private generateCacheKey; private getCacheTTL; onPreRequest(context: PluginHandlerContext<PluginLifecycleHook.PRE_REQUEST>): Promise<void | Response>; onPostRequest(context: PluginHandlerContext<PluginLifecycleHook.POST_REQUEST>): Promise<void | Response>; } export { CacheDriver, InMemoryCacheDriver, SessionStorageDriver, withCachingPlugin as withCaching, withCachingOptions };