This commit is contained in:
+11
-12
@@ -1,26 +1,25 @@
|
||||
const STORAGE_KEY = 'stb:identity'
|
||||
const STORAGE_KEY = 'stb:identity';
|
||||
|
||||
export interface Identity {
|
||||
name: string
|
||||
name: string;
|
||||
}
|
||||
|
||||
type StorageLike = Pick<Storage, 'getItem' | 'setItem'>
|
||||
type StorageLike = Pick<Storage, 'getItem' | 'setItem'>;
|
||||
|
||||
export function readIdentity(storage: StorageLike): Identity | null {
|
||||
const raw = storage.getItem(STORAGE_KEY)
|
||||
if (!raw) return null
|
||||
const raw = storage.getItem(STORAGE_KEY);
|
||||
if (!raw) return null;
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(raw)
|
||||
if (typeof parsed?.name !== 'string' || !parsed.name.trim()) return null
|
||||
const parsed = JSON.parse(raw);
|
||||
if (typeof parsed?.name !== 'string' || !parsed.name.trim()) return null;
|
||||
|
||||
return { name: parsed.name }
|
||||
}
|
||||
catch {
|
||||
return null
|
||||
return { name: parsed.name };
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function writeIdentity(storage: StorageLike, identity: Identity): void {
|
||||
storage.setItem(STORAGE_KEY, JSON.stringify(identity))
|
||||
storage.setItem(STORAGE_KEY, JSON.stringify(identity));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user