/** * Interface for storage operations */ export interface StorageWrapper { /** * Gets an item from storage * @param key - The key of the item to get * @returns The value of the item, or null if it doesn't exist */ getItem(key: string): string | null; /** * Sets an item in storage * @param key - The key of the item to set * @param value - The value to set */ setItem(key: string, value: string): void; /** * Removes an item from storage * @param key - The key of the item to remove */ removeItem(key: string): void; /** deletes all values in storage */ clear(): void; } /** * Implementation of StorageWrapper for testing or server environments that don't have localStorage */ export class MemoryStorageWrapper implements StorageWrapper { private storage: Record = {}; getItem(key: string): string | null { return this.storage[key] ?? null; } setItem(key: string, value: string): void { this.storage[key] = value; } removeItem(key: string): void { const { [key]: _, ...rest } = this.storage; this.storage = rest; } clear(): void { this.storage = {}; } } /** * Declare global property for storage */ declare global { // eslint-disable-next-line no-var var __i18nStorage: StorageWrapper | undefined; } /** * Factory function to get the appropriate storage wrapper based on environment * @returns An instance of StorageWrapper appropriate for the current environment */ export function createStorageWrapper(): StorageWrapper { if (typeof globalThis.localStorage !== 'undefined') { return globalThis.localStorage; } if (!globalThis.__i18nStorage) { globalThis.__i18nStorage = new MemoryStorageWrapper(); } return globalThis.__i18nStorage; }