Skip to content

Commit

Permalink
feat: Extract wxt/storage to its own package, @wxt-dev/storage (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
aklinker1 authored Nov 14, 2024
1 parent 62ea796 commit d74f6b0
Show file tree
Hide file tree
Showing 15 changed files with 1,166 additions and 899 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ on:
- module-solid
- module-svelte
- module-vue
- storage
- unocss
- wxt

Expand Down
11 changes: 6 additions & 5 deletions .github/workflows/sync-releases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ on:
default: wxt
type: choice
options:
- wxt
- module-react
- module-vue
- module-svelte
- module-solid
- auto-icons
- i18n
- module-react
- module-solid
- module-svelte
- module-vue
- storage
- wxt

jobs:
sync:
Expand Down
3 changes: 2 additions & 1 deletion docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { version as wxtVersion } from '../../packages/wxt/package.json';
import { version as i18nVersion } from '../../packages/i18n/package.json';
import { version as autoIconsVersion } from '../../packages/auto-icons/package.json';
import { version as unocssVersion } from '../../packages/unocss/package.json';
import { version as storageVersion } from '../../packages/storage/package.json';

const title = 'Next-gen Web Extension Framework';
const titleSuffix = ' – WXT';
Expand Down Expand Up @@ -87,7 +88,7 @@ export default defineConfig({
),
]),
navItem('Other Packages', [
navItem(`wxt/storage — ${wxtVersion}`, '/storage'),
navItem(`@wxt-dev/storage — ${storageVersion}`, '/storage'),
navItem(`@wxt-dev/auto-icons — ${autoIconsVersion}`, '/auto-icons'),
navItem(`@wxt-dev/i18n — ${i18nVersion}`, '/i18n'),
navItem(`@wxt-dev/unocss — ${unocssVersion}`, '/unocss'),
Expand Down
54 changes: 39 additions & 15 deletions docs/storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,47 @@ outline: deep

[Changelog](https://github.com/wxt-dev/wxt/blob/main/packages/wxt/CHANGELOG.md)

WXT provides a simplified API to replace the `browser.storage.*` APIs. Use the `storage` auto-import from `wxt/storage` or import it manually to get started:
A simplified wrapper around the extension storage APIs.

## Installation

### With WXT

This module is built-in to WXT, so you don't need to install anything.

```ts
import { storage } from 'wxt/storage';
```

> [!IMPORTANT]
> To use the `wxt/storage` API, the `"storage"` permission must be added to the manifest:
>
> ```ts
> // wxt.config.ts
> export default defineConfig({
> manifest: {
> permissions: ['storage'],
> },
> });
> ```
If you use auto-imports, `storage` is auto-imported for you, so you don't even need to import it!

### Without WXT

Install the NPM package:

```sh
npm i @wxt-dev/storage
pnpm add @wxt-dev/storage
yarn add @wxt-dev/storage
bun add @wxt-dev/storage
```

```ts
import { storage } from '@wxt-dev/storage';
```

## Storage Permission

To use the `wxt/storage` API, the `"storage"` permission must be added to the manifest:

```ts
// wxt.config.ts
export default defineConfig({
manifest: {
permissions: ['storage'],
},
});
```

## Basic Usage

Expand Down Expand Up @@ -51,7 +75,7 @@ await storage.watch<number>(
await storage.getMeta<{ v: number }>('local:installDate');
```

For a full list of methods available, see the [API reference](/api/reference/wxt/storage/interfaces/WxtStorage).
For a full list of methods available, see the [API reference](/api/reference/@wxt-dev/storage/interfaces/WxtStorage).

## Watchers

Expand Down Expand Up @@ -134,7 +158,7 @@ const unwatch = showChangelogOnUpdate.watch((newValue) => {
});
```

For a full list of properties and methods available, see the [API reference](/api/reference/wxt/storage/interfaces/WxtStorageItem).
For a full list of properties and methods available, see the [API reference](/api/reference/@wxt-dev/storage/interfaces/WxtStorageItem).

### Versioning

Expand Down Expand Up @@ -330,4 +354,4 @@ await storage.setItems([
]);
```
Refer to the [API Reference](/api/reference/wxt/storage/interfaces/WxtStorage) for types and examples of how to use all the bulk APIs.
Refer to the [API Reference](/api/reference/@wxt-dev/storage/interfaces/WxtStorage) for types and examples of how to use all the bulk APIs.
36 changes: 36 additions & 0 deletions packages/storage/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# WXT Storage

[Changelog](https://github.com/wxt-dev/wxt/blob/main/packages/storage/CHANGELOG.md) &bull; [Docs](https://wxt.dev/storage.html)

A simplified wrapper around the extension storage APIs.

## Installation

### With WXT

This module is built-in to WXT, so you don't need to install anything.

```ts
import { storage } from 'wxt/storage';
```

If you use auto-imports, `storage` is auto-imported for you, so you don't even need to import it!

### Without WXT

Install the NPM package:

```sh
npm i @wxt-dev/storage
pnpm add @wxt-dev/storage
yarn add @wxt-dev/storage
bun add @wxt-dev/storage
```

```ts
import { storage } from '@wxt-dev/storage';
```

## Usage

Read full docs on the [documentation website](https://wxt.dev/storage.html).
56 changes: 56 additions & 0 deletions packages/storage/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "@wxt-dev/storage",
"description": "Web extension storage API provided by WXT, supports all browsers.",
"version": "1.0.0",
"type": "module",
"repository": {
"type": "git",
"url": "git+https://github.com/wxt-dev/wxt.git",
"directory": "packages/storage"
},
"homepage": "https://wxt.dev/storage.html",
"keywords": [
"wxt",
"storage",
"extension",
"addon",
"chrome",
"firefox",
"edge"
],
"author": {
"name": "Aaron Klinker",
"email": "[email protected]"
},
"license": "MIT",
"scripts": {
"build": "buildc -- unbuild",
"check": "buildc --deps-only -- check",
"test": "buildc --deps-only -- vitest"
},
"dependencies": {
"async-mutex": "^0.5.0",
"dequal": "^2.0.3"
},
"devDependencies": {
"@aklinker1/check": "^1.4.5",
"@types/chrome": "^0.0.268",
"@webext-core/fake-browser": "^1.3.1",
"oxlint": "^0.9.9",
"publint": "^0.2.11",
"typescript": "^5.6.2",
"unbuild": "^2.0.0",
"vitest": "^2.0.0"
},
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.mts",
"import": "./dist/index.mjs"
}
},
"files": [
"dist"
]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { fakeBrowser } from '@webext-core/fake-browser';
import { describe, it, expect, beforeEach, vi, expectTypeOf } from 'vitest';
import { browser } from 'wxt/browser';
import { MigrationError, WxtStorageItem, storage } from '../storage';
import { MigrationError, type WxtStorageItem, storage } from '../index';

/**
* This works because fakeBrowser is synchronous, and is will finish any number of chained
Expand Down Expand Up @@ -223,7 +222,7 @@ describe('Storage Utils', () => {
describe('setMeta', () => {
it('should set metadata at key+$', async () => {
const existing = { v: 1 };
await browser.storage[storageArea].set({ count$: existing });
await chrome.storage[storageArea].set({ count$: existing });
const newValues = {
date: Date.now(),
};
Expand All @@ -239,7 +238,7 @@ describe('Storage Utils', () => {
'should remove any properties set to %s',
async (version) => {
const existing = { v: 1 };
await browser.storage[storageArea].set({ count$: existing });
await chrome.storage[storageArea].set({ count$: existing });
const expected = {};

await storage.setMeta(`${storageArea}:count`, { v: version });
Expand Down Expand Up @@ -1163,7 +1162,7 @@ describe('Storage Utils', () => {

await item.removeValue();
// Make sure it's actually blank before running the test
expect(await browser.storage.local.get()).toEqual({});
expect(await chrome.storage.local.get()).toEqual({});
init.mockClear();

const [value1, value2] = await Promise.all([
Expand Down
Loading

0 comments on commit d74f6b0

Please sign in to comment.