type WithCacheOptions = { // Lifespan of the cache in milliseconds // @default 10 minutes lifespan?: number; }; export function withCache( fn: (...args: Args) => Return, options?: WithCacheOptions, ): (...args: Args) => Return { const { lifespan = 1000 * 60 * 10 } = options ?? {}; const cache = new Map(); return (...args: Args) => { const key = JSON.stringify(args); if (cache.has(key)) { return cache.get(key) as Return; } const value = fn(...args); cache.set(key, value); setTimeout(() => { cache.delete(key); }, lifespan); return value; }; }