UNPKG

@clerk/shared

Version:

Internal package utils used by the Clerk SDKs

51 lines (50 loc) • 1.45 kB
//#region src/internal/clerk-js/localStorage.ts const CLERK_PREFIX = "__clerk_"; const CLERK_ENVIRONMENT_STORAGE_ENTRY = "environment"; const PASSWORD_CONFIRMATION_ERROR_STORAGE_ENTRY = "password_confirmation_error"; const serialize = JSON.stringify; const parse = JSON.parse; /** * Safe wrapper around localStorage that automatically prefixes keys with 'clerk_' * and handles potential errors and entry expiration */ var SafeLocalStorage = class { static _key(key) { return `${CLERK_PREFIX}${key}`; } static isExpired(entry) { return !!entry.exp && Date.now() > entry.exp; } static setItem(key, value, expiresInMs) { try { const entry = { value, ...expiresInMs && { exp: Date.now() + expiresInMs } }; window.localStorage.setItem(this._key(key), serialize(entry)); } catch {} } static getItem(key, defaultValue) { try { const item = window.localStorage.getItem(this._key(key)); if (!item) return defaultValue; const entry = parse(item); if (!entry) return defaultValue; if (this.isExpired(entry)) { this.removeItem(key); return defaultValue; } return entry?.value ?? defaultValue; } catch { return defaultValue; } } static removeItem(key) { try { window.localStorage.removeItem(this._key(key)); } catch {} } }; //#endregion export { CLERK_ENVIRONMENT_STORAGE_ENTRY, PASSWORD_CONFIRMATION_ERROR_STORAGE_ENTRY, SafeLocalStorage }; //# sourceMappingURL=localStorage.mjs.map