|
| 1 | +/** |
| 2 | + * @fileoverview A utility to interact safely with localStorage and sessionStorage |
| 3 | + * with built-in error handling and JSON support. |
| 4 | + */ |
| 5 | + |
| 6 | +type StorageType = 'local' | 'session'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Returns the correct Storage object based on type. |
| 10 | + * @param type - 'local' or 'session' |
| 11 | + */ |
| 12 | +function getStorage(type: StorageType): Storage | null { |
| 13 | + try { |
| 14 | + const storage = type === 'local' ? window.localStorage : window.sessionStorage; |
| 15 | + const testKey = '__storage_test__'; |
| 16 | + storage.setItem(testKey, testKey); |
| 17 | + storage.removeItem(testKey); |
| 18 | + return storage; |
| 19 | + } catch (err) { |
| 20 | + console.error(`[webdev-power-kit][storage] Storage unavailable:`, err); |
| 21 | + return null; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Sets a value in storage. |
| 27 | + * @param key - Storage key |
| 28 | + * @param value - Any serializable value |
| 29 | + * @param type - 'local' (default) or 'session' |
| 30 | + */ |
| 31 | +export function setItem<T>(key: string, value: T, type: StorageType = 'local'): void { |
| 32 | + const storage = getStorage(type); |
| 33 | + if (!storage) return; |
| 34 | + |
| 35 | + try { |
| 36 | + const json = JSON.stringify(value); |
| 37 | + storage.setItem(key, json); |
| 38 | + } catch (err) { |
| 39 | + console.error(`[webdev-power-kit][storage] Failed to set item "${key}":`, err); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Gets a value from storage. |
| 45 | + * @param key - Storage key |
| 46 | + * @param type - 'local' (default) or 'session' |
| 47 | + * @returns Parsed value or null |
| 48 | + */ |
| 49 | +export function getItem<T>(key: string, type: StorageType = 'local'): T | null { |
| 50 | + const storage = getStorage(type); |
| 51 | + if (!storage) return null; |
| 52 | + |
| 53 | + try { |
| 54 | + const item = storage.getItem(key); |
| 55 | + return item ? JSON.parse(item) : null; |
| 56 | + } catch (err) { |
| 57 | + console.error(`[webdev-power-kit][storage] Failed to get item "${key}":`, err); |
| 58 | + return null; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Removes a key from storage. |
| 64 | + * @param key - Key to remove |
| 65 | + * @param type - 'local' (default) or 'session' |
| 66 | + */ |
| 67 | +export function removeItem(key: string, type: StorageType = 'local'): void { |
| 68 | + const storage = getStorage(type); |
| 69 | + if (!storage) return; |
| 70 | + |
| 71 | + try { |
| 72 | + storage.removeItem(key); |
| 73 | + } catch (err) { |
| 74 | + console.error(`[webdev-power-kit][storage] Failed to remove item "${key}":`, err); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Clears all storage keys. |
| 80 | + * @param type - 'local' (default) or 'session' |
| 81 | + */ |
| 82 | +export function clearStorage(type: StorageType = 'local'): void { |
| 83 | + const storage = getStorage(type); |
| 84 | + if (!storage) return; |
| 85 | + |
| 86 | + try { |
| 87 | + storage.clear(); |
| 88 | + } catch (err) { |
| 89 | + console.error(`[webdev-power-kit][storage] Failed to clear ${type}Storage:`, err); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Checks if a key exists in storage. |
| 95 | + * @param key - Key to check |
| 96 | + * @param type - 'local' (default) or 'session' |
| 97 | + * @returns true if exists, false otherwise |
| 98 | + */ |
| 99 | +export function hasItem(key: string, type: StorageType = 'local'): boolean { |
| 100 | + const storage = getStorage(type); |
| 101 | + if (!storage) return false; |
| 102 | + |
| 103 | + try { |
| 104 | + return storage.getItem(key) !== null; |
| 105 | + } catch (err) { |
| 106 | + console.error(`[webdev-power-kit][storage] Failed to check item "${key}":`, err); |
| 107 | + return false; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Returns all keys in storage. |
| 113 | + * @param type - 'local' (default) or 'session' |
| 114 | + * @returns Array of keys |
| 115 | + */ |
| 116 | +export function getAllKeys(type: StorageType = 'local'): string[] { |
| 117 | + const storage = getStorage(type); |
| 118 | + if (!storage) return []; |
| 119 | + |
| 120 | + try { |
| 121 | + return Object.keys(storage); |
| 122 | + } catch (err) { |
| 123 | + console.error(`[webdev-power-kit][storage] Failed to get keys:`, err); |
| 124 | + return []; |
| 125 | + } |
| 126 | +} |
0 commit comments