import * as i0 from '@angular/core'; import { Signal, InjectionToken, EnvironmentProviders } from '@angular/core'; import * as ngwr_storage from 'ngwr/storage'; /** * Reactive key/value storage on top of any `Storage`-compatible engine. * * - **Swap engines** through {@link WR_STORAGE_ENGINE} or * {@link provideWrStorage}. Default: `localStorage` with an in-memory * fallback for SSR / private mode. * - **Namespacing** via `prefix` (config) — every key is transparently * prefixed on read / write. * - **TTL** per-call or default — values past their expiry are removed * lazily on the next `get` / `watch` read. * - **Reactive watch** — `watch(key)` returns a `Signal` that updates on * local writes and cross-tab `storage` events. * * SSR-safe: the default engine factory returns an in-memory shim when * `localStorage` is unavailable, so reads always work. * * @example * ```ts * const store = inject(WrStorage); * * store.set('user', { name: 'Ada' }); * store.get<{ name: string }>('user'); // { name: 'Ada' } * * store.set('cart', items, { ttl: 60_000 }); // expires in 60s * * const theme = store.watch<'light' | 'dark'>('theme', 'light'); * effect(() => console.log('theme is', theme())); * ``` * * @see https://ngwr.dev/services/storage */ declare class WrStorage { private readonly engine; private readonly config; private readonly isBrowser; /** Per-key watchers. Shared so multiple `watch(k)` calls reuse one signal. */ private readonly watchers; private listenerInstalled; /** Is `key` present (regardless of value / expiry)? */ has(key: string): boolean; /** * Read `key`. Returns `fallback` (default `null`) when the key is * missing or expired. Expired values are also removed as a side effect. */ get(key: string, fallback?: T | null): T | null; /** * Write `value` at `key`. Per-call `ttl` (ms) overrides the config * default. JSON-serializable values only when `json` is on (the default). */ set(key: string, value: T, opts?: { ttl?: number; }): void; /** Remove `key`. No-op when absent. */ remove(key: string): void; /** * Clear every key owned by this prefix. When `prefix` is empty this * defers to `engine.clear()` (clears everything — be careful with * shared storage). */ clear(): void; /** All known keys under our prefix, with the prefix stripped. */ keys(): readonly string[]; /** * Reactive read of `key`. Updates when this instance writes to the key, * when `clear()` runs, and when another tab updates `localStorage` * under the same prefix. Returned signal is shared per key — calling * `watch(k)` twice returns the same signal. */ watch(key: string, fallback?: T | null): Signal; private fullKey; private notify; private rawKeys; private installCrossTabListener; static ɵfac: i0.ɵɵFactoryDeclaration; static ɵprov: i0.ɵɵInjectableDeclaration; } /** * Storage backend. Anything implementing the native `Storage` interface * works — `localStorage`, `sessionStorage`, a Map-backed shim, an * IndexedDB sync wrapper, an encrypted wrapper, a worker bridge, … */ type WrStorageEngine = Storage; /** {@link WrStorage} configuration. Pass partial — merged with defaults. */ interface WrStorageConfig { /** * Prefix prepended to every key on read / write. Keeps your app's keys * from colliding with third-party libs sharing the same storage. * @default '' */ readonly prefix?: string; /** * Auto JSON-(de)serialize values. Disable to store / read raw strings * verbatim (useful when interop with non-ngwr code). * @default true */ readonly json?: boolean; /** * Default TTL in milliseconds applied to every `set()` without a * per-call override. `0` means no expiry. * @default 0 */ readonly ttl?: number; } /** Fully-resolved config (all defaults filled). @internal */ type WrStorageConfigResolved = Required; declare const DEFAULT_WR_STORAGE_CONFIG: WrStorageConfigResolved; declare const WR_STORAGE_CONFIG: InjectionToken>; /** * @license * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://github.com/thekhegay/ngwr/blob/main/LICENSE */ /** * In-memory implementation of `Storage`. Used as the SSR / private-mode * fallback. Exported so consumers can opt into it explicitly for tests. */ declare function createMemoryStorage(): WrStorageEngine; /** * Injection token for the active storage engine. Override at any * injector level to swap engines (e.g. a feature module uses * `sessionStorage`; the rest uses `localStorage`): * * ```ts * providers: [{ provide: WR_STORAGE_ENGINE, useValue: sessionStorage }] * ``` * * The default factory returns `localStorage` in the browser when it's * actually writable, an in-memory map otherwise. */ declare const WR_STORAGE_ENGINE: InjectionToken; /** * @license * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://github.com/thekhegay/ngwr/blob/main/LICENSE */ /** * Configure {@link WrStorage}. All fields optional — merged with defaults. * * @example Override the engine globally (e.g. session storage) * ```ts * provideWrStorage({ engine: sessionStorage }) * ``` * * @example Lazy engine factory (e.g. wrap localStorage with encryption) * ```ts * provideWrStorage({ engine: () => new EncryptedStorage(localStorage, key) }) * ``` * * @example Namespace + auto-expire * ```ts * provideWrStorage({ prefix: 'myapp:', ttl: 24 * 60 * 60 * 1000 }) * ``` */ interface ProvideWrStorageOptions extends WrStorageConfig { /** * Storage engine. Either an instance (eager) or a factory (lazy — * called inside the injection context the first time `WR_STORAGE_ENGINE` * is read). Defaults to {@link WR_STORAGE_ENGINE}'s factory * (`localStorage` with in-memory fallback). */ readonly engine?: WrStorageEngine | (() => WrStorageEngine); } declare function provideWrStorage(options?: ProvideWrStorageOptions): EnvironmentProviders; export { DEFAULT_WR_STORAGE_CONFIG, WR_STORAGE_CONFIG, WR_STORAGE_ENGINE, WrStorage, createMemoryStorage, provideWrStorage }; export type { ProvideWrStorageOptions, WrStorageConfig, WrStorageConfigResolved, WrStorageEngine };