UNPKG

ng7-storage

Version:

An Angular service for browser session storage management with optional base64 encryption/decryption.

261 lines (256 loc) 7.58 kB
import * as _angular_core from '@angular/core'; import { InjectionToken, Provider } from '@angular/core'; import { Observable } from 'rxjs'; type StorageType = 'localStorage' | 'sessionStorage'; interface StorageItem<T = any> { value: T; timestamp: number; expiry?: number; encrypted?: boolean; } interface StorageConfig { prefix?: string; defaultTTL?: number; enableLogging?: boolean; caseSensitive?: boolean; storageType?: StorageType; } interface NamedStorageConfig { [name: string]: StorageConfig; } interface StorageFlags { autoCleanup?: boolean; strictMode?: boolean; enableMetrics?: boolean; } interface StorageStats { totalItems: number; totalSize: number; availableSpace: number; items: Array<{ key: string; size: number; timestamp: number; hasExpiry: boolean; }>; } interface StorageChangeEvent<T = any> { key: string; oldValue: T | null; newValue: T | null; action: 'set' | 'remove' | 'clear' | 'expire'; timestamp: number; } declare const STORAGE_OPTIONS: InjectionToken<StorageConfig>; declare const STORAGE_NAMED_OPTIONS: InjectionToken<NamedStorageConfig>; declare const STORAGE_FLAGS: InjectionToken<StorageFlags>; declare const DEFAULT_STORAGE_CONFIG: Required<StorageConfig>; declare const DEFAULT_STORAGE_FLAGS: Required<StorageFlags>; /** * Simple provider for basic configuration */ declare function provideNgStorageConfig(config: StorageConfig, flags?: StorageFlags): Provider[]; declare class NgStorageService { private readonly config; private readonly flags; private readonly storage; private readonly storageTypeName; private readonly supportedMessage; private readonly isSupported; private readonly isCryptoSupported; private readonly _storageData; private readonly _changeSubject; private readonly _keyChangeSubjects; readonly storageData: _angular_core.Signal<Record<string, any>>; readonly changes$: Observable<StorageChangeEvent<any>>; readonly stats: _angular_core.Signal<{ itemCount: number; isEmpty: boolean; keys: string[]; }>; constructor(options?: StorageConfig, flags?: StorageFlags); /** * Gets the appropriate storage instance based on configuration */ private getStorageInstance; /** * Checks if the configured storage is supported and available */ private checkStorageSupport; /** * Initializes reactive state from existing storage */ private initializeReactiveState; /** * Generates a storage key with prefix */ private generateKey; /** * Logs debug information if logging is enabled */ private log; /** * Encrypts data using AES-GCM encryption */ private encrypt; /** * Decrypts data using AES-GCM decryption */ private decrypt; /** * Calculates the expiry timestamp */ private calculateExpiry; /** * Checks if an item has expired */ private isExpired; /** * Emits change event */ private emitChange; /** * Updates reactive state */ private updateReactiveState; /** * Internal get data method without reactive updates */ private getDataInternal; /** * Removes expired items from storage */ private cleanupExpiredItems; /** * Stores data in storage with optional encryption and TTL */ setData<T = any>(key: string, value: T, options?: { encrypt?: boolean; ttlMinutes?: number; }): Promise<boolean>; /** * Retrieves data from storage with automatic decryption and expiry check */ getData<T = any>(key: string, options?: { decrypt?: boolean; defaultValue?: T; }): Promise<T | null>; /** * Creates a signal for a specific key */ createSignal<T = any>(key: string, defaultValue?: T): Promise<_angular_core.Signal<T | null>>; /** * Watch changes for a specific key */ watch<T = any>(key: string): Observable<T | null>; /** * Watch all changes */ watchAll(): Observable<StorageChangeEvent>; /** * Watch changes for multiple keys */ watchKeys<T = any>(keys: string[]): Observable<{ key: string; value: T | null; }>; /** * Watch changes by pattern (simple glob-like matching) */ watchPattern<T = any>(pattern: string): Observable<{ key: string; value: T | null; }>; /** * Removes data associated with the specified key */ removeData(key: string): boolean; /** * Removes multiple items at once */ removeMultiple(keys: string[]): { success: string[]; failed: string[]; }; /** * Clears all storage data with the current prefix */ removeAll(): boolean; /** * Checks if a key exists in storage */ hasKey(key: string): Promise<boolean>; /** * Gets all keys managed by this service */ getKeys(): string[]; /** * Gets storage statistics */ getStorageStats(): Promise<StorageStats>; /** * Updates existing data if key exists */ updateData<T = any>(key: string, updateFn: (currentValue: T | null) => T, options?: { encrypt?: boolean; ttlMinutes?: number; }): Promise<boolean>; /** * Sets data only if key doesn't exist */ setIfNotExists<T = any>(key: string, value: T, options?: { encrypt?: boolean; ttlMinutes?: number; }): Promise<boolean>; /** * Gets storage configuration */ getConfig(): Required<StorageConfig>; /** * Gets the current storage type */ getStorageType(): StorageType; /** * Switches storage type (creates a new instance) */ static withStorageType(config: Partial<StorageConfig> & { storageType: StorageType; }): NgStorageService; /** * Creates a localStorage instance */ static localStorage(config?: Partial<Omit<StorageConfig, 'storageType'>>): NgStorageService; /** * Creates a sessionStorage instance */ static sessionStorage(config?: Partial<Omit<StorageConfig, 'storageType'>>): NgStorageService; /** * Checks if storage is supported */ isStorageSupported(): boolean; /** * Checks if encryption is supported */ isEncryptionSupported(): boolean; /** * Forces cleanup of expired items */ cleanup(): Promise<number>; /** * Clears the encryption key (useful for key rotation) */ clearEncryptionKey(): void; /** * Destroys all subscriptions and cleanup */ destroy(): void; static ɵfac: _angular_core.ɵɵFactoryDeclaration<NgStorageService, [{ optional: true; }, { optional: true; }]>; static ɵprov: _angular_core.ɵɵInjectableDeclaration<NgStorageService>; } declare function provideNgStorage<T = any>(optionsFactory: () => StorageConfig, flags?: StorageFlags): Provider[]; /** * Provides multiple named NgStorageService instances */ declare function provideNamedNgStorage(optionsFactory: () => NamedStorageConfig, flags?: StorageFlags): Provider[]; export { DEFAULT_STORAGE_CONFIG, DEFAULT_STORAGE_FLAGS, NgStorageService, STORAGE_FLAGS, STORAGE_NAMED_OPTIONS, STORAGE_OPTIONS, provideNamedNgStorage, provideNgStorage, provideNgStorageConfig }; export type { NamedStorageConfig, StorageChangeEvent, StorageConfig, StorageFlags, StorageItem, StorageStats, StorageType };