@webext-core/storage 1.1.1 → 1.1.3

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # `@webext-core/storage`
2
2
 
3
- A type-safe wrapper around the `Browser.storage` APIs, based off local storage.
3
+ A type-safe, localStorage-esk wrapper around the web extension storage APIs. Supports all browsers (Chrome, Firefox, Safari, etc).
4
4
 
5
5
  ```bash
6
6
  pnpm i @webext-core/storage
@@ -14,4 +14,4 @@ const value = await localExtStorage.getItem('some-key');
14
14
 
15
15
  ## Get Started
16
16
 
17
- See [documentation](https://webext-core.aklinker1.io/storage) to get started!
17
+ See [documentation](https://webext-core.aklinker1.io/guide/storage/) to get started!
package/lib/index.cjs CHANGED
@@ -56,10 +56,7 @@ module.exports = __toCommonJS(src_exports);
56
56
  // src/defineExtensionStorage.ts
57
57
  var import_webextension_polyfill = __toESM(require("webextension-polyfill"), 1);
58
58
  function defineExtensionStorage(storage) {
59
- const area = getStorageName(storage);
60
- const onStorageChanged = (changes, changedArea) => __async(this, null, function* () {
61
- if (area !== changedArea)
62
- return;
59
+ const onStorageChanged = (changes) => __async(this, null, function* () {
63
60
  const work = listeners.map(({ key, cb }) => {
64
61
  if (!(key in changes))
65
62
  return;
@@ -73,7 +70,7 @@ function defineExtensionStorage(storage) {
73
70
  let listeners = [];
74
71
  function addListener(listener) {
75
72
  if (listeners.length === 0) {
76
- import_webextension_polyfill.default.storage.onChanged.addListener(onStorageChanged);
73
+ storage.onChanged.addListener(onStorageChanged);
77
74
  }
78
75
  listeners.push(listener);
79
76
  }
@@ -86,7 +83,9 @@ function defineExtensionStorage(storage) {
86
83
  }
87
84
  }
88
85
  return {
89
- clear: storage.clear,
86
+ clear() {
87
+ return storage.clear();
88
+ },
90
89
  getItem(key) {
91
90
  return storage.get(key).then((res) => {
92
91
  var _a;
@@ -109,19 +108,6 @@ function defineExtensionStorage(storage) {
109
108
  }
110
109
  };
111
110
  }
112
- function getStorageName(storage) {
113
- switch (storage) {
114
- case import_webextension_polyfill.default.storage.local:
115
- return "local";
116
- case import_webextension_polyfill.default.storage.sync:
117
- return "sync";
118
- case import_webextension_polyfill.default.storage.managed:
119
- return "managed";
120
- }
121
- throw Error(
122
- "Unsupported storage area. local, sync, and managed are the only supporte storage areas."
123
- );
124
- }
125
111
 
126
112
  // src/index.ts
127
113
  var import_webextension_polyfill2 = __toESM(require("webextension-polyfill"), 1);
package/lib/index.d.ts CHANGED
@@ -6,6 +6,12 @@ declare type AnySchema = Record<string, any>;
6
6
  */
7
7
  declare type RemoveListenerCallback = () => void;
8
8
  declare type OnChangeCallback<TSchema extends AnySchema, TKey extends keyof TSchema = keyof TSchema> = (newValue: TSchema[TKey], oldValue: TSchema[TKey] | null) => void;
9
+ /**
10
+ * This is the interface for the storage objects exported from the package. It is similar to `localStorage`, except for a few differences:
11
+ *
12
+ * - ***It's async*** since the web extension storage APIs are async.
13
+ * - It can store any data type, ***not just strings***.
14
+ */
9
15
  interface ExtensionStorage<TSchema extends AnySchema> {
10
16
  /**
11
17
  * Clear all values from storage.
@@ -34,15 +40,33 @@ interface ExtensionStorage<TSchema extends AnySchema> {
34
40
  }
35
41
 
36
42
  /**
37
- * Create a storage instance with an optional type schema.
43
+ * Create a storage instance with an optional schema, `TSchema`, for type safety.
44
+ *
45
+ * @param storage The storage to to use. Either `Browser.storage.local`, `Browser.storage.sync`, or `Browser.storage.managed`.
46
+ *
47
+ * @example
48
+ * import browser from 'webextension-polyfill';
49
+ *
50
+ * interface Schema {
51
+ * installDate: number;
52
+ * }
53
+ * const extensionStorage = defineExtensionStorage<Schema>(browser.storage.local);
38
54
  *
39
- * @arg storage Either `Browser.storage.local`, `Browser.storage.sync`, or
40
- * `Browser.storage.managed`. This is the storage that will back the implementation.
55
+ * const date = await extensionStorage.getItem("installDate");
41
56
  */
42
57
  declare function defineExtensionStorage<TSchema extends AnySchema = AnySchema>(storage: Storage.StorageArea): ExtensionStorage<TSchema>;
43
58
 
59
+ /**
60
+ * An implementation of `ExtensionStorage` based on the `browser.storage.local` storage area.
61
+ */
44
62
  declare const localExtStorage: ExtensionStorage<AnySchema>;
63
+ /**
64
+ * An implementation of `ExtensionStorage` based on the `browser.storage.sync` storage area.
65
+ */
45
66
  declare const syncExtStorage: ExtensionStorage<AnySchema>;
67
+ /**
68
+ * An implementation of `ExtensionStorage` based on the `browser.storage.managed` storage area.
69
+ */
46
70
  declare const managedExtStorage: ExtensionStorage<AnySchema>;
47
71
 
48
72
  export { ExtensionStorage, defineExtensionStorage, localExtStorage, managedExtStorage, syncExtStorage };
@@ -1054,10 +1054,7 @@ var webExtCoreStorage = (() => {
1054
1054
  // src/defineExtensionStorage.ts
1055
1055
  var import_webextension_polyfill = __toESM(require_browser_polyfill(), 1);
1056
1056
  function defineExtensionStorage(storage) {
1057
- const area = getStorageName(storage);
1058
- const onStorageChanged = (changes, changedArea) => __async(this, null, function* () {
1059
- if (area !== changedArea)
1060
- return;
1057
+ const onStorageChanged = (changes) => __async(this, null, function* () {
1061
1058
  const work = listeners.map(({ key, cb }) => {
1062
1059
  if (!(key in changes))
1063
1060
  return;
@@ -1071,7 +1068,7 @@ var webExtCoreStorage = (() => {
1071
1068
  let listeners = [];
1072
1069
  function addListener(listener) {
1073
1070
  if (listeners.length === 0) {
1074
- import_webextension_polyfill.default.storage.onChanged.addListener(onStorageChanged);
1071
+ storage.onChanged.addListener(onStorageChanged);
1075
1072
  }
1076
1073
  listeners.push(listener);
1077
1074
  }
@@ -1084,7 +1081,9 @@ var webExtCoreStorage = (() => {
1084
1081
  }
1085
1082
  }
1086
1083
  return {
1087
- clear: storage.clear,
1084
+ clear() {
1085
+ return storage.clear();
1086
+ },
1088
1087
  getItem(key) {
1089
1088
  return storage.get(key).then((res) => {
1090
1089
  var _a;
@@ -1107,19 +1106,6 @@ var webExtCoreStorage = (() => {
1107
1106
  }
1108
1107
  };
1109
1108
  }
1110
- function getStorageName(storage) {
1111
- switch (storage) {
1112
- case import_webextension_polyfill.default.storage.local:
1113
- return "local";
1114
- case import_webextension_polyfill.default.storage.sync:
1115
- return "sync";
1116
- case import_webextension_polyfill.default.storage.managed:
1117
- return "managed";
1118
- }
1119
- throw Error(
1120
- "Unsupported storage area. local, sync, and managed are the only supporte storage areas."
1121
- );
1122
- }
1123
1109
 
1124
1110
  // src/index.ts
1125
1111
  var import_webextension_polyfill2 = __toESM(require_browser_polyfill(), 1);
package/lib/index.js CHANGED
@@ -22,10 +22,7 @@ var __async = (__this, __arguments, generator) => {
22
22
  // src/defineExtensionStorage.ts
23
23
  import browser from "webextension-polyfill";
24
24
  function defineExtensionStorage(storage) {
25
- const area = getStorageName(storage);
26
- const onStorageChanged = (changes, changedArea) => __async(this, null, function* () {
27
- if (area !== changedArea)
28
- return;
25
+ const onStorageChanged = (changes) => __async(this, null, function* () {
29
26
  const work = listeners.map(({ key, cb }) => {
30
27
  if (!(key in changes))
31
28
  return;
@@ -39,7 +36,7 @@ function defineExtensionStorage(storage) {
39
36
  let listeners = [];
40
37
  function addListener(listener) {
41
38
  if (listeners.length === 0) {
42
- browser.storage.onChanged.addListener(onStorageChanged);
39
+ storage.onChanged.addListener(onStorageChanged);
43
40
  }
44
41
  listeners.push(listener);
45
42
  }
@@ -52,7 +49,9 @@ function defineExtensionStorage(storage) {
52
49
  }
53
50
  }
54
51
  return {
55
- clear: storage.clear,
52
+ clear() {
53
+ return storage.clear();
54
+ },
56
55
  getItem(key) {
57
56
  return storage.get(key).then((res) => {
58
57
  var _a;
@@ -75,19 +74,6 @@ function defineExtensionStorage(storage) {
75
74
  }
76
75
  };
77
76
  }
78
- function getStorageName(storage) {
79
- switch (storage) {
80
- case browser.storage.local:
81
- return "local";
82
- case browser.storage.sync:
83
- return "sync";
84
- case browser.storage.managed:
85
- return "managed";
86
- }
87
- throw Error(
88
- "Unsupported storage area. local, sync, and managed are the only supporte storage areas."
89
- );
90
- }
91
77
 
92
78
  // src/index.ts
93
79
  import Browser from "webextension-polyfill";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@webext-core/storage",
3
- "version": "1.1.1",
4
- "description": "Local storage based wrapper around the Browser.storage APIs",
3
+ "version": "1.1.3",
4
+ "description": "A type-safe, localStorage-esk wrapper around the web extension storage APIs. Supports all browsers (Chrome, Firefox, Safari, etc)",
5
5
  "license": "MIT",
6
6
  "keywords": [
7
7
  "web-extension",
@@ -9,12 +9,12 @@
9
9
  "chrome-extension",
10
10
  "webext",
11
11
  "web-ext",
12
- "web",
13
12
  "chrome",
13
+ "firefox",
14
+ "safari",
14
15
  "browser",
15
16
  "extension",
16
- "storage",
17
- "webextension-polyfill"
17
+ "storage"
18
18
  ],
19
19
  "homepage": "https://github.com/aklinker1/webext-core/tree/main/packages/storage",
20
20
  "repository": {
@@ -47,7 +47,7 @@
47
47
  "tsup": "^6.4.0",
48
48
  "typescript": "^4.8.4",
49
49
  "vitest": "^0.24.5",
50
- "@webext-core/fake-browser": "1.0.3",
50
+ "@webext-core/fake-browser": "1.2.2",
51
51
  "tsconfig": "0.0.0"
52
52
  },
53
53
  "scripts": {