Skip to content
On this page

API Reference - storage

@webext-core/storage

defineExtensionStorage

ts
function defineExtensionStorage<TSchema extends AnySchema = AnySchema>(
  storage: Storage.StorageArea
): ExtensionStorage<TSchema> {
  // ...
}

Create a storage instance with an optional schema, TSchema, for type safety.

Parameters

  • storage: Storage.StorageArea
    The storage to to use. Either Browser.storage.local, Browser.storage.sync, or Browser.storage.managed.

Examples

ts
import browser from 'webextension-polyfill';

interface Schema {
  installDate: number;
}
const extensionStorage = defineExtensionStorage<Schema>(browser.storage.local);

const date = await extensionStorage.getItem("installDate");

ExtensionStorage

ts
interface ExtensionStorage<TSchema extends AnySchema> {
  clear(): Promise<void>;
  getItem<TKey extends keyof TSchema>(
    key: TKey
  ): Promise<Required<TSchema>[TKey] | null>;
  setItem<TKey extends keyof TSchema>(
    key: TKey,
    value: TSchema[TKey]
  ): Promise<void>;
  removeItem<TKey extends keyof TSchema>(key: TKey): Promise<void>;
  onChange<TKey extends keyof TSchema>(
    key: TKey,
    cb: OnChangeCallback<TSchema, TKey>
  ): RemoveListenerCallback;
}

This is the interface for the storage objects exported from the package. It is similar to localStorage, except for a few differences:

  • It's async since the web extension storage APIs are async.
  • It can store any data type, not just strings.

localExtStorage

ts
const localExtStorage: ExtensionStorage<AnySchema>;

An implementation of ExtensionStorage based on the browser.storage.local storage area.

managedExtStorage

ts
const managedExtStorage: ExtensionStorage<AnySchema>;

An implementation of ExtensionStorage based on the browser.storage.managed storage area.

syncExtStorage

ts
const syncExtStorage: ExtensionStorage<AnySchema>;

An implementation of ExtensionStorage based on the browser.storage.sync storage area.




API reference generated by plugins/typescript-docs.ts

Released under the MIT License.