webext-storage 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/distribution/storage-item.d.ts +5 -5
- package/package.json +1 -1
- package/readme.md +31 -1
|
@@ -3,13 +3,13 @@ export type StorageItemOptions<T> = {
|
|
|
3
3
|
area?: chrome.storage.AreaName;
|
|
4
4
|
defaultValue?: T;
|
|
5
5
|
};
|
|
6
|
-
export declare class StorageItem<Base,
|
|
6
|
+
export declare class StorageItem<Base, Default = Base | undefined, Return = Default extends undefined ? Base : Default> {
|
|
7
7
|
readonly key: string;
|
|
8
8
|
readonly area: chrome.storage.AreaName;
|
|
9
|
-
readonly defaultValue?:
|
|
10
|
-
constructor(key: string, { area, defaultValue, }?: StorageItemOptions<NonNullable<
|
|
9
|
+
readonly defaultValue?: Default;
|
|
10
|
+
constructor(key: string, { area, defaultValue, }?: StorageItemOptions<NonNullable<Default>>);
|
|
11
11
|
get: () => Promise<Return>;
|
|
12
|
-
set: (value: NonNullable<
|
|
12
|
+
set: (value: NonNullable<Default>) => Promise<void>;
|
|
13
13
|
remove: () => Promise<void>;
|
|
14
|
-
onChange(callback: (value: NonNullable<
|
|
14
|
+
onChange(callback: (value: NonNullable<Default>) => void, signal?: AbortSignal): void;
|
|
15
15
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -12,6 +12,36 @@
|
|
|
12
12
|
|
|
13
13
|
**Sponsored by [PixieBrix](https://www.pixiebrix.com)** :tada:
|
|
14
14
|
|
|
15
|
+
`chrome.storage.local.get()` is very inconvenient to use and it does not provide type safety. This module provides a better API:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
// Before
|
|
19
|
+
const storage = await chrome.storage.local.get('user-options');
|
|
20
|
+
const value = storage['user-options']; // The type is `any`
|
|
21
|
+
await chrome.storage.local.set({['user-options']: {color: 'red'}}); // Not type-checked
|
|
22
|
+
chrome.storage.onChanged.addListener((storageArea, change) => {
|
|
23
|
+
if (storageArea === 'local' && change['user-options']) { // Repetitive
|
|
24
|
+
console.log('New options', change['user-options'].newValue)
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// After
|
|
29
|
+
const options = new StorageItem<Record<string, string>>('user-options');
|
|
30
|
+
const value = await options.get(); // The type is `Record<string, string> | undefined`
|
|
31
|
+
await options.set({color: 'red'}) // Type-checked
|
|
32
|
+
options.onChange(newValue => {
|
|
33
|
+
console.log('New options', newValue)
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Why this is better:
|
|
38
|
+
|
|
39
|
+
- The storage item is defined in a single place, including its storageArea, its types and default value
|
|
40
|
+
- `get` only is only `.get()` instead of the awkward post-get object access that
|
|
41
|
+
- Every `get` and `set` operation is type-safe
|
|
42
|
+
- If you provide a `defaultValue`, the return type will not be ` | undefined`
|
|
43
|
+
- The `onChange` example speaks for itself
|
|
44
|
+
|
|
15
45
|
## Install
|
|
16
46
|
|
|
17
47
|
```sh
|
|
@@ -48,7 +78,7 @@ username.onChange(newName => {
|
|
|
48
78
|
|
|
49
79
|
## Related
|
|
50
80
|
|
|
51
|
-
- [webext-storage-cache](https://github.com/fregante/webext-storage-cache) -
|
|
81
|
+
- [webext-storage-cache](https://github.com/fregante/webext-storage-cache) - Cache values in your Web Extension and clear them on expiration.
|
|
52
82
|
- [webext-tools](https://github.com/fregante/webext-tools) - Utility functions for Web Extensions.
|
|
53
83
|
- [webext-content-scripts](https://github.com/fregante/webext-content-scripts) - Utility functions to inject content scripts in WebExtensions.
|
|
54
84
|
- [webext-base-css](https://github.com/fregante/webext-base-css) - Extremely minimal stylesheet/setup for Web Extensions’ options pages (also dark mode)
|