Adrià Vilanova Martínez | 5bdc473 | 2022-05-31 20:12:21 +0200 | [diff] [blame] | 1 | declare global { |
| 2 | interface Window { |
| 3 | extCustomStorage: any; |
| 4 | } |
| 5 | } |
| 6 | |
Adrià Vilanova Martínez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 7 | export default class ExtSessionStorage { |
Adrià Vilanova Martínez | 5bdc473 | 2022-05-31 20:12:21 +0200 | [diff] [blame] | 8 | static set(items: any): Promise<void> { |
Adrià Vilanova Martínez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 9 | 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ínez | 5bdc473 | 2022-05-31 20:12:21 +0200 | [diff] [blame] | 19 | static get(keys: string|Array<string>|undefined): Promise<any> { |
Adrià Vilanova Martínez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 20 | 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ínez | 5bdc473 | 2022-05-31 20:12:21 +0200 | [diff] [blame] | 34 | let returnObject: any = {}; |
Adrià Vilanova Martínez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 35 | 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 | } |