@ngxs/storage-plugin
Version:
extendable storage plugin for @ngxs/store
97 lines (93 loc) • 3.46 kB
TypeScript
import { InjectionToken, Type } from '@angular/core';
import { StateToken } from '@ngxs/store';
import { ɵStateClass as _StateClass } from '@ngxs/store/internals';
/** This enables the user to provide a storage engine per individual key. */
interface KeyWithExplicitEngine {
key: string | _StateClass | StateToken<any>;
engine: Type<StorageEngine> | InjectionToken<StorageEngine>;
}
/** Determines whether the provided key has the following structure. */
declare function ɵisKeyWithExplicitEngine(key: any): key is KeyWithExplicitEngine;
/**
* This tuples all of the possible types allowed in the `key` property.
* This is not exposed publicly and used internally only.
*/
type StorageKey = string | _StateClass | StateToken<any> | KeyWithExplicitEngine;
declare function ɵextractStringKey(storageKey: StorageKey): string;
/**
* The following key is used to store the entire serialized
* state when no specific state is provided.
*/
declare const ɵDEFAULT_STATE_KEY = "@@STATE";
declare enum StorageOption {
LocalStorage = 0,
SessionStorage = 1
}
interface NgxsStoragePluginOptions {
/**
* Keys for the state slice to store in the storage engine.
*/
keys: '*' | StorageKey[];
/**
* The namespace is used to prefix the key for the state slice. This is
* necessary when running micro frontend applications which use storage plugin.
* The namespace will eliminate the conflict between keys that might overlap.
*/
namespace?: string;
/**
* Storage engine to use. Deaults to localStorage but can provide
*
* sessionStorage or custom implementation of the StorageEngine interface
*/
storage?: StorageOption;
/**
* Migration strategies.
*/
migrations?: {
/**
* Version to key off.
*/
version: number | string;
/**
* Method to migrate the previous state.
*/
migrate: (state: any) => any;
/**
* Key to migrate.
*/
key?: string;
/**
* Key for the version. Defaults to 'version'.
*/
versionKey?: string;
}[];
/**
* Serailizer for the object before its pushed into the engine.
*/
serialize?(obj: any): string;
/**
* Deserializer for the object before its pulled out of the engine.
*/
deserialize?(obj: any): any;
/**
* Method to alter object before serialization.
*/
beforeSerialize?(obj: any, key: string): any;
/**
* Method to alter object after deserialization.
*/
afterDeserialize?(obj: any, key: string): any;
}
interface ɵNgxsTransformedStoragePluginOptions extends NgxsStoragePluginOptions {
keys: StorageKey[];
}
declare const ɵUSER_OPTIONS: InjectionToken<NgxsStoragePluginOptions>;
declare const ɵALL_STATES_PERSISTED: InjectionToken<boolean>;
declare const ɵNGXS_STORAGE_PLUGIN_OPTIONS: InjectionToken<ɵNgxsTransformedStoragePluginOptions>;
declare const STORAGE_ENGINE: InjectionToken<StorageEngine>;
interface StorageEngine {
getItem(key: string): any;
setItem(key: string, value: any): void;
}
export { STORAGE_ENGINE, StorageOption, ɵALL_STATES_PERSISTED, ɵDEFAULT_STATE_KEY, ɵNGXS_STORAGE_PLUGIN_OPTIONS, ɵUSER_OPTIONS, ɵextractStringKey, ɵisKeyWithExplicitEngine };
export type { KeyWithExplicitEngine, NgxsStoragePluginOptions, StorageEngine, StorageKey, ɵNgxsTransformedStoragePluginOptions };