Files
cz-web/src/utils/storage.ts
2026-10-01 17:24:57 +08:00

34 lines
794 B
TypeScript

const PREFIX = 'cz:'
function parse<T>(raw: string | null): T | null {
if (raw === null) return null
try {
return JSON.parse(raw) as T
}
catch {
return null
}
}
export const storage = {
get<T>(key: string): T | null {
return parse<T>(localStorage.getItem(PREFIX + key))
},
set<T>(key: string, value: T): void {
localStorage.setItem(PREFIX + key, JSON.stringify(value))
},
remove(key: string): void {
localStorage.removeItem(PREFIX + key)
},
sessionGet<T>(key: string): T | null {
return parse<T>(sessionStorage.getItem(PREFIX + key))
},
sessionSet<T>(key: string, value: T): void {
sessionStorage.setItem(PREFIX + key, JSON.stringify(value))
},
sessionRemove(key: string): void {
sessionStorage.removeItem(PREFIX + key)
},
}