From 671f2d132633980f9d358130fedee33726f5a278 Mon Sep 17 00:00:00 2001 From: MK Date: Mon, 23 Sep 2024 12:31:48 +0100 Subject: [PATCH] refactor: SafeLocalStorage --- packages/common/src/utils/SafeLocalStorage.ts | 17 ++++++++++++----- packages/common/tests/SafeLocalStorage.test.ts | 4 ++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/common/src/utils/SafeLocalStorage.ts b/packages/common/src/utils/SafeLocalStorage.ts index c0eeda3f04..d5bde64de6 100644 --- a/packages/common/src/utils/SafeLocalStorage.ts +++ b/packages/common/src/utils/SafeLocalStorage.ts @@ -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( @@ -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] } } diff --git a/packages/common/tests/SafeLocalStorage.test.ts b/packages/common/tests/SafeLocalStorage.test.ts index 27ca1928ab..369a70f7c8 100644 --- a/packages/common/tests/SafeLocalStorage.test.ts +++ b/packages/common/tests/SafeLocalStorage.test.ts @@ -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 @@ -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 ', () => {