Skip to content

Commit

Permalink
chore(frontend/utils): migrate safe_storage to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
JoltCode committed Aug 20, 2024
1 parent 34f63c6 commit e1287d5
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
* Wraps localStorage.getItem in a try/catch. Return null
* if the key does not exist or localStorage fails.
*/
function getItem(key: string): ?string {
function getItem(key: string) {
try {
return localStorage.getItem(key) || null;
const gotKey = localStorage.getItem(key);
if (gotKey === null) {
return null;
}
return JSON.parse(gotKey) as unknown;
} catch (err) {
console.warn('Could not read from localStorage.');
return null;
Expand All @@ -18,9 +22,9 @@ function getItem(key: string): ?string {
/**
* Wraps localStorage.setItem in a try/catch.
*/
function setItem(key: string, value: string): void {
function setItem(key: string, value: unknown): void {
try {
localStorage.setItem(key, value);
localStorage.setItem(key, JSON.stringify(value));
} catch (err) {
console.warn('Could not write to localStorage.');
}
Expand Down

0 comments on commit e1287d5

Please sign in to comment.