export type StorageValue = null | string | number | boolean | object; export type WatchEvent = "update" | "remove"; export type WatchCallback = (event: WatchEvent, key: string) => any; type MaybePromise = T | Promise; type MaybeDefined = T extends any ? T : any; /** Types that {@link Storage.getItem} can convert the stored value to. */ export type GetItemType = keyof GetItemTypeMap; export interface GetItemTypeMap { text: string; json: JSONValue; bytes: Uint8Array; stream: ReadableStream; blob: Blob; } export type JSONValue = string | number | boolean | null | JSONObject | JSONArray; export interface JSONObject { [key: string]: JSONValue; } export type JSONArray = JSONValue[]; export type Unwatch = () => MaybePromise; export interface StorageMeta { atime?: Date; mtime?: Date; ttl?: number; [key: string]: StorageValue | Date | undefined; } export type TransactionOptions = Record; /** Options accepted when reading items, adding the `type` conversion option. */ export type GetItemOptions = TransactionOptions & { /** Convert the stored value to this type. Defaults to lenient JSON parsing. */ type?: K; }; /** {@link GetItemOptions} with a `type` that is required, selecting the return type. */ type TypedGetItemOptions = TransactionOptions & { type: K; }; /** {@link GetItemOptions} with no `type`, keeping the default return type. */ type UntypedGetItemOptions = TransactionOptions & { type?: undefined; }; /** An entry accepted by {@link Storage.getItems}: a key, or a key with per item options. */ export type GetItemsInput = string | { key: string; options?: GetItemOptions; }; /** * Value type of a {@link GetItemsInput}, based on its per item `type`. * * Falls back to `Default` (the `type` of the common options, or the storage's value type) * for entries that do not set one. */ type GetItemsValue = Item extends { options: { type: infer K extends GetItemType; }; } ? GetItemTypeMap[K] | null : Default; export type GetKeysOptions = TransactionOptions & { maxDepth?: number; }; export interface DriverFlags { maxDepth?: boolean; ttl?: boolean; } export interface DriverDependency { /** * Name of the npm package to install. * * Can differ from the import specifier used by the driver (e.g. `uploadthing` for `uploadthing/server`). */ name: string; /** * Import specifier the driver uses, when it differs from `name` (e.g. `uploadthing/server`). * * Defaults to `name` when not set. */ import?: string; /** * Supported version range of the package. */ version: string; /** * The dependency is only needed for some driver features or configurations. */ optional?: boolean; } /** * Third-party packages a driver dynamically imports, keyed by the driver option * that can be used to provide them (usually `lib`). * * Drivers expose this as a `DRIVER_DEPENDENCIES` export so that consumer frameworks * can check or install what a configured driver needs. */ export type DriverDependencies = Record; export interface Driver { name?: string; flags?: DriverFlags; options?: OptionsT; getInstance?: () => InstanceT; hasItem: (key: string, opts: TransactionOptions) => MaybePromise; getItem: (key: string, opts?: TransactionOptions) => MaybePromise; /** @experimental */ getItems?: (items: { key: string; options?: TransactionOptions; }[], commonOptions?: TransactionOptions) => MaybePromise<{ key: string; value: StorageValue; }[]>; /** @experimental */ getItemRaw?: (key: string, opts: TransactionOptions) => MaybePromise; setItem?: (key: string, value: string, opts: TransactionOptions) => MaybePromise; /** @experimental */ setItems?: (items: { key: string; value: string; options?: TransactionOptions; }[], commonOptions?: TransactionOptions) => MaybePromise; /** @experimental */ setItemRaw?: (key: string, value: any, opts: TransactionOptions) => MaybePromise; removeItem?: (key: string, opts: TransactionOptions) => MaybePromise; getMeta?: (key: string, opts: TransactionOptions) => MaybePromise; getKeys: (base: string, opts: GetKeysOptions) => MaybePromise; clear?: (base: string, opts: TransactionOptions) => MaybePromise; dispose?: () => MaybePromise; watch?: (callback: WatchCallback) => MaybePromise; } type StorageDefinition = { items: unknown; [key: string]: unknown; }; type StorageItemMap = T extends StorageDefinition ? T["items"] : T; type StorageItemType = K extends keyof StorageItemMap ? StorageItemMap[K] : T extends StorageDefinition ? StorageValue : T; export interface Storage { hasItem, K extends keyof StorageItemMap>(key: K, opts?: TransactionOptions): Promise; hasItem(key: string, opts?: TransactionOptions): Promise; getItem, K extends string & keyof StorageItemMap>(key: K, opts?: UntypedGetItemOptions): Promise | null>; getItem(key: string, opts: TypedGetItemOptions): Promise; getItem>(key: string, opts?: UntypedGetItemOptions): Promise; /** @experimental */ getItems(items: Items, commonOptions: TypedGetItemOptions): Promise<{ key: string; value: GetItemsValue; }[]>; /** @experimental */ getItems(items: Items, commonOptions?: UntypedGetItemOptions): Promise<{ key: string; value: GetItemsValue; }[]>; /** @experimental See https://github.com/unjs/unstorage/issues/142 */ getItemRaw: (key: string, opts?: TransactionOptions) => Promise | null>; setItem, K extends keyof StorageItemMap>(key: K, value: StorageItemType, opts?: TransactionOptions): Promise; setItem(key: string, value: U, opts?: TransactionOptions): Promise; /** @experimental */ setItems: (items: { key: string; value: U; options?: TransactionOptions; }[], commonOptions?: TransactionOptions) => Promise; /** @experimental See https://github.com/unjs/unstorage/issues/142 */ setItemRaw: (key: string, value: MaybeDefined, opts?: TransactionOptions) => Promise; removeItem, K extends keyof StorageItemMap>(key: K, opts?: (TransactionOptions & { removeMeta?: boolean; }) | boolean): Promise; removeItem(key: string, opts?: (TransactionOptions & { removeMeta?: boolean; }) | boolean): Promise; getMeta: (key: string, opts?: (TransactionOptions & { nativeOnly?: boolean; }) | boolean) => MaybePromise; setMeta: (key: string, value: StorageMeta, opts?: TransactionOptions) => Promise; removeMeta: (key: string, opts?: TransactionOptions) => Promise; getKeys: (base?: string, opts?: GetKeysOptions) => Promise; clear: (base?: string, opts?: TransactionOptions) => Promise; dispose: () => Promise; watch: (callback: WatchCallback) => Promise; unwatch: () => Promise; mount: (base: string, driver: Driver) => Storage; unmount: (base: string, dispose?: boolean) => Promise; getMount: (key?: string) => { base: string; driver: Driver; }; getMounts: (base?: string, options?: { parents?: boolean; }) => { base: string; driver: Driver; }[]; keys: Storage["getKeys"]; get: Storage["getItem"]; set: Storage["setItem"]; has: Storage["hasItem"]; del: Storage["removeItem"]; remove: Storage["removeItem"]; } export {};