pinia-plugin-state-persistence
Version:
Pinia plugin for universal state persistence across synchronous and asynchronous storage systems, supporting advanced features like path inclusion/exclusion and custom serialization.
34 lines (33 loc) • 1.25 kB
TypeScript
import type { StateTree } from 'pinia';
type MaybePromise<T> = T | Promise<T>;
export interface Storage {
getItem: (key: string) => MaybePromise<string | object | null>;
setItem: (key: string, value: string) => MaybePromise<any>;
removeItem: (key: string) => MaybePromise<any>;
}
export interface PersistOptions<S extends StateTree = StateTree> {
key?: string | Record<keyof S, string> | Record<string, string>;
debug?: boolean;
overwrite?: boolean;
clientOnly?: boolean;
storage?: Storage;
filter?: (mutation: any, state: S) => boolean;
serialize?: (state: Partial<S>) => string;
deserialize?: (state: string) => Partial<S>;
deepCopy?: boolean;
include?: string | string[];
exclude?: string | string[];
}
export interface GlobalPersistOptions<S extends StateTree = StateTree> extends Omit<PersistOptions<S>, 'key' | 'include' | 'exclude'> {
key?: string;
}
declare module 'pinia' {
interface PiniaCustomProperties {
$persist: () => MaybePromise<void>;
$restore: () => MaybePromise<void>;
}
interface DefineStoreOptionsBase<S extends StateTree, Store> {
persist?: boolean | PersistOptions<S> | Omit<PersistOptions<S>, 'overwrite'>[];
}
}
export {};