Skip to content

API > wxt/storage > WxtStorage

Interface: WxtStorage

Contents

Methods

defineItem()

defineItem(key)

defineItem<TValue, TMetadata>(key): WxtStorageItem<null | TValue, TMetadata>

Define a storage item with a default value, type, or versioning.

Read full docs: https://wxt.dev/guide/storage.html#defining-storage-items

Type parameters

TValue

TMetadata extends Record<string, unknown> = object

Parameters

key: string

Source

src/storage.ts:538

defineItem(key, options)

defineItem<TValue, TMetadata>(key, options): WxtStorageItem<TValue, TMetadata>

Type parameters

TValue

TMetadata extends Record<string, unknown> = object

Parameters

key: string

options: WxtStorageItemOptions<TValue>

Source

src/storage.ts:541


getItem()

getItem<T>(key, opts?): Promise<null | T>

Get an item from storage, or return null if it doesn't exist.

Type parameters

T

Parameters

key: string

opts?: GetItemOptions<T>

Returns

Example

ts
await storage.getItem<number>("local:installDate");

Source

src/storage.ts:440


getItems()

getItems(keys): Promise<object[]>

Get multiple items from storage. The return order is guaranteed to be the same as the order requested.

Parameters

keys: (string | object)[]

Returns

Example

ts
await storage.getItems(["local:installDate", "session:someCounter"]);

Source

src/storage.ts:448


getMeta()

getMeta<T>(key): Promise<T>

Return an object containing metadata about the key. Object is stored at key + "$". If value is not an object, it returns an empty object.

Type parameters

T extends Record<string, unknown>

Parameters

key: string

Returns

Example

ts
await storage.getMeta("local:installDate");

Source

src/storage.ts:458


removeItem()

removeItem(key, opts?): Promise<void>

Removes an item from storage.

Parameters

key: string

opts?: RemoveItemOptions

Returns

Example

ts
await storage.removeItem("local:installDate");

Source

src/storage.ts:494


removeItems()

removeItems(keys): Promise<void>

Remove a list of keys from storage.

Parameters

keys: (string | object)[]

Source

src/storage.ts:498


removeMeta()

removeMeta(key, properties?): Promise<void>

Remove the entire metadata for a key, or specific properties by name.

Parameters

key: string

properties?: string | string[]

Returns

Example

ts
// Remove all metadata properties from the item
await storage.removeMeta("local:installDate");

// Remove only specific the "v" field
await storage.removeMeta("local:installDate", "v")

Source

src/storage.ts:511


restoreSnapshot()

restoreSnapshot(base, data): Promise<void>

Restores the results of snapshot. If new properties have been saved since the snapshot, they are not overridden. Only values existing in the snapshot are overridden.

Parameters

base: string

data: any

Source

src/storage.ts:523


setItem()

setItem<T>(key, value): Promise<void>

Set a value in storage. Setting a value to null or undefined is equivalent to calling removeItem.

Type parameters

T

Parameters

key: string

value: null | T

Returns

Example

ts
await storage.setItem<number>("local:installDate", Date.now());

Source

src/storage.ts:466


setItems()

setItems(values): Promise<void>

Set multiple values in storage. If a value is set to null or undefined, the key is removed.

Parameters

values: object[]

Returns

Example

ts
await storage.setItem([
  { key: "local:installDate", value: Date.now() },
  { key: "session:someCounter, value: 5 },
]);

Source

src/storage.ts:476


setMeta()

setMeta<T>(key, properties): Promise<void>

Sets metadata properties. If some properties are already set, but are not included in the properties parameter, they will not be removed.

Type parameters

T extends Record<string, unknown>

Parameters

key: string

properties: null | T

Returns

Example

ts
await storage.setMeta("local:installDate", { appVersion });

Source

src/storage.ts:484


snapshot()

snapshot(base, opts?): Promise<Record<string, unknown>>

Return all the items in storage.

Parameters

base: string

opts?: SnapshotOptions

Source

src/storage.ts:515


unwatch()

unwatch(): void

Remove all watch listeners.

Source

src/storage.ts:531


watch()

watch<T>(key, cb): Unwatch

Watch for changes to a specific key in storage.

Type parameters

T

Parameters

key: string

cb: WatchCallback<null | T>

Source

src/storage.ts:527


Generated using typedoc-plugin-markdown and TypeDoc