@daysnap/utils
Version:
53 lines (51 loc) • 1.23 kB
TypeScript
interface StorageLike {
getItem: (key: string) => string | null;
setItem: (key: string, value: string) => void;
removeItem: (key: string) => void;
}
type DefaultVal<T> = T extends Record<string, any> ? Partial<T> : T;
interface StorageOptions<T = any> {
initialValue?: T;
debug: boolean;
cached: boolean;
}
declare class Storage<T = any> {
private readonly key;
private readonly storage;
private value;
private options;
constructor(key: string, storage: StorageLike, options?: Partial<StorageOptions<T>>);
_debug(...args: string[]): void;
/**
* 设置值
*/
setItem(val: T): T;
/**
* 获取值
*/
getItem(): T | null;
getItem(defaultVal: DefaultVal<T>): T;
/**
* 删除值
*/
removeItem(): void;
/**
* 更新值
*/
updateItem(val: DefaultVal<T>): T;
/**
* 获取值后,删除存储的值
*/
getItemOnce(): T | null;
getItemOnce(defaultVal: DefaultVal<T>): T;
/**
* 优先从缓存中获取值
*/
getItemWithCache(): T | null;
getItemWithCache(defaultVal: DefaultVal<T>): T;
/**
* 清除缓存
*/
clearCache(): void;
}
export { Storage };