UNPKG

@modern-kit/utils

Version:
22 lines (19 loc) 805 B
import { isClient } from '../../device/isClient/index.mjs'; import { isFunction } from '../../validator/isFunction/index.mjs'; import { parseJSON } from '../../common/parseJSON/index.mjs'; import '../../device/isServer/index.mjs'; function getStorageItem(type, key, initialValue = null) { if (!isClient()) { throw new Error("Cannot be executed unless it is a client environment."); } try { const initialValueToUse = isFunction(initialValue) ? initialValue() : initialValue; const storage = window[type]; const storedValue = storage.getItem(key); return storedValue ? parseJSON(storedValue) : initialValueToUse; } catch (err) { throw new Error(`Failed to read data for key "${key}" in ${type}: ${err}`); } } export { getStorageItem }; //# sourceMappingURL=index.mjs.map