UNPKG

ngx-webstorage

Version:

Local and session storage services and decorators for Angular

231 lines (207 loc) 9.26 kB
import { Subject, Observable } from 'rxjs'; import * as i0 from '@angular/core'; import { InjectionToken, NgZone, Provider } from '@angular/core'; declare enum StorageStrategies { Local = "local_strategy", Session = "session_strategy", InMemory = "in_memory_strategy" } interface WebStorage { readonly length: number; clear(): void; getItem(key: string): string | null; key(index: number): string | null; removeItem(key: string): void; setItem(key: string, value: string): void; [name: string]: any; } declare class CompatHelper { static isStorageAvailable(storage: WebStorage): boolean; } interface StorageStrategy<T> { readonly keyChanges: Subject<string>; readonly isAvailable: boolean; readonly name: string; get(key: string): Observable<T>; set(key: string, value: T): Observable<T>; del(key: string): Observable<void>; clear(): Observable<void>; } interface StorageService { retrieve(key: string): any; store(key: string, value: any): any; clear(key?: string): void; getStrategyName(): string; observe(key: string): Observable<any>; } declare class SyncStorage implements StorageService { protected strategy: StorageStrategy<any>; constructor(strategy: StorageStrategy<any>); retrieve(key: string): any; store(key: string, value: any): any; clear(key?: string): void; getStrategyName(): string; observe(key: string): Observable<any>; } declare class AsyncStorage implements StorageService { protected strategy: StorageStrategy<any>; constructor(strategy: StorageStrategy<any>); retrieve(key: string): Observable<any>; store(key: string, value: any): Observable<any>; clear(key?: string): Observable<void>; getStrategyName(): string; observe(key: string): Observable<any>; } interface StrategyCache { [key: string]: any; } declare class StrategyCacheService { protected caches: { [name: string]: StrategyCache; }; get(strategyName: string, key: string): any; set(strategyName: string, key: string, value: any): void; del(strategyName: string, key: string): void; clear(strategyName: string): void; protected getCacheStore(strategyName: string): StrategyCache; static ɵfac: i0.ɵɵFactoryDeclaration<StrategyCacheService, never>; static ɵprov: i0.ɵɵInjectableDeclaration<StrategyCacheService>; } declare const LOCAL_STORAGE: InjectionToken<WebStorage>; declare const SESSION_STORAGE: InjectionToken<WebStorage>; declare const STORAGE_STRATEGIES: InjectionToken<StorageStrategy<any>>; declare abstract class BaseSyncStorageStrategy implements StorageStrategy<any> { protected storage: WebStorage; protected cache: StrategyCacheService; readonly keyChanges: Subject<string>; abstract readonly name: string; constructor(storage: WebStorage, cache: StrategyCacheService); protected _isAvailable: boolean | undefined; get isAvailable(): boolean; get(key: string): Observable<any>; set(key: string, value: any): Observable<any>; del(key: string): Observable<void>; clear(): Observable<void>; } declare class LocalStorageStrategy extends BaseSyncStorageStrategy { protected storage: WebStorage; protected cache: StrategyCacheService; protected platformId: any; protected zone: NgZone; static readonly strategyName: string; readonly name: string; constructor(storage: WebStorage, cache: StrategyCacheService, platformId: any, zone: NgZone); protected listenExternalChanges(): void; static ɵfac: i0.ɵɵFactoryDeclaration<LocalStorageStrategy, never>; static ɵprov: i0.ɵɵInjectableDeclaration<LocalStorageStrategy>; } declare class SessionStorageStrategy extends BaseSyncStorageStrategy { protected storage: WebStorage; protected cache: StrategyCacheService; protected platformId: any; protected zone: NgZone; static readonly strategyName: string; readonly name: string; constructor(storage: WebStorage, cache: StrategyCacheService, platformId: any, zone: NgZone); protected listenExternalChanges(): void; static ɵfac: i0.ɵɵFactoryDeclaration<SessionStorageStrategy, never>; static ɵprov: i0.ɵɵInjectableDeclaration<SessionStorageStrategy>; } declare class InMemoryStorageStrategy implements StorageStrategy<any> { protected cache: StrategyCacheService; static readonly strategyName: string; readonly keyChanges: Subject<string>; isAvailable: boolean; readonly name: string; constructor(cache: StrategyCacheService); get(key: string): Observable<any>; set(key: string, value: any): Observable<any>; del(key: string): Observable<void>; clear(): Observable<void>; static ɵfac: i0.ɵɵFactoryDeclaration<InMemoryStorageStrategy, never>; static ɵprov: i0.ɵɵInjectableDeclaration<InMemoryStorageStrategy>; } declare const StorageStrategyStubName: string; declare class StorageStrategyStub implements StorageStrategy<any> { readonly keyChanges: Subject<string>; store: any; _available: boolean; readonly name: string; constructor(name?: string); get isAvailable(): boolean; get(key: string): Observable<any>; set(key: string, value: any): Observable<any>; del(key: string): Observable<void>; clear(): Observable<void>; } declare class StorageStub implements WebStorage { [name: string]: any; store: { [prop: string]: any; }; get length(): number; clear(): void; getItem(key: string): string | null; key(index: number): string | null; removeItem(key: string): void; setItem(key: string, value: string): void; } declare const InvalidStrategyError = "invalid_strategy"; declare class StrategyIndex { protected strategies: StorageStrategy<any>[]; static index: { [name: string]: StorageStrategy<any>; }; readonly registration$: Subject<string>; constructor(strategies: StorageStrategy<any>[]); static get(name: string): StorageStrategy<any>; static set(name: string, strategy: StorageStrategy<any>): void; static clear(name?: string): void; static isStrategyRegistered(name: string): boolean; static hasRegistredStrategies(): boolean; getStrategy(name: string): StorageStrategy<any>; indexStrategies(): void; indexStrategy(name: string, overrideIfExists?: boolean): StorageStrategy<any>; register(name: string, strategy: StorageStrategy<any>, overrideIfExists?: boolean): void; static ɵfac: i0.ɵɵFactoryDeclaration<StrategyIndex, [{ optional: true; }]>; static ɵprov: i0.ɵɵInjectableDeclaration<StrategyIndex>; } declare class LocalStorageService extends SyncStorage { } declare class SessionStorageService extends SyncStorage { } declare function LocalStorage(key?: string, defaultValue?: any): (prototype: any, propName: string) => void; declare function SessionStorage(key?: string, defaultValue?: any): (prototype: any, propName: string) => void; interface NgxWebstorageConfiguration { prefix?: string; separator?: string; caseSensitive?: boolean; } declare const LIB_CONFIG: InjectionToken<NgxWebstorageConfiguration>; declare enum InternalNgxWebstorageFeatureKind { Config = 1, LocalStorage = 2, SessionStorage = 3 } type NgxWebstorageFeatureKind = string | InternalNgxWebstorageFeatureKind; type NgxWebstorageFeature<FeatureKind extends NgxWebstorageFeatureKind> = { kind: FeatureKind; providers: Provider[]; }; /** * Provide ngx-webstorage basic features. * * - You can customise the configuration with the `withConfiguration` feature. * - You can enable the `LocalStorage` features with the `withLocalStorage` feature. * - You can enable the `SessionStorage` features with the `withSessionStorage` feature. * * @default config { prefix: 'ngx-webstorage', separator: '|', caseSensitive: false } */ declare function provideNgxWebstorage(...features: NgxWebstorageFeature<NgxWebstorageFeatureKind>[]): i0.EnvironmentProviders; declare function makeNgxWebstorageFeature<FeatureKind extends NgxWebstorageFeatureKind>(kind: FeatureKind, providers: Provider[]): NgxWebstorageFeature<FeatureKind>; declare function withNgxWebstorageConfig(config: NgxWebstorageConfiguration): NgxWebstorageFeature<InternalNgxWebstorageFeatureKind.Config>; /** Provides everything necessary to use the `LocalStorage` features. */ declare function withLocalStorage(): NgxWebstorageFeature<InternalNgxWebstorageFeatureKind.LocalStorage>; declare function withSessionStorage(): NgxWebstorageFeature<InternalNgxWebstorageFeatureKind.SessionStorage>; export { AsyncStorage, CompatHelper, InMemoryStorageStrategy, InternalNgxWebstorageFeatureKind, InvalidStrategyError, LIB_CONFIG, LOCAL_STORAGE, LocalStorage, LocalStorageService, LocalStorageStrategy, SESSION_STORAGE, STORAGE_STRATEGIES, SessionStorage, SessionStorageService, SessionStorageStrategy, StorageStrategies, StorageStrategyStub, StorageStrategyStubName, StorageStub, StrategyCacheService, StrategyIndex, SyncStorage, makeNgxWebstorageFeature, provideNgxWebstorage, withLocalStorage, withNgxWebstorageConfig, withSessionStorage }; export type { NgxWebstorageFeature, NgxWebstorageFeatureKind, StorageStrategy, StrategyCache };