/** * @file Unified Storage Utilities * @description Standardized storage abstractions for localStorage, sessionStorage, * and in-memory storage with TTL and type safety. * * @module shared/storage-utils */ /** * Storage item wrapper with metadata. */ export interface StorageItem { value: T; timestamp: number; ttl?: number; version?: number; } /** * Storage options for set operations. */ export interface StorageSetOptions { /** Time-to-live in milliseconds (0 = no expiration) */ ttlMs?: number; /** Version number for migrations */ version?: number; } /** * Storage configuration. */ export interface StorageConfig { /** Prefix for all keys */ prefix?: string; /** Default TTL in milliseconds */ defaultTtlMs?: number; /** Storage version */ version?: number; } /** * Type-safe storage wrapper with TTL support. * * @example * ```ts * const storage = new StorageWrapper(localStorage, { prefix: 'app' }); * * // Set with TTL * storage.set('user', { name: 'John' }, { ttlMs: 3600000 }); * * // Get with type * const user = storage.get('user'); * ``` */ export declare class StorageWrapper { private storage; private config; constructor(storage: Storage, config?: StorageConfig); /** * Set a value in storage. */ set(key: string, value: T, options?: StorageSetOptions): boolean; /** * Get a value from storage. */ get(key: string, defaultValue?: T): T | undefined; /** * Remove a value from storage. */ remove(key: string): void; /** * Check if a key exists and is not expired. */ has(key: string): boolean; /** * Get all keys with the configured prefix. */ keys(): string[]; /** * Clear all items with the configured prefix. */ clear(): void; /** * Remove all expired items. */ cleanup(): number; /** * Get storage statistics. */ getStats(): { itemCount: number; totalSize: number; expiredCount: number; }; /** * Get prefixed storage key. */ private getKey; /** * Check if item is a StorageItem wrapper. */ private isStorageItem; } /** * In-memory storage implementation (SSR-safe fallback). */ export declare class MemoryStorage implements Storage { private store; get length(): number; key(index: number): string | null; getItem(key: string): string | null; setItem(key: string, value: string): void; removeItem(key: string): void; clear(): void; } /** * Check if localStorage is available. */ export declare function isLocalStorageAvailable(): boolean; /** * Check if sessionStorage is available. */ export declare function isSessionStorageAvailable(): boolean; /** * Get localStorage with fallback to memory storage. */ export declare function getLocalStorage(): Storage; /** * Get sessionStorage with fallback to memory storage. */ export declare function getSessionStorage(): Storage; /** * Create a localStorage wrapper with prefix. */ export declare function createLocalStorage(config?: StorageConfig): StorageWrapper; /** * Create a sessionStorage wrapper with prefix. */ export declare function createSessionStorage(config?: StorageConfig): StorageWrapper; /** * Create a memory storage wrapper. */ export declare function createMemoryStorage(): StorageWrapper; /** * Set a value in localStorage. */ export declare function setLocal(key: string, value: T, options?: StorageSetOptions): boolean; /** * Get a value from localStorage. */ export declare function getLocal(key: string, defaultValue?: T): T | undefined; /** * Remove a value from localStorage. */ export declare function removeLocal(key: string): void; /** * Set a value in sessionStorage. */ export declare function setSession(key: string, value: T, options?: StorageSetOptions): boolean; /** * Get a value from sessionStorage. */ export declare function getSession(key: string, defaultValue?: T): T | undefined; /** * Remove a value from sessionStorage. */ export declare function removeSession(key: string): void; /** * Safely parse JSON from storage with type guard. */ export declare function getJsonFromStorage(storage: Storage, key: string, guard: (value: unknown) => value is T): T | null; /** * Save JSON to storage. */ export declare function setJsonToStorage(storage: Storage, key: string, value: T): boolean; /** * Listen for storage changes across tabs. * * @param callback - Callback when storage changes * @param options - Filter options * @returns Unsubscribe function */ export declare function onStorageChange(callback: (event: StorageEvent) => void, options?: { key?: string; storage?: Storage; }): () => void; /** * Watch a specific key for changes. */ export declare function watchStorageKey(key: string, callback: (newValue: T | null, oldValue: T | null) => void, options?: { storage?: Storage; }): () => void;