import { Query, QueryKey, QueryState } from '@tanstack/react-query'; import { CacheStats } from '../types'; /** * Cache query filter options */ export interface CacheFilterOptions { /** Match exact query key */ exact?: boolean; /** Filter by query type */ type?: 'active' | 'inactive' | 'all'; /** Filter stale queries */ stale?: boolean; /** Filter fetching queries */ fetching?: boolean; /** Custom predicate */ predicate?: (query: Query) => boolean; } /** * Cache entry info for inspection */ export interface CacheEntryInfo { queryKey: QueryKey; data: TData | undefined; state: QueryState; isStale: boolean; isFetching: boolean; dataUpdatedAt: number; errorUpdatedAt: number; fetchStatus: 'fetching' | 'paused' | 'idle'; } /** * Return type for useApiCache */ export interface UseApiCacheResult { /** Get cached data for a query key */ getData: (queryKey: QueryKey) => TData | undefined; /** Set cached data for a query key */ setData: (queryKey: QueryKey, updater: TData | ((old: TData | undefined) => TData)) => TData | undefined; /** Remove cached data for a query key */ removeData: (queryKey: QueryKey) => void; /** Invalidate queries matching the key */ invalidate: (queryKey: QueryKey, options?: { exact?: boolean; }) => Promise; /** Invalidate all queries */ invalidateAll: () => Promise; /** Refetch queries matching the key */ refetch: (queryKey: QueryKey, options?: { exact?: boolean; }) => Promise; /** Cancel in-flight queries */ cancel: (queryKey: QueryKey) => Promise; /** Reset queries to initial state */ reset: (queryKey: QueryKey) => Promise; /** Prefetch a query */ prefetch: (queryKey: QueryKey, queryFn: () => Promise, options?: { staleTime?: number; }) => Promise; /** Get cache statistics */ getStats: () => CacheStats; /** Get cache entry info */ getEntry: (queryKey: QueryKey) => CacheEntryInfo | null; /** Get all cache entries matching filter */ getEntries: (filter?: CacheFilterOptions) => CacheEntryInfo[]; /** Check if query exists in cache */ hasQuery: (queryKey: QueryKey) => boolean; /** Check if query is stale */ isStale: (queryKey: QueryKey) => boolean; /** Check if query is fetching */ isFetching: (queryKey: QueryKey) => boolean; /** Clear entire cache */ clear: () => void; /** Subscribe to cache changes */ subscribe: (callback: () => void) => () => void; } /** * Hook for managing API response cache * * @returns Cache management utilities * * @example * ```typescript * function UserList() { * const { getData, setData, invalidate } = useApiCache(); * * // Read from cache * const cachedUsers = getData(['users', 'list']); * * // Optimistically update cache * const handleOptimisticUpdate = (userId: string, updates: Partial) => { * setData(['users', 'list'], (users) => * users?.map(u => u.id === userId ? { ...u, ...updates } : u) ?? [] * ); * }; * * // Invalidate after mutation * const handleMutationSuccess = async () => { * await invalidate(['users']); * }; * * return
...
; * } * ``` */ export declare function useApiCache(): UseApiCacheResult; /** * Hook for monitoring cache statistics * * @example * ```typescript * function CacheMonitor() { * const { stats, isMonitoring, startMonitoring, stopMonitoring } = useCacheMonitor({ * interval: 5000, * autoStart: true, * }); * * return ( *
*

Entries: {stats.entries}

*

Hit Rate: {(stats.hitRate * 100).toFixed(1)}%

*

Size: {(stats.size / 1024).toFixed(1)} KB

*
* ); * } * ``` */ export declare function useCacheMonitor(options?: { interval?: number; autoStart?: boolean; }): { stats: CacheStats; isMonitoring: boolean; startMonitoring: () => void; stopMonitoring: () => void; refresh: () => void; }; /** * Hook for watching specific cache entries * * @example * ```typescript * function UserCacheWatcher({ userId }: { userId: string }) { * const { entry, isLoaded, isStale } = useCacheEntry(['users', userId]); * * if (!isLoaded) return

Not in cache

; * if (isStale) return

Data is stale

; * * return

User: {entry.data?.name}

; * } * ``` */ export declare function useCacheEntry(queryKey: QueryKey): { entry: CacheEntryInfo | null; isLoaded: boolean; isStale: boolean; isFetching: boolean; refresh: () => void; }; /** * Hook for bulk cache operations * * @example * ```typescript * function BulkCacheOperations() { * const { invalidateByTag, removeByPredicate } = useBulkCacheOperations(); * * const handleLogout = async () => { * // Invalidate all user-related queries * await invalidateByTag('user'); * }; * * const handleClearStale = () => { * removeByPredicate((query) => query.isStale()); * }; * * return
...
; * } * ``` */ export declare function useBulkCacheOperations(): { invalidateByTag: (tag: string) => Promise; invalidateByPrefix: (prefix: string) => Promise; removeByPredicate: (predicate: (query: Query) => boolean) => void; refetchActive: () => Promise; refetchStale: () => Promise; };