Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
Set Cookie Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinabgroww committed Apr 22, 2024
1 parent 8a2929c commit f6fc85a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
29 changes: 29 additions & 0 deletions packages/web-storage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 25 additions & 5 deletions packages/web-storage/test/localStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down

0 comments on commit f6fc85a

Please sign in to comment.