Skip to content

Commit

Permalink
refactor: SafeLocalStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
magiziz committed Sep 23, 2024
1 parent 2a75488 commit 671f2d1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
17 changes: 12 additions & 5 deletions packages/common/src/utils/SafeLocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ export const SafeLocalStorage = {
value: SafeLocalStorageItems[Key]
): void {
if (isSafe()) {
localStorage.setItem(key, JSON.stringify(value))
localStorage.setItem(
key,
key === SafeLocalStorageKeys.DEEPLINK_CHOICE ? JSON.stringify(value) : (value as string)
)
}
},
getItem<Key extends keyof SafeLocalStorageItems>(
Expand All @@ -40,11 +43,15 @@ export const SafeLocalStorage = {
const value = localStorage.getItem(key)

if (value) {
try {
return JSON.parse(value)
} catch {
return undefined
if (key === SafeLocalStorageKeys.DEEPLINK_CHOICE) {
try {
return JSON.parse(value)
} catch {
return undefined
}
}

return value as SafeLocalStorageItems[Key]
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/common/tests/SafeLocalStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('SafeLocalStorage unsafe', () => {
describe('SafeLocalStorage safe', () => {
let getItem = vi.fn(value => {
if (value === '@appkit/wallet_id') {
return JSON.stringify('test')
return 'test'
}

return undefined
Expand All @@ -46,7 +46,7 @@ describe('SafeLocalStorage safe', () => {

it('should setItem', () => {
expect(SafeLocalStorage.setItem('@appkit/wallet_id', 'test')).toBe(undefined)
expect(setItem).toHaveBeenCalledWith('@appkit/wallet_id', JSON.stringify('test'))
expect(setItem).toHaveBeenCalledWith('@appkit/wallet_id', 'test')
})

it('should getItem ', () => {
Expand Down

0 comments on commit 671f2d1

Please sign in to comment.