storetify
Version:
Enhanced localStorage with expiration, subscription, and full TypeScript support
73 lines (69 loc) • 3.69 kB
TypeScript
/**
* NextStorage
* Next localStorage
*/
declare class NextStorage {
protected store: Storage;
namespace: string;
protected observers: Map<string, StoretifyListener<StoretifyValue>[]>;
protected windowEventStorage: boolean;
protected static storage: NextStorage | null;
protected constructor(store: Storage);
static getInstance(store?: any): NextStorage;
setNamespace(space: string): void;
getNamespace(): string;
getUsed(): string;
setStore<T extends Storage>(store: T): void;
protected getStore(): Storage;
set<T = StoretifyValue>(key: string, value: T, expires?: number): this;
get<T = StoretifyValue>(key: string): T | null;
getItem(key: string): string | null;
has(key: string): boolean;
publish(observers: StoretifyListener[] | string, e: StorageEvent, force?: boolean, defaultKey?: string | null): void;
publishAll(e: StorageEvent): void;
subscribe(key: string, listener: StoretifyListener): this;
getObserver(key: string): StoretifyListener[];
unsubscribe(keys?: string | string[], listener?: StoretifyListener): void;
remove(key: string, soft?: boolean): this;
clear(): void;
}
type JSONPrimitive = string | number | boolean | null;
type StoretifyValue = JSONPrimitive | JSONObject | JSONArray;
interface JSONObject {
[key: string]: StoretifyValue;
}
type JSONArray = Array<StoretifyValue>;
type StoretifySafeValue<T = StoretifyValue> = T | null;
type StoretifySetterFunction<T = StoretifyValue> = (...args: any[]) => StoretifySafeValue<T>;
type StoretifyArgument<T extends StoretifyValue | StoretifySetterFunction> = [string, T?, number?];
interface StoretifyEvent<T = StoretifyValue> extends Omit<StorageEvent, "newValue" | "oldValue"> {
newValue: StoretifySafeValue<T>;
oldValue: StoretifySafeValue<T>;
native: StorageEvent;
}
type StoretifyListener<T = StoretifyValue> = (e: StoretifyEvent<T>) => void;
interface StoretifyStoreStage {
storage: NextStorage;
set: <T = StoretifyValue>(key: string, value: StoretifySafeValue<T> | StoretifySetterFunction<T>, expires?: number) => StoretifyStoreStage;
get: <T = StoretifyValue>(key: string) => StoretifySafeValue<T>;
remove: (key: string, soft?: boolean) => StoretifyStoreStage;
has: (key: string) => boolean;
clear: () => void;
subscribe: <T = StoretifyValue>(key: string, listener: StoretifyListener<T>) => StoretifyStoreStage;
unsubscribe: <T = StoretifyValue>(keys?: string | string[], listener?: StoretifyListener<T>) => void;
getObserver<T = StoretifyValue>(key: string): StoretifyListener<T>[];
getUsed: () => string;
}
interface StoretifyStage extends Partial<StoretifyStoreStage> {
<T extends StoretifyValue | StoretifySetterFunction<T>>(key: string, value: T, expires?: number): StoretifyStoreStage;
<T = StoretifyValue>(key: string): StoretifySafeValue<T>;
<T = StoretifyValue>(...rest: StoretifyArgument<StoretifyValue | StoretifySetterFunction>): StoretifySafeValue<T> | StoretifyStoreStage;
}
interface Storetify extends StoretifyStoreStage {
<T extends StoretifyValue | StoretifySetterFunction<T>>(key: string, value: T, expires?: number): StoretifyStoreStage;
<T = StoretifyValue>(key: string): StoretifySafeValue<T>;
<T = StoretifyValue>(...rest: StoretifyArgument<StoretifyValue | StoretifySetterFunction>): StoretifySafeValue<T> | StoretifyStoreStage;
}
declare const _default: Storetify;
export { _default as default };
export type { JSONArray, JSONObject, JSONPrimitive, Storetify, StoretifyArgument, StoretifyEvent, StoretifyListener, StoretifySafeValue, StoretifySetterFunction, StoretifyStage, StoretifyStoreStage, StoretifyValue };