import { Browser } from "@wxt-dev/browser"; //#region src/index.d.ts declare const storage: WxtStorage; interface WxtStorage { /** * Get an item from storage, or return `null` if it doesn't exist. * * @example * await storage.getItem("local:installDate"); */ getItem(key: StorageItemKey, opts: GetItemOptions & { fallback: TValue; }): Promise; getItem(key: StorageItemKey, opts?: GetItemOptions): Promise; /** * Get multiple items from storage. The return order is guaranteed to be the same as the order * requested. * * @example * await storage.getItems(["local:installDate", "session:someCounter"]); */ getItems(keys: Array | { key: StorageItemKey; options?: GetItemOptions; }>): Promise>; /** * Return an object containing metadata about the key. Object is stored at `key + "$"`. If value * is not an object, it returns an empty object. * * @example * await storage.getMeta("local:installDate"); */ getMeta>(key: StorageItemKey): Promise; /** * Get the metadata of multiple storage items. * * @param keys List of keys or items to get the metadata of. * @returns An array containing storage keys and their metadata. */ getMetas(keys: Array>): Promise>; /** * Set a value in storage. Setting a value to `null` or `undefined` is equivalent to calling * `removeItem`. * * @example * await storage.setItem("local:installDate", Date.now()); */ setItem(key: StorageItemKey, value: T | null): Promise; /** * Set multiple values in storage. If a value is set to `null` or `undefined`, the key is removed. * * @example * await storage.setItem([ * { key: "local:installDate", value: Date.now() }, * { key: "session:someCounter, value: 5 }, * ]); */ setItems(values: Array<{ key: StorageItemKey; value: any; } | { item: WxtStorageItem; value: any; }>): Promise; /** * Sets metadata properties. If some properties are already set, but are not included in the * `properties` parameter, they will not be removed. * * @example * await storage.setMeta("local:installDate", { appVersion }); */ setMeta>(key: StorageItemKey, properties: T | null): Promise; /** * Set the metadata of multiple storage items. * * @param metas List of storage keys or items and metadata to set for each. */ setMetas(metas: Array<{ key: StorageItemKey; meta: Record; } | { item: WxtStorageItem; meta: Record; }>): Promise; /** * Removes an item from storage. * * @example * await storage.removeItem("local:installDate"); */ removeItem(key: StorageItemKey, opts?: RemoveItemOptions): Promise; /** * Remove a list of keys from storage. */ removeItems(keys: Array | { key: StorageItemKey; options?: RemoveItemOptions; } | { item: WxtStorageItem; options?: RemoveItemOptions; }>): Promise; /** * Removes all items from the provided storage area. */ clear(base: StorageArea): Promise; /** * Remove the entire metadata for a key, or specific properties by name. * * @example * // Remove all metadata properties from the item * await storage.removeMeta("local:installDate"); * * // Remove only specific the "v" field * await storage.removeMeta("local:installDate", "v") */ removeMeta(key: StorageItemKey, properties?: string | string[]): Promise; /** * Return all the items in storage. */ snapshot(base: StorageArea, opts?: SnapshotOptions): Promise>; /** * 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. */ restoreSnapshot(base: StorageArea, data: any): Promise; /** * Watch for changes to a specific key in storage. */ watch(key: StorageItemKey, cb: WatchCallback): Unwatch; /** * Remove all watch listeners. */ unwatch(): void; /** * Define a storage item with a default value, type, or versioning. * * Read full docs: https://wxt.dev/storage.html#defining-storage-items */ defineItem = {}>(key: StorageItemKey): WxtStorageItem; defineItem = {}>(key: StorageItemKey, options: WxtStorageItemOptions & { fallback: TValue; }): WxtStorageItem; defineItem = {}>(key: StorageItemKey, options: WxtStorageItemOptions & { defaultValue: TValue; }): WxtStorageItem; defineItem = {}>(key: StorageItemKey, options: WxtStorageItemOptions & { init: () => TValue | Promise; }): WxtStorageItem; defineItem = {}>(key: StorageItemKey, options: WxtStorageItemOptions): WxtStorageItem; } interface WxtStorageItem> { /** * The storage key passed when creating the storage item. */ key: StorageItemKey; /** * @deprecated Renamed to fallback, use it instead. */ defaultValue: TValue; /** * The value provided by the `fallback` option. */ fallback: TValue; /** * Get the latest value from storage. */ getValue(): Promise; /** * Get metadata. */ getMeta(): Promise>; /** * Set the value in storage. */ setValue(value: TValue): Promise; /** * Set metadata properties. */ setMeta(properties: NullablePartial): Promise; /** * Remove the value from storage. */ removeValue(opts?: RemoveItemOptions): Promise; /** * Remove all metadata or certain properties from metadata. */ removeMeta(properties?: string[]): Promise; /** * Listen for changes to the value in storage. */ watch(cb: WatchCallback): Unwatch; /** * If there are migrations defined on the storage item, migrate to the latest version. * * **This function is ran automatically whenever the extension updates**, so you don't have to call it * manually. */ migrate(): Promise; } type StorageArea = 'local' | 'session' | 'sync' | 'managed'; type StorageItemKey = `${StorageArea}:${string}`; interface GetItemOptions { /** * @deprecated Renamed to `fallback`, use it instead. */ defaultValue?: T; /** * Default value returned when `getItem` would otherwise return `null`. */ fallback?: T; } interface RemoveItemOptions { /** * Optionally remove metadata when deleting a key. * * @default false */ removeMeta?: boolean; } interface SnapshotOptions { /** * Exclude a list of keys. The storage area prefix should be removed since the snapshot is for a * specific storage area already. */ excludeKeys?: string[]; } interface WxtStorageItemOptions { /** * @deprecated Renamed to `fallback`, use it instead. */ defaultValue?: T; /** * Default value returned when `getValue` would otherwise return `null`. */ fallback?: T; /** * If passed, a value in storage will be initialized immediately after * defining the storage item. This function returns the value that will be * saved to storage during the initialization process if a value doesn't * already exist. */ init?: () => T | Promise; /** * Provide a version number for the storage item to enable migrations. When changing the version * in the future, migration functions will be ran on application startup. */ version?: number; /** * A map of version numbers to the functions used to migrate the data to that version. */ migrations?: Record any>; /** * Print debug logs, such as migration process. * @default false */ debug?: boolean; /** * A callback function that runs on migration complete. */ onMigrationComplete?: (migratedValue: T, targetVersion: number) => void; } type StorageAreaChanges = { [key: string]: Browser.storage.StorageChange; }; /** * Same as `Partial`, but includes `| null`. It makes all the properties of an object optional and * nullable. */ type NullablePartial = { [key in keyof T]+?: T[key] | undefined | null }; /** * Callback called when a value in storage is changed. */ type WatchCallback = (newValue: T, oldValue: T) => void; /** * Call to remove a watch listener */ type Unwatch = () => void; declare class MigrationError extends Error { key: string; version: number; constructor(key: string, version: number, options?: ErrorOptions); } //#endregion export { GetItemOptions, MigrationError, RemoveItemOptions, SnapshotOptions, StorageArea, StorageAreaChanges, StorageItemKey, Unwatch, WatchCallback, WxtStorage, WxtStorageItem, WxtStorageItemOptions, storage };