@wxt-dev/storage 1.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.
package/dist/index.d.mts CHANGED
@@ -103,6 +103,10 @@ interface WxtStorage {
103
103
  item: WxtStorageItem<any, any>;
104
104
  options?: RemoveItemOptions;
105
105
  }>): Promise<void>;
106
+ /**
107
+ * Removes all items from the provided storage area.
108
+ */
109
+ clear(base: StorageArea): Promise<void>;
106
110
  /**
107
111
  * Remove the entire metadata for a key, or specific properties by name.
108
112
  *
@@ -134,7 +138,7 @@ interface WxtStorage {
134
138
  /**
135
139
  * Define a storage item with a default value, type, or versioning.
136
140
  *
137
- * Read full docs: https://wxt.dev/guide/extension-apis/storage.html#defining-storage-items
141
+ * Read full docs: https://wxt.dev/storage.html#defining-storage-items
138
142
  */
139
143
  defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(key: StorageItemKey): WxtStorageItem<TValue | null, TMetadata>;
140
144
  defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(key: StorageItemKey, options: WxtStorageItemOptions<TValue>): WxtStorageItem<TValue, TMetadata>;
package/dist/index.d.ts CHANGED
@@ -103,6 +103,10 @@ interface WxtStorage {
103
103
  item: WxtStorageItem<any, any>;
104
104
  options?: RemoveItemOptions;
105
105
  }>): Promise<void>;
106
+ /**
107
+ * Removes all items from the provided storage area.
108
+ */
109
+ clear(base: StorageArea): Promise<void>;
106
110
  /**
107
111
  * Remove the entire metadata for a key, or specific properties by name.
108
112
  *
@@ -134,7 +138,7 @@ interface WxtStorage {
134
138
  /**
135
139
  * Define a storage item with a default value, type, or versioning.
136
140
  *
137
- * Read full docs: https://wxt.dev/guide/extension-apis/storage.html#defining-storage-items
141
+ * Read full docs: https://wxt.dev/storage.html#defining-storage-items
138
142
  */
139
143
  defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(key: StorageItemKey): WxtStorageItem<TValue | null, TMetadata>;
140
144
  defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(key: StorageItemKey, options: WxtStorageItemOptions<TValue>): WxtStorageItem<TValue, TMetadata>;
package/dist/index.mjs CHANGED
@@ -1,6 +1,13 @@
1
1
  import { dequal } from 'dequal/lite';
2
2
  import { Mutex } from 'async-mutex';
3
3
 
4
+ const browser = (
5
+ // @ts-expect-error
6
+ globalThis.browser?.runtime?.id == null ? globalThis.chrome : (
7
+ // @ts-expect-error
8
+ globalThis.browser
9
+ )
10
+ );
4
11
  const storage = createStorage();
5
12
  function createStorage() {
6
13
  const drivers = {
@@ -152,7 +159,7 @@ function createStorage() {
152
159
  const resultsMap = {};
153
160
  await Promise.all(
154
161
  Object.entries(areaToDriverMetaKeysMap).map(async ([area, keys2]) => {
155
- const areaRes = await chrome.storage[area].get(
162
+ const areaRes = await browser.storage[area].get(
156
163
  keys2.map((key) => key.driverMetaKey)
157
164
  );
158
165
  keys2.forEach((key) => {
@@ -260,6 +267,10 @@ function createStorage() {
260
267
  })
261
268
  );
262
269
  },
270
+ clear: async (base) => {
271
+ const driver = getDriver(base);
272
+ await driver.clear();
273
+ },
263
274
  removeMeta: async (key, properties) => {
264
275
  const { driver, driverKey } = resolveKey(key);
265
276
  await removeMeta(driver, driverKey, properties);
@@ -400,7 +411,7 @@ function createStorage() {
400
411
  }
401
412
  function createDriver(storageArea) {
402
413
  const getStorageArea = () => {
403
- if (chrome.runtime == null) {
414
+ if (browser.runtime == null) {
404
415
  throw Error(
405
416
  [
406
417
  "'wxt/storage' must be loaded in a web extension environment",
@@ -409,14 +420,14 @@ function createDriver(storageArea) {
409
420
  ].join("\n")
410
421
  );
411
422
  }
412
- if (chrome.storage == null) {
423
+ if (browser.storage == null) {
413
424
  throw Error(
414
425
  "You must add the 'storage' permission to your manifest to use 'wxt/storage'"
415
426
  );
416
427
  }
417
- const area = chrome.storage[storageArea];
428
+ const area = browser.storage[storageArea];
418
429
  if (area == null)
419
- throw Error(`"chrome.storage.${storageArea}" is undefined`);
430
+ throw Error(`"browser.storage.${storageArea}" is undefined`);
420
431
  return area;
421
432
  };
422
433
  const watchListeners = /* @__PURE__ */ new Set();
@@ -452,6 +463,9 @@ function createDriver(storageArea) {
452
463
  removeItems: async (keys) => {
453
464
  await getStorageArea().remove(keys);
454
465
  },
466
+ clear: async () => {
467
+ await getStorageArea().clear();
468
+ },
455
469
  snapshot: async () => {
456
470
  return await getStorageArea().get();
457
471
  },
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wxt-dev/storage",
3
3
  "description": "Web extension storage API provided by WXT, supports all browsers.",
4
- "version": "1.0.0",
4
+ "version": "1.1.0",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "type": "git",