UNPKG

@scayle/storefront-core

Version:

Collection of essential utilities to work with the Storefront API

120 lines (119 loc) 3.62 kB
import type { Log } from '../utils'; import type { Cache as CacheInterface } from './cache'; /** * Number of seconds in a minute, used for calculating cache TTL. */ export declare const MINUTE = 60; /** * Options for configuring the caching behavior. */ export type CacheOptions = Partial<{ /** * The TTL (Time-To-Live) of the cache entry in seconds. * Defaults to one hour if not set. */ ttl: number; /** * Custom cache key. If not provided, one will be generated automatically. */ cacheKey: string; /** * Prefix for the generated cache key. Defaults to the function name. */ cacheKeyPrefix: string; /** * Timeout in milliseconds for cache operations (get and set). */ timeout: number; }>; /** * Type representing an asynchronous function. * * @template TArgs The type of the function's arguments. * @template TResult The return type of the function. */ type Fn<TArgs extends unknown[], TResult> = (...args: TArgs) => Promise<TResult>; /** * A class for caching the results of asynchronous function calls. */ export declare class Cached { private cache; private log; private prefix; private enabled; /** * Creates a new Cached instance. * * @param cache The cache implementation to use. * @param log The logger instance. * @param prefix An optional prefix for cache keys. * @param enabled Whether caching is enabled. * * @throws {Error} If the cache is not initialized. */ constructor(cache: CacheInterface, log: Log, prefix: string | number | undefined, enabled: boolean); /** * Whether caching is enabled. */ get isCacheEnabled(): boolean; /** * Executes a function and caches its result. * * @param fn The asynchronous function to execute. * @param options Optional cache options. * * @returns A wrapped function that handles caching. * * @template TArgs The type of the function's arguments. * @template TResult The return type of the function. */ execute<TArgs extends unknown[], TResult>(fn: Fn<TArgs, TResult>, options?: CacheOptions): Awaited<Fn<TArgs, TResult>>; /** * Retrieves a cached value from the cache. * * @param cacheKey The key of the cached value. * @param options Optional cache options. * * @returns A promise that resolves with the cached value or undefined if not found or caching is disabled. * * @private */ private getCacheValue; /** * Sets a value in the cache. * * @param cacheKey The key to store the value under. * @param value The value to store in the cache. * @param options Optional cache options. * * @returns A promise that resolves when the value is set or undefined if caching is disabled. * * @private */ private setCacheValue; /** * Creates a cache key from the given parameters and prefix. * * @param params The parameters to use for generating the key. * @param prefix An optional prefix for the key. * * @returns The generated cache key. * * @private */ private createCacheKey; /** * Handles errors during cache operations. * * @param error The error that occurred. * @param operation The operation which caused the error * * @private */ private handleError; } export type { Cache as CacheInterface } from './cache'; /** * Type representing the `execute` method of the `Cached` class. */ export type CachedType = Cached['execute'];