/** * @file Data Prefetching * @description API and data prefetching utilities with caching, deduplication, * and stale-while-revalidate patterns. * * Features: * - API response prefetching * - GraphQL query prefetching * - Stale-while-revalidate * - Request deduplication * - Cache management * - Prefetch on interaction */ /** * Data prefetch configuration */ export interface DataPrefetchConfig { /** Enable data prefetching */ enabled: boolean; /** Default cache TTL in ms */ defaultTTL: number; /** Enable stale-while-revalidate */ staleWhileRevalidate: boolean; /** Stale time before revalidation in ms */ staleTime: number; /** Enable request deduplication */ deduplicate: boolean; /** Deduplication window in ms */ dedupeWindow: number; /** Maximum cache entries */ maxCacheEntries: number; /** Include credentials */ credentials: RequestCredentials; /** Default headers */ defaultHeaders: Record; /** Retry configuration */ retry: { attempts: number; delay: number; backoff: 'linear' | 'exponential'; }; /** Debug mode */ debug: boolean; } /** * Prefetch request options */ export interface PrefetchRequestOptions { /** HTTP method */ method?: 'GET' | 'POST'; /** Request headers */ headers?: Record; /** Request body (for POST) */ body?: unknown; /** Cache TTL in ms */ ttl?: number; /** Cache key (defaults to URL) */ cacheKey?: string; /** Priority */ priority?: 'high' | 'normal' | 'low'; /** Skip cache and force fetch */ force?: boolean; /** Tags for cache invalidation */ tags?: string[]; } /** * Prefetch result */ export interface PrefetchResult { data: T | null; error: Error | null; fromCache: boolean; stale: boolean; fetchedAt: number; } /** * GraphQL query options */ export interface GraphQLPrefetchOptions { query: string; variables?: Record; operationName?: string; ttl?: number; tags?: string[]; } /** * Manages data prefetching with caching */ export declare class DataPrefetchManager { private readonly config; private cache; private pendingRequests; private listeners; constructor(config?: Partial); /** * Prefetch data from a URL */ prefetch(url: string, options?: PrefetchRequestOptions): Promise>; /** * Prefetch multiple URLs */ prefetchMany(requests: Array<{ url: string; options?: PrefetchRequestOptions; }>): Promise>>; /** * Prefetch a GraphQL query */ prefetchGraphQL(endpoint: string, options: GraphQLPrefetchOptions): Promise>; /** * Get cached data without fetching */ getCached(cacheKey: string): T | null; /** * Check if data is cached */ isCached(cacheKey: string): boolean; /** * Check if cached data is stale */ isStale(cacheKey: string): boolean; /** * Invalidate cache by key */ invalidate(cacheKey: string): boolean; /** * Invalidate cache by tag */ invalidateByTag(tag: string): number; /** * Subscribe to cache updates */ subscribe(cacheKey: string, callback: (data: unknown) => void): () => void; /** * Clear all cache */ clearCache(): void; /** * Get cache statistics */ getStats(): { entries: number; size: number; pendingRequests: number; }; private executeFetch; private fetchWithRetry; private revalidate; private setCache; private evictOldest; private notifySubscribers; private generateCacheKey; private generateGraphQLCacheKey; private estimateSize; private sleep; private log; } /** * Get or create the global data prefetch manager */ export declare function getDataPrefetchManager(config?: Partial): DataPrefetchManager; /** * Reset the manager instance */ export declare function resetDataPrefetchManager(): void; /** * Prefetch data from a URL */ export declare function prefetchData(url: string, options?: PrefetchRequestOptions): Promise>; /** * Prefetch a GraphQL query */ export declare function prefetchGraphQL(endpoint: string, options: GraphQLPrefetchOptions): Promise>; /** * Get cached data */ export declare function getCachedData(cacheKey: string): T | null; /** * Invalidate cache by key */ export declare function invalidateData(cacheKey: string): boolean; /** * Invalidate cache by tag */ export declare function invalidateDataByTag(tag: string): number;