import { Injectable } from "@angular/core"; @Injectable({ providedIn: "root", }) export class LocalStorageService { setItem(key: string, value: T) { try { const item = JSON.stringify(value); localStorage.setItem(key, item); } catch (e) { console.error("Error storing item in local storage: ", e); } } getItem(key: string): T | null { try { const item = localStorage.getItem(key); return item ? (JSON.parse(item) as T) : null; } catch (err) { console.error("Error getting item from local storage: ", err); return null; } } /** * @throws Error if item is not found */ removeItem(key: string) { localStorage.removeItem(key); } /** * @Throws Error if localstorage API is not available */ clear() { localStorage.clear(); } }