Files
InvoiceShelf/resources/scripts-v2/utils/local-storage.ts
Darko Gjorgjijoski 991b716b33 Phase 1: TypeScript foundation in scripts-v2/
Create the complete TypeScript foundation for the Vue 3 migration
in a parallel scripts-v2/ directory. 72 files, 5430 lines, zero
any types, strict mode.

- types/ (21 files): Domain interfaces for all 17 entities derived
  from actual Laravel models and API resources. Enums for all
  statuses. Generic API response wrappers.
- api/ (29 files): Typed axios client with interceptors, endpoint
  constants from routes/api.php, 25 typed service classes covering
  every API endpoint.
- composables/ (14 files): Vue 3 composition functions for auth,
  notifications, dialogs, modals, pagination, filters, currency,
  dates, theme, sidebar, company context, and permissions.
- utils/ (5 files): Pure typed utilities for money formatting,
  date formatting (date-fns), localStorage, and error handling.
- config/ (3 files): Typed ability constants, app constants.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 05:00:00 +02:00

67 lines
1.5 KiB
TypeScript

/**
* Typed wrapper around localStorage for safe get/set/remove operations.
* Handles JSON serialization and deserialization automatically.
*/
/**
* Retrieve a value from localStorage, parsed from JSON.
*
* @param key - The localStorage key
* @returns The parsed value, or null if the key does not exist or parsing fails
*/
export function get<T>(key: string): T | null {
const raw = localStorage.getItem(key)
if (raw === null) {
return null
}
try {
return JSON.parse(raw) as T
} catch {
// If parsing fails, return the raw string cast to T.
// This handles cases where the value is a plain string not wrapped in quotes.
return raw as unknown as T
}
}
/**
* Store a value in localStorage as JSON.
*
* @param key - The localStorage key
* @param value - The value to store (will be JSON-serialized)
*/
export function set<T>(key: string, value: T): void {
if (typeof value === 'string') {
localStorage.setItem(key, value)
} else {
localStorage.setItem(key, JSON.stringify(value))
}
}
/**
* Remove a key from localStorage.
*
* @param key - The localStorage key to remove
*/
export function remove(key: string): void {
localStorage.removeItem(key)
}
/**
* Check whether a key exists in localStorage.
*
* @param key - The localStorage key
* @returns True if the key exists
*/
export function has(key: string): boolean {
return localStorage.getItem(key) !== null
}
/**
* Clear all entries in localStorage.
*/
export function clear(): void {
localStorage.clear()
}