-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathlocalStorageHelpers.ts
More file actions
42 lines (39 loc) · 1.28 KB
/
Copy pathlocalStorageHelpers.ts
File metadata and controls
42 lines (39 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { LocalStorageItem } from "./coreTypes";
import EncryptionService from "./encryption";
import { getSecurePrefix } from "./utils";
const KEY_PREFIX = getSecurePrefix();
/**
* Function to preload all the local storage data
* @returns
*/
const getAllLocalStorageItems = (storage: Storage) => {
const localStorageItems: LocalStorageItem = {};
if (typeof window !== "undefined") {
const encrypt = new EncryptionService();
for (const [key, value] of Object.entries(storage)) {
if (key.startsWith(KEY_PREFIX)) {
let keyType = key.replace(KEY_PREFIX, "")[0];
let parsedKey = key.replace(/[.][bjns][.]/, ".");
let decryptedValue = encrypt.decrypt(value);
let parsedValue = null;
if (decryptedValue != null)
switch (keyType) {
case "b":
parsedValue = decryptedValue === "true";
break;
case "j":
parsedValue = JSON.parse(decryptedValue);
break;
case "n":
parsedValue = Number(decryptedValue);
break;
default:
parsedValue = decryptedValue;
}
localStorageItems[parsedKey] = parsedValue;
}
}
}
return localStorageItems;
};
export default getAllLocalStorageItems;