webext-storage 0.0.0 → 1.1.0
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.
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
/// <reference types="chrome" />
|
|
2
|
-
export type StorageItemOptions = {
|
|
2
|
+
export type StorageItemOptions<T> = {
|
|
3
3
|
area?: chrome.storage.AreaName;
|
|
4
|
+
defaultValue?: T;
|
|
4
5
|
};
|
|
5
|
-
export declare class StorageItem<
|
|
6
|
+
export declare class StorageItem<Base, InferredBase extends (Base | undefined) = Base | undefined, Return = InferredBase extends undefined ? Base | undefined : InferredBase> {
|
|
6
7
|
readonly key: string;
|
|
7
8
|
readonly area: chrome.storage.AreaName;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
readonly defaultValue?: InferredBase;
|
|
10
|
+
constructor(key: string, { area, defaultValue, }?: StorageItemOptions<NonNullable<InferredBase>>);
|
|
11
|
+
get(): Promise<Return>;
|
|
12
|
+
set(value: NonNullable<InferredBase>): Promise<void>;
|
|
13
|
+
remove(): Promise<void>;
|
|
14
|
+
onChange(callback: (value: NonNullable<InferredBase>) => void, signal?: AbortSignal): void;
|
|
12
15
|
}
|
|
@@ -2,14 +2,16 @@ import chromeP from 'webext-polyfill-kinda';
|
|
|
2
2
|
export class StorageItem {
|
|
3
3
|
key;
|
|
4
4
|
area;
|
|
5
|
-
|
|
5
|
+
defaultValue;
|
|
6
|
+
constructor(key, { area = 'local', defaultValue, } = {}) {
|
|
6
7
|
this.key = key;
|
|
7
8
|
this.area = area;
|
|
9
|
+
this.defaultValue = defaultValue;
|
|
8
10
|
}
|
|
9
11
|
async get() {
|
|
10
12
|
const result = await chromeP.storage[this.area].get(this.key);
|
|
11
13
|
if (!Object.hasOwn(result, this.key)) {
|
|
12
|
-
return;
|
|
14
|
+
return this.defaultValue;
|
|
13
15
|
}
|
|
14
16
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return -- Assumes the user never uses the Storage API directly
|
|
15
17
|
return result[this.key];
|
|
@@ -17,9 +19,11 @@ export class StorageItem {
|
|
|
17
19
|
async set(value) {
|
|
18
20
|
await chromeP.storage[this.area].set({ [this.key]: value });
|
|
19
21
|
}
|
|
22
|
+
async remove() {
|
|
23
|
+
await chromeP.storage[this.area].remove(this.key);
|
|
24
|
+
}
|
|
20
25
|
onChange(callback, signal) {
|
|
21
26
|
const changeHandler = (changes, area) => {
|
|
22
|
-
console.log('changeHandler', changes, area);
|
|
23
27
|
const changedItem = changes[this.key];
|
|
24
28
|
if (area === this.area && changedItem) {
|
|
25
29
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- Assumes the user never uses the Storage API directly
|
|
@@ -29,6 +33,8 @@ export class StorageItem {
|
|
|
29
33
|
chrome.storage.onChanged.addListener(changeHandler);
|
|
30
34
|
signal?.addEventListener('abort', () => {
|
|
31
35
|
chrome.storage.onChanged.removeListener(changeHandler);
|
|
32
|
-
}, {
|
|
36
|
+
}, {
|
|
37
|
+
once: true,
|
|
38
|
+
});
|
|
33
39
|
}
|
|
34
40
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webext-storage",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A more usable typed storage API for Web Extensions",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"browser",
|
|
@@ -25,7 +25,8 @@
|
|
|
25
25
|
"main": "./distribution/storage-item.js",
|
|
26
26
|
"types": "./distribution/storage-item.d.ts",
|
|
27
27
|
"files": [
|
|
28
|
-
"distribution"
|
|
28
|
+
"distribution/storage-item.js",
|
|
29
|
+
"distribution/storage-item.d.ts"
|
|
29
30
|
],
|
|
30
31
|
"scripts": {
|
|
31
32
|
"build": "tsc",
|
|
@@ -51,7 +52,7 @@
|
|
|
51
52
|
"@types/sinon-chrome": "^2.2.13",
|
|
52
53
|
"jest-chrome": "^0.8.0",
|
|
53
54
|
"sinon-chrome": "^3.0.1",
|
|
54
|
-
"tsd": "^0.
|
|
55
|
+
"tsd": "^0.29.0",
|
|
55
56
|
"typescript": "^5.2.2",
|
|
56
57
|
"vitest": "^0.34.6",
|
|
57
58
|
"xo": "^0.56.0"
|
package/readme.md
CHANGED
|
@@ -5,6 +5,13 @@
|
|
|
5
5
|
|
|
6
6
|
> A more usable typed storage API for Web Extensions
|
|
7
7
|
|
|
8
|
+
- Browsers: Chrome, Firefox, and Safari
|
|
9
|
+
- Manifest: v2 and v3
|
|
10
|
+
- Permissions: `storage` or `unlimitedStorage`
|
|
11
|
+
- Context: They can be called from any context
|
|
12
|
+
|
|
13
|
+
**Sponsored by [PixieBrix](https://www.pixiebrix.com)** :tada:
|
|
14
|
+
|
|
8
15
|
## Install
|
|
9
16
|
|
|
10
17
|
```sh
|
|
@@ -19,6 +26,8 @@ Or download the [standalone bundle](https://bundle.fregante.com/?pkg=webext-stor
|
|
|
19
26
|
import {StorageItem} from "webext-storage";
|
|
20
27
|
|
|
21
28
|
const username = new StorageItem<string>('username')
|
|
29
|
+
// Or
|
|
30
|
+
const username = new StorageItem('username', {defaultValue: 'admin'})
|
|
22
31
|
|
|
23
32
|
await username.set('Ugo');
|
|
24
33
|
// Promise<void>
|
|
@@ -26,6 +35,9 @@ await username.set('Ugo');
|
|
|
26
35
|
await username.get();
|
|
27
36
|
// Promise<string>
|
|
28
37
|
|
|
38
|
+
await username.remove();
|
|
39
|
+
// Promise<void>
|
|
40
|
+
|
|
29
41
|
await username.set({name: 'Ugo'});
|
|
30
42
|
// TypeScript Error: Argument of type '{ name: string; }' is not assignable to parameter of type 'string'.
|
|
31
43
|
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-new -- Type tests only */
|
|
2
|
-
import { expectType, expectNotAssignable, expectAssignable } from 'tsd';
|
|
3
|
-
import { StorageItem } from './storage-item.js';
|
|
4
|
-
new StorageItem('key', { area: 'local' });
|
|
5
|
-
new StorageItem('key', { area: 'sync' });
|
|
6
|
-
const unknownItem = new StorageItem('key');
|
|
7
|
-
expectAssignable(unknownItem.get());
|
|
8
|
-
const objectItem = new StorageItem('key');
|
|
9
|
-
expectType(objectItem.get());
|
|
10
|
-
expectType(objectItem.set({ name: 'new name' }));
|
|
11
|
-
const stringItem = new StorageItem('key');
|
|
12
|
-
expectAssignable(stringItem.get());
|
|
13
|
-
expectNotAssignable(stringItem.get());
|
|
14
|
-
expectType(stringItem.get());
|
|
15
|
-
expectType(stringItem.set('some string'));
|
|
16
|
-
// @ts-expect-error Type is string
|
|
17
|
-
await stringItem.set(1);
|
|
18
|
-
// @ts-expect-error Type is string
|
|
19
|
-
await stringItem.set(true);
|
|
20
|
-
// @ts-expect-error Type is string
|
|
21
|
-
await stringItem.set([true, 'string']);
|
|
22
|
-
// @ts-expect-error Type is string
|
|
23
|
-
await stringItem.set({ wow: [true, 'string'] });
|
|
24
|
-
// @ts-expect-error Type is string
|
|
25
|
-
await stringItem.set(1, { days: 1 });
|