UNPKG

@metis-w/api-client

Version:

Modern TypeScript HTTP API client with dynamic routes, parameterized endpoints, interceptors, and advanced features

121 lines 4.26 kB
import { DynamicClientConstants } from "../constants"; /** * Manages caching for dynamic routes, actions, and parameterized routes * in the DynamicClient to improve performance and maintain stable references. */ export class CacheManager { constructor() { this.routeCache = new Map(); this.actionCache = new Map(); this.parameterizedCache = new Map(); } /** * Gets a cached route by controller name * @param controller - The controller name to get the cached route for * @returns The cached route or undefined if not found */ getRoute(controller) { return this.routeCache.get(controller); } /** * Sets a cached route for a controller * @param controller - The controller name to set the route for * @param route - The route to cache */ setRoute(controller, route) { this.routeCache.set(controller, route); } /** * Checks if a route is cached * @param controller - The controller name to check * @returns True if the route is cached, false otherwise */ hasRoute(controller) { return this.routeCache.has(controller); } /** * Gets a cached action route by cache key * @param cacheKey - The cache key to get the action route for * @returns The cached action route or undefined if not found */ getActionRoute(cacheKey) { return this.actionCache.get(cacheKey); } /** * Sets a cached action route * @param cacheKey - The cache key to set the action route for * @param actionRoute - The action route to cache */ setActionRoute(cacheKey, actionRoute) { this.actionCache.set(cacheKey, actionRoute); } /** * Checks if an action route is cached * @param cacheKey - The cache key to check * @returns True if the action route is cached, false otherwise */ hasActionRoute(cacheKey) { return this.actionCache.has(cacheKey); } /** * Gets a cached parameterized route by cache key * @param cacheKey - The cache key to get the parameterized route for * @returns The cached parameterized route or undefined if not found */ getParameterizedRoute(cacheKey) { return this.parameterizedCache.get(cacheKey); } /** * Sets a cached parameterized route * @param cacheKey - The cache key to set the parameterized route for * @param paramRoute - The parameterized route to cache */ setParameterizedRoute(cacheKey, paramRoute) { this.parameterizedCache.set(cacheKey, paramRoute); } /** * Checks if a parameterized route is cached * @param cacheKey - The cache key to check * @returns True if the parameterized route is cached, false otherwise */ hasParameterizedRoute(cacheKey) { return this.parameterizedCache.has(cacheKey); } /** * Generates cache key for action routes * @param controller - The controller name to generate the cache key for * @returns The generated cache key */ static generateActionCacheKey(controller) { return `${DynamicClientConstants.CACHE_PREFIXES.ACTION}${controller}`; } /** * Generates cache key for parameterized routes * @param controller - The controller name to generate the cache key for * @param id - The ID to include in the cache key */ static generateParameterizedCacheKey(controller, id) { return `${DynamicClientConstants.CACHE_PREFIXES.PARAMETERIZED}${controller}_${id}`; } /** * This method clears the route, action, and parameterized caches. * It is useful for resetting the cache state, for example, when the API structure changes */ clearProxyCache() { this.routeCache.clear(); this.actionCache.clear(); this.parameterizedCache.clear(); } /** * Gets cache statistics * @returns An object containing the number of cached routes, actions, and parameterized routes */ getStats() { return { routes: this.routeCache.size, actions: this.actionCache.size, parameterized: this.parameterizedCache.size, }; } } //# sourceMappingURL=cache-manager.js.map