blob: dda0ae129815b680a7f3596d34abf4c636622fd4 [file] [log] [blame]
Adrià Vilanova Martínez5bdc4732022-05-31 20:12:21 +02001declare global {
2 interface Window {
3 extCustomStorage: any;
4 }
5}
6
Adrià Vilanova Martínez86fda492022-05-31 15:05:21 +02007export default class ExtSessionStorage {
Adrià Vilanova Martínez5bdc4732022-05-31 20:12:21 +02008 static set(items: any): Promise<void> {
Adrià Vilanova Martínez86fda492022-05-31 15:05:21 +02009 return new Promise((res, rej) => {
10 if (window.extCustomStorage === undefined) window.extCustomStorage = {};
11
12 for (const [key, value] of Object.entries(items))
13 window.extCustomStorage[key] = value;
14
15 res();
16 });
17 }
18
Adrià Vilanova Martínez5bdc4732022-05-31 20:12:21 +020019 static get(keys: string|Array<string>|undefined): Promise<any> {
Adrià Vilanova Martínez86fda492022-05-31 15:05:21 +020020 return new Promise((res, rej) => {
21 if (window.extCustomStorage === undefined) window.extCustomStorage = {};
22
23 if (keys === undefined) {
24 res(window.extCustomStorage);
25 return;
26 }
27
28 if (typeof keys === 'string') {
29 const key = keys;
30 keys = [key];
31 }
32
33 if (Array.isArray(keys)) {
Adrià Vilanova Martínez5bdc4732022-05-31 20:12:21 +020034 let returnObject: any = {};
Adrià Vilanova Martínez86fda492022-05-31 15:05:21 +020035 for (const key of keys) {
36 returnObject[key] = window.extCustomStorage[key];
37 }
38 res(returnObject);
39 return;
40 }
41
42 rej(new Error(
43 'The keys passed are not a valid type ' +
44 '(undefined, string or array).'));
45 });
46 }
47}