webext-storage 1.3.1 → 2.0.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.
@@ -12,10 +12,11 @@ Return = Base | undefined> {
12
12
  readonly defaultValue?: Return;
13
13
  constructor(key: string, { area, defaultValue, }?: StorageItemMapOptions<Exclude<Return, undefined>>);
14
14
  has: (secondaryKey: string) => Promise<boolean>;
15
- delete: (secondaryKey: string) => Promise<void>;
16
15
  get: (secondaryKey: string) => Promise<Return>;
17
16
  set: (secondaryKey: string, value: Exclude<Return, undefined>) => Promise<void>;
18
17
  remove: (secondaryKey: string) => Promise<void>;
18
+ /** @deprecated Only here to match the Map API; use `remove` instead */
19
+ delete: (secondaryKey: string) => Promise<void>;
19
20
  onChanged(callback: (key: string, value: Exclude<Return, undefined>) => void, signal?: AbortSignal): void;
20
21
  private getRawStorageKey;
21
22
  private getSecondaryStorageKey;
@@ -11,11 +11,7 @@ export class StorageItemMap {
11
11
  has = async (secondaryKey) => {
12
12
  const rawStorageKey = this.getRawStorageKey(secondaryKey);
13
13
  const result = await chromeP.storage[this.areaName].get(rawStorageKey);
14
- return Object.hasOwn(result, secondaryKey);
15
- };
16
- delete = async (secondaryKey) => {
17
- const rawStorageKey = this.getRawStorageKey(secondaryKey);
18
- await chromeP.storage[this.areaName].remove(rawStorageKey);
14
+ return Object.hasOwn(result, rawStorageKey);
19
15
  };
20
16
  get = async (secondaryKey) => {
21
17
  const rawStorageKey = this.getRawStorageKey(secondaryKey);
@@ -28,12 +24,21 @@ export class StorageItemMap {
28
24
  };
29
25
  set = async (secondaryKey, value) => {
30
26
  const rawStorageKey = this.getRawStorageKey(secondaryKey);
31
- await chromeP.storage[this.areaName].set({ [rawStorageKey]: value });
27
+ // eslint-disable-next-line unicorn/prefer-ternary -- ur rong
28
+ if (value === undefined) {
29
+ await chromeP.storage[this.areaName].remove(rawStorageKey);
30
+ }
31
+ else {
32
+ await chromeP.storage[this.areaName].set({ [rawStorageKey]: value });
33
+ }
32
34
  };
33
35
  remove = async (secondaryKey) => {
34
36
  const rawStorageKey = this.getRawStorageKey(secondaryKey);
35
37
  await chromeP.storage[this.areaName].remove(rawStorageKey);
36
38
  };
39
+ /** @deprecated Only here to match the Map API; use `remove` instead */
40
+ // eslint-disable-next-line @typescript-eslint/member-ordering -- invalid
41
+ delete = this.remove;
37
42
  onChanged(callback, signal) {
38
43
  const changeHandler = (changes, area) => {
39
44
  if (area !== this.areaName) {
@@ -15,6 +15,7 @@ Return = Base | undefined> {
15
15
  constructor(key: string, { area, defaultValue, }?: StorageItemOptions<Exclude<Return, undefined>>);
16
16
  get: () => Promise<Return>;
17
17
  set: (value: Exclude<Return, undefined>) => Promise<void>;
18
+ has: () => Promise<boolean>;
18
19
  remove: () => Promise<void>;
19
20
  onChanged(callback: (value: Exclude<Return, undefined>) => void, signal?: AbortSignal): void;
20
21
  }
@@ -19,7 +19,17 @@ export class StorageItem {
19
19
  return result[this.key];
20
20
  };
21
21
  set = async (value) => {
22
- await chromeP.storage[this.area].set({ [this.key]: value });
22
+ // eslint-disable-next-line unicorn/prefer-ternary -- ur rong
23
+ if (value === undefined) {
24
+ await chromeP.storage[this.area].remove(this.key);
25
+ }
26
+ else {
27
+ await chromeP.storage[this.area].set({ [this.key]: value });
28
+ }
29
+ };
30
+ has = async () => {
31
+ const result = await chromeP.storage[this.area].get(this.key);
32
+ return Object.hasOwn(result, this.key);
23
33
  };
24
34
  remove = async () => {
25
35
  await chromeP.storage[this.area].remove(this.key);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webext-storage",
3
- "version": "1.3.1",
3
+ "version": "2.0.0",
4
4
  "description": "A more usable typed storage API for Web Extensions",
5
5
  "keywords": [
6
6
  "browser",
package/readme.md CHANGED
@@ -24,6 +24,7 @@ options.onChanged(newValue => {
24
24
  - `item.get()` returns the raw value instead of an object
25
25
  - Every `get` and `set` operation is type-safe
26
26
  - If you provide a `defaultValue`, the return type will not be ` | undefined`
27
+ - Calling `.set(undefined)` will unset the value instead of the call being ignored
27
28
  - The `onChanged` example speaks for itself
28
29
 
29
30
  Now compare it to the native API: