forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.ts
More file actions
80 lines (68 loc) · 1.99 KB
/
Copy pathstorage.ts
File metadata and controls
80 lines (68 loc) · 1.99 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* A typed wrapper around localStorage largely borrowed from (but less capable
* than) https://www.npmjs.com/package/typed-local-store
*
* Provides a fully-typed interface to localStorage, and is easy to modify for other storage strategies (i.e. sessionStorage)
*/
/**
* Valid localStorage key names mapped to an arbitrary value of the correct
* type. Used to provide both good typing AND good type-ahead, so that you can
* see a list of valid storage keys while using this module elsewhere.
*/
type Schema = {
walletId: string;
walletAddress: string;
walletNetwork: string;
networkPassphrase: string;
};
/**
* Typed interface that follows the Web Storage API: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API
*
* Implementation has been borrowed and simplified from https://www.npmjs.com/package/typed-local-store
*/
class TypedStorage<T> {
private readonly storage: Storage;
constructor() {
this.storage = localStorage;
}
public get length(): number {
return this.storage?.length;
}
public key<U extends keyof T>(index: number): U {
return this.storage?.key(index) as U;
}
public getItem<U extends keyof T>(
key: U,
retrievalMode: "fail" | "raw" | "safe" = "fail",
): T[U] | null {
const item = this.storage?.getItem(key.toString());
if (item == null) {
return item;
}
try {
return JSON.parse(item) as T[U];
} catch (error) {
switch (retrievalMode) {
case "safe":
return null;
case "raw":
return item as unknown as T[U];
default:
throw error;
}
}
}
public setItem<U extends keyof T>(key: U, value: T[U]): void {
this.storage?.setItem(key.toString(), JSON.stringify(value));
}
public removeItem<U extends keyof T>(key: U): void {
this.storage?.removeItem(key.toString());
}
public clear(): void {
this.storage?.clear();
}
}
/**
* Fully-typed wrapper around localStorage
*/
export default new TypedStorage<Schema>();