Skip to content

Commit

Permalink
feat: Add storage.clear (#1368)
Browse files Browse the repository at this point in the history
Co-authored-by: Aaron <[email protected]>
  • Loading branch information
chengxilo and aklinker1 authored Jan 27, 2025
1 parent ac7b676 commit 20eea2a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/storage/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,18 @@ describe('Storage Utils', () => {
});
});

describe('clear', () => {
it('should remove all items', async () => {
await fakeBrowser.storage[storageArea].set({
one: 1,
two: 2,
});

await storage.clear(storageArea);
expect(await fakeBrowser.storage[storageArea].get()).toEqual({});
});
});

describe('removeMeta', () => {
it('should remove all metadata', async () => {
await fakeBrowser.storage[storageArea].set({ count$: { v: 4 } });
Expand Down
14 changes: 14 additions & 0 deletions packages/storage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ function createStorage(): WxtStorage {
}),
);
},
clear: async (base) => {
const driver = getDriver(base);
await driver.clear();
},
removeMeta: async (key, properties) => {
const { driver, driverKey } = resolveKey(key);
await removeMeta(driver, driverKey, properties);
Expand Down Expand Up @@ -534,6 +538,9 @@ function createDriver(storageArea: StorageArea): WxtStorageDriver {
removeItems: async (keys) => {
await getStorageArea().remove(keys);
},
clear: async () => {
await getStorageArea().clear();
},
snapshot: async () => {
return await getStorageArea().get();
},
Expand Down Expand Up @@ -674,6 +681,12 @@ export interface WxtStorage {
| { item: WxtStorageItem<any, any>; options?: RemoveItemOptions }
>,
): Promise<void>;

/**
* Removes all items from the provided storage area.
*/
clear(base: StorageArea): Promise<void>;

/**
* Remove the entire metadata for a key, or specific properties by name.
*
Expand Down Expand Up @@ -730,6 +743,7 @@ interface WxtStorageDriver {
setItems(values: Array<{ key: string; value: any }>): Promise<void>;
removeItem(key: string): Promise<void>;
removeItems(keys: string[]): Promise<void>;
clear(): Promise<void>;
snapshot(): Promise<Record<string, unknown>>;
restoreSnapshot(data: Record<string, unknown>): Promise<void>;
watch<T>(key: string, cb: WatchCallback<T | null>): Unwatch;
Expand Down

0 comments on commit 20eea2a

Please sign in to comment.