From f6fc85a13a15ad4f206bf98007db2a6620977bca Mon Sep 17 00:00:00 2001 From: Abhinab Date: Mon, 22 Apr 2024 12:13:26 +0530 Subject: [PATCH] Set Cookie Updated --- packages/web-storage/src/index.ts | 29 ++++++++++++++++++ .../web-storage/test/localStorage.test.ts | 30 +++++++++++++++---- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/packages/web-storage/src/index.ts b/packages/web-storage/src/index.ts index bffb5838..1abbb473 100644 --- a/packages/web-storage/src/index.ts +++ b/packages/web-storage/src/index.ts @@ -124,6 +124,35 @@ export function setDataToStorage(key : string, data : any, storageType: string, localStorageInstance.set(bucketKey, data, expiresInMin); } } +export function setDataToStorageStrict(key : string, data : any, storageType: string, expiresInMin = DEFAULT_STORAGE_EXPIRY_TIME, bucket = BUCKETS.OTHERS) { + if (checkForErrors() || !localStorageInstance.supported()) { + return ''; + } + + // we dont allow more than 14 days of storage for others bucket. If more than 14 days is stored, we make it to 14 days. + if ((expiresInMin > MAXIMUM_EXPIRY_LIMIT) && (bucket === BUCKETS.OTHERS)) { + expiresInMin = MAXIMUM_EXPIRY_LIMIT; + } + + const bucketKey = getFullKeyForItem(key, bucket); + + const expiresInDay = ((expiresInMin / 60) / 24); + + + if (STORAGE_TYPE.COOKIE === storageType) { + cookie.set(key, data, { expires: expiresInDay, path: '/', secure: true, sameSite:'Strict' }); + + } else if (STORAGE_TYPE.SESSION_STORAGE === storageType) { + sessionStorageInstance.set(key, data); + + } else if (STORAGE_TYPE.LOCAL_COOKIE_STORAGE === storageType) { + localStorageInstance.set(bucketKey, data, expiresInMin); + cookie.set(key, data, { expires: expiresInDay, path: '/', secure: true, sameSite:'Strict' }); + + } else { + localStorageInstance.set(bucketKey, data, expiresInMin); + } +} /** * Clear Particular Key from Storage diff --git a/packages/web-storage/test/localStorage.test.ts b/packages/web-storage/test/localStorage.test.ts index 518c2034..d4f836f0 100644 --- a/packages/web-storage/test/localStorage.test.ts +++ b/packages/web-storage/test/localStorage.test.ts @@ -26,6 +26,13 @@ describe('Local Storage Tests', () => { } catch { testResult = false; } + try { + storage.setDataToStorageStrict(key, value, storage.STORAGE_TYPE_AVAILABLE.LOCAL_STORAGE, 10); + testResult = true; + + } catch { + testResult = false; + } } expect(testResult).toBe(true); @@ -41,16 +48,29 @@ describe('Local Storage Tests', () => { */ it('Get Data from LS', () => { - const key = 'storageLibTest'; - const value = key; + const key1 = 'storageLibTest1'; + const key2 = 'storageLibTest2'; + const value1 = key1; + const value2 = key2; let testResult = false; if (!storage.errorInStorage()) { try { - storage.setDataToStorage(key, value, storage.STORAGE_TYPE_AVAILABLE.LOCAL_STORAGE, 10); - const result = storage.getDataFromStorage(key, storage.STORAGE_TYPE_AVAILABLE.LOCAL_STORAGE); + storage.setDataToStorage(key1, value1, storage.STORAGE_TYPE_AVAILABLE.LOCAL_STORAGE, 10); + const result = storage.getDataFromStorage(key1, storage.STORAGE_TYPE_AVAILABLE.LOCAL_STORAGE); + + if (result === value1) { + testResult = true; + } + + } catch { + testResult = false; + } + try { + storage.setDataToStorageStrict(key2, value2, storage.STORAGE_TYPE_AVAILABLE.LOCAL_STORAGE, 10); + const result = storage.getDataFromStorage(key2, storage.STORAGE_TYPE_AVAILABLE.LOCAL_STORAGE); - if (result === value) { + if (result === value2) { testResult = true; }