@telperion/ng-pack 1.3.2 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,7 +1,52 @@
|
|
|
1
1
|
import { InjectionToken, inject } from '@angular/core';
|
|
2
|
-
import { ReactiveWebLocalStorage, ReactiveWebSessionStorage } from '@
|
|
2
|
+
import { ReactiveWebLocalStorage, ReactiveWebSessionStorage, ReactiveCookieStorage } from '@telperion/reactive-storage';
|
|
3
3
|
import { toSignal } from '@angular/core/rxjs-interop';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Creates a StorageSignal from a ReactiveStorage instance.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The type of value stored
|
|
9
|
+
* @param storage - The ReactiveStorage instance (localStorage, sessionStorage, or cookie storage)
|
|
10
|
+
* @param store - The storage namespace/store name
|
|
11
|
+
* @param key - The key within the store
|
|
12
|
+
* @returns A StorageSignal that provides reactive access to the stored value
|
|
13
|
+
*
|
|
14
|
+
* @remarks
|
|
15
|
+
* This is a low-level factory function that bridges Angular signals with ReactiveStorage.
|
|
16
|
+
* Most applications should use the higher-level functions:
|
|
17
|
+
* - {@link localStorageSignal} for localStorage
|
|
18
|
+
* - {@link sessionStorageSignal} for sessionStorage
|
|
19
|
+
* - {@link cookieStorageSignal} for cookies
|
|
20
|
+
*
|
|
21
|
+
* The function:
|
|
22
|
+
* 1. Converts the ReactiveStorage Observable into an Angular signal using `toSignal`
|
|
23
|
+
* 2. Creates a Proxy that intercepts calls to add `set`, `update`, and `delete` methods
|
|
24
|
+
* 3. Ensures all storage operations propagate to both the underlying storage and the signal
|
|
25
|
+
*
|
|
26
|
+
* **Proxy Pattern:**
|
|
27
|
+
* The returned signal is a Proxy that:
|
|
28
|
+
* - Delegates reading to the underlying signal
|
|
29
|
+
* - Provides `set()` to write values
|
|
30
|
+
* - Provides `update()` for functional updates
|
|
31
|
+
* - Provides `delete()` to remove the value
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* // Typically used internally by specialized signal creators
|
|
36
|
+
* import { ReactiveWebLocalStorage } from '@telperion/reactive-storage';
|
|
37
|
+
*
|
|
38
|
+
* const storage = new ReactiveWebLocalStorage('my-app');
|
|
39
|
+
* const userSignal = storageSignal<User>(storage, 'users', 'current');
|
|
40
|
+
*
|
|
41
|
+
* userSignal.set({ id: 1, name: 'Alice' });
|
|
42
|
+
* console.log(userSignal()); // { id: 1, name: 'Alice' }
|
|
43
|
+
*
|
|
44
|
+
* userSignal.update(user => ({ ...user, name: 'Bob' }));
|
|
45
|
+
* userSignal.delete();
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @internal This is primarily for internal use. Use the specialized signal creators instead.
|
|
49
|
+
*/
|
|
5
50
|
function storageSignal(storage, store, key) {
|
|
6
51
|
const source = toSignal(storage.get(store, key));
|
|
7
52
|
function setter(value) {
|
|
@@ -34,33 +79,594 @@ function storageSignal(storage, store, key) {
|
|
|
34
79
|
});
|
|
35
80
|
}
|
|
36
81
|
|
|
82
|
+
/**
|
|
83
|
+
* Injection token for the ReactiveWebLocalStorage singleton.
|
|
84
|
+
* @internal
|
|
85
|
+
*/
|
|
37
86
|
const LOCAL_STORAGE = new InjectionToken('Telperion Local Storage');
|
|
87
|
+
/**
|
|
88
|
+
* Provides a ReactiveWebLocalStorage instance for dependency injection.
|
|
89
|
+
*
|
|
90
|
+
* @param appName - Optional application name for namespacing localStorage keys
|
|
91
|
+
* @returns An Angular provider configuration
|
|
92
|
+
*
|
|
93
|
+
* @remarks
|
|
94
|
+
* This provider creates a singleton ReactiveWebLocalStorage instance that can be injected
|
|
95
|
+
* throughout your application. All `localStorageSignal` calls will use this shared instance,
|
|
96
|
+
* ensuring consistent storage access and change detection.
|
|
97
|
+
*
|
|
98
|
+
* **Namespacing:**
|
|
99
|
+
* The `appName` parameter prefixes all localStorage keys to prevent conflicts:
|
|
100
|
+
* - With appName: `my-app:store:key`
|
|
101
|
+
* - Without appName: `store:key`
|
|
102
|
+
*
|
|
103
|
+
* **Singleton Pattern:**
|
|
104
|
+
* Only one instance is created per application, ensuring:
|
|
105
|
+
* - Consistent storage access across components
|
|
106
|
+
* - Efficient memory usage
|
|
107
|
+
* - Reliable cross-component synchronization
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* // app.config.ts
|
|
112
|
+
* import { ApplicationConfig } from '@angular/core';
|
|
113
|
+
* import { provideLocalStorage } from '@telperion/ng-pack/storage-signals';
|
|
114
|
+
*
|
|
115
|
+
* export const appConfig: ApplicationConfig = {
|
|
116
|
+
* providers: [
|
|
117
|
+
* provideLocalStorage('my-app'),
|
|
118
|
+
* // ... other providers
|
|
119
|
+
* ]
|
|
120
|
+
* };
|
|
121
|
+
* ```
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```typescript
|
|
125
|
+
* // Without namespace (keys are not prefixed)
|
|
126
|
+
* export const appConfig: ApplicationConfig = {
|
|
127
|
+
* providers: [
|
|
128
|
+
* provideLocalStorage(),
|
|
129
|
+
* ]
|
|
130
|
+
* };
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* @public
|
|
134
|
+
*/
|
|
38
135
|
function provideLocalStorage(appName) {
|
|
39
136
|
return {
|
|
40
137
|
provide: LOCAL_STORAGE,
|
|
41
138
|
useValue: new ReactiveWebLocalStorage(appName)
|
|
42
139
|
};
|
|
43
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* Creates a reactive signal connected to browser's localStorage.
|
|
143
|
+
*
|
|
144
|
+
* @template T - The type of value stored
|
|
145
|
+
* @param store - The storage namespace/store name
|
|
146
|
+
* @param key - The key within the store
|
|
147
|
+
* @returns A StorageSignal that reactively tracks the localStorage value
|
|
148
|
+
*
|
|
149
|
+
* @remarks
|
|
150
|
+
* This function creates an Angular signal that automatically syncs with localStorage.
|
|
151
|
+
* Any changes to the value (via `set`, `update`, or `delete`) are immediately persisted
|
|
152
|
+
* to localStorage and propagate to all components using the same signal.
|
|
153
|
+
*
|
|
154
|
+
* **Requirements:**
|
|
155
|
+
* Must call {@link provideLocalStorage} in application config before using this function.
|
|
156
|
+
*
|
|
157
|
+
* **Storage Key:**
|
|
158
|
+
* The actual localStorage key is formed as:
|
|
159
|
+
* - With app name: `appName:store:key`
|
|
160
|
+
* - Without app name: `store:key`
|
|
161
|
+
*
|
|
162
|
+
* **Type Safety:**
|
|
163
|
+
* Values are automatically serialized/deserialized as JSON:
|
|
164
|
+
* - Objects and arrays are preserved
|
|
165
|
+
* - Primitives (string, number, boolean) work correctly
|
|
166
|
+
* - Functions and undefined values are not supported
|
|
167
|
+
*
|
|
168
|
+
* **Cross-Component Sync:**
|
|
169
|
+
* All components using the same `store` and `key` share the same signal state.
|
|
170
|
+
* Changes in one component immediately reflect in others.
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```typescript
|
|
174
|
+
* import { Component } from '@angular/core';
|
|
175
|
+
* import { localStorageSignal } from '@telperion/ng-pack/storage-signals';
|
|
176
|
+
*
|
|
177
|
+
* @Component({
|
|
178
|
+
* selector: 'app-settings',
|
|
179
|
+
* template: `
|
|
180
|
+
* <div>
|
|
181
|
+
* <p>Theme: {{ theme() }}</p>
|
|
182
|
+
* <button (click)="theme.set('dark')">Dark</button>
|
|
183
|
+
* <button (click)="theme.set('light')">Light</button>
|
|
184
|
+
* <button (click)="theme.delete()">Reset</button>
|
|
185
|
+
* </div>
|
|
186
|
+
* `
|
|
187
|
+
* })
|
|
188
|
+
* export class SettingsComponent {
|
|
189
|
+
* theme = localStorageSignal<string>('settings', 'theme');
|
|
190
|
+
* }
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```typescript
|
|
195
|
+
* // Complex types
|
|
196
|
+
* interface UserPreferences {
|
|
197
|
+
* theme: 'light' | 'dark';
|
|
198
|
+
* fontSize: number;
|
|
199
|
+
* notifications: boolean;
|
|
200
|
+
* }
|
|
201
|
+
*
|
|
202
|
+
* const prefs = localStorageSignal<UserPreferences>('user', 'preferences');
|
|
203
|
+
*
|
|
204
|
+
* // Set entire object
|
|
205
|
+
* prefs.set({ theme: 'dark', fontSize: 14, notifications: true });
|
|
206
|
+
*
|
|
207
|
+
* // Update specific property
|
|
208
|
+
* prefs.update(current => ({
|
|
209
|
+
* ...current,
|
|
210
|
+
* theme: current?.theme === 'dark' ? 'light' : 'dark'
|
|
211
|
+
* }));
|
|
212
|
+
* ```
|
|
213
|
+
*
|
|
214
|
+
* @throws Error if {@link provideLocalStorage} was not called in application config
|
|
215
|
+
*
|
|
216
|
+
* @public
|
|
217
|
+
*/
|
|
44
218
|
function localStorageSignal(store, key) {
|
|
45
219
|
const storage = inject(LOCAL_STORAGE);
|
|
46
220
|
return storageSignal(storage, store, key);
|
|
47
221
|
}
|
|
48
222
|
|
|
223
|
+
/**
|
|
224
|
+
* Injection token for the ReactiveWebSessionStorage singleton.
|
|
225
|
+
* @internal
|
|
226
|
+
*/
|
|
49
227
|
const SESSION_STORAGE = new InjectionToken('Telperion Session Storage');
|
|
228
|
+
/**
|
|
229
|
+
* Provides a ReactiveWebSessionStorage instance for dependency injection.
|
|
230
|
+
*
|
|
231
|
+
* @param appName - Optional application name for namespacing sessionStorage keys
|
|
232
|
+
* @returns An Angular provider configuration
|
|
233
|
+
*
|
|
234
|
+
* @remarks
|
|
235
|
+
* This provider creates a singleton ReactiveWebSessionStorage instance that can be injected
|
|
236
|
+
* throughout your application. All `sessionStorageSignal` calls will use this shared instance,
|
|
237
|
+
* ensuring consistent storage access and change detection.
|
|
238
|
+
*
|
|
239
|
+
* **SessionStorage vs LocalStorage:**
|
|
240
|
+
* - **sessionStorage**: Data persists only for the browser tab/window session (cleared on close)
|
|
241
|
+
* - **localStorage**: Data persists indefinitely until explicitly cleared
|
|
242
|
+
*
|
|
243
|
+
* **Use Cases for sessionStorage:**
|
|
244
|
+
* - Multi-step form wizards
|
|
245
|
+
* - Temporary authentication tokens
|
|
246
|
+
* - Per-session user preferences
|
|
247
|
+
* - Shopping cart for current session
|
|
248
|
+
* - Tab-specific state isolation
|
|
249
|
+
*
|
|
250
|
+
* **Namespacing:**
|
|
251
|
+
* The `appName` parameter prefixes all sessionStorage keys to prevent conflicts:
|
|
252
|
+
* - With appName: `my-app:store:key`
|
|
253
|
+
* - Without appName: `store:key`
|
|
254
|
+
*
|
|
255
|
+
* **Singleton Pattern:**
|
|
256
|
+
* Only one instance is created per application, ensuring:
|
|
257
|
+
* - Consistent storage access across components
|
|
258
|
+
* - Efficient memory usage
|
|
259
|
+
* - Reliable cross-component synchronization
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* ```typescript
|
|
263
|
+
* // app.config.ts
|
|
264
|
+
* import { ApplicationConfig } from '@angular/core';
|
|
265
|
+
* import { provideSessionStorage } from '@telperion/ng-pack/storage-signals';
|
|
266
|
+
*
|
|
267
|
+
* export const appConfig: ApplicationConfig = {
|
|
268
|
+
* providers: [
|
|
269
|
+
* provideSessionStorage('my-app'),
|
|
270
|
+
* // ... other providers
|
|
271
|
+
* ]
|
|
272
|
+
* };
|
|
273
|
+
* ```
|
|
274
|
+
*
|
|
275
|
+
* @example
|
|
276
|
+
* ```typescript
|
|
277
|
+
* // Without namespace
|
|
278
|
+
* export const appConfig: ApplicationConfig = {
|
|
279
|
+
* providers: [
|
|
280
|
+
* provideSessionStorage(),
|
|
281
|
+
* ]
|
|
282
|
+
* };
|
|
283
|
+
* ```
|
|
284
|
+
*
|
|
285
|
+
* @public
|
|
286
|
+
*/
|
|
50
287
|
function provideSessionStorage(appName) {
|
|
51
288
|
return {
|
|
52
289
|
provide: SESSION_STORAGE,
|
|
53
290
|
useValue: new ReactiveWebSessionStorage(appName)
|
|
54
291
|
};
|
|
55
292
|
}
|
|
293
|
+
/**
|
|
294
|
+
* Creates a reactive signal connected to browser's sessionStorage.
|
|
295
|
+
*
|
|
296
|
+
* @template T - The type of value stored
|
|
297
|
+
* @param store - The storage namespace/store name
|
|
298
|
+
* @param key - The key within the store
|
|
299
|
+
* @returns A StorageSignal that reactively tracks the sessionStorage value
|
|
300
|
+
*
|
|
301
|
+
* @remarks
|
|
302
|
+
* This function creates an Angular signal that automatically syncs with sessionStorage.
|
|
303
|
+
* Any changes to the value (via `set`, `update`, or `delete`) are immediately persisted
|
|
304
|
+
* to sessionStorage and propagate to all components using the same signal.
|
|
305
|
+
*
|
|
306
|
+
* **Session Lifecycle:**
|
|
307
|
+
* - Data is cleared when the browser tab/window is closed
|
|
308
|
+
* - Each tab has its own independent sessionStorage
|
|
309
|
+
* - Opening a page in a new tab creates fresh sessionStorage (even for same URL)
|
|
310
|
+
* - Refreshing the page preserves sessionStorage data
|
|
311
|
+
*
|
|
312
|
+
* **Requirements:**
|
|
313
|
+
* Must call {@link provideSessionStorage} in application config before using this function.
|
|
314
|
+
*
|
|
315
|
+
* **Storage Key:**
|
|
316
|
+
* The actual sessionStorage key is formed as:
|
|
317
|
+
* - With app name: `appName:store:key`
|
|
318
|
+
* - Without app name: `store:key`
|
|
319
|
+
*
|
|
320
|
+
* **Type Safety:**
|
|
321
|
+
* Values are automatically serialized/deserialized as JSON:
|
|
322
|
+
* - Objects and arrays are preserved
|
|
323
|
+
* - Primitives (string, number, boolean) work correctly
|
|
324
|
+
* - Functions and undefined values are not supported
|
|
325
|
+
*
|
|
326
|
+
* **Cross-Component Sync:**
|
|
327
|
+
* All components in the same tab using the same `store` and `key` share the same signal state.
|
|
328
|
+
* Changes in one component immediately reflect in others within the same tab.
|
|
329
|
+
* Different tabs do NOT share sessionStorage.
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* ```typescript
|
|
333
|
+
* import { Component } from '@angular/core';
|
|
334
|
+
* import { sessionStorageSignal } from '@telperion/ng-pack/storage-signals';
|
|
335
|
+
*
|
|
336
|
+
* @Component({
|
|
337
|
+
* selector: 'app-wizard',
|
|
338
|
+
* template: `
|
|
339
|
+
* <div>
|
|
340
|
+
* <p>Current Step: {{ currentStep() }}</p>
|
|
341
|
+
* <button (click)="nextStep()">Next</button>
|
|
342
|
+
* <button (click)="currentStep.delete()">Reset</button>
|
|
343
|
+
* </div>
|
|
344
|
+
* `
|
|
345
|
+
* })
|
|
346
|
+
* export class WizardComponent {
|
|
347
|
+
* currentStep = sessionStorageSignal<number>('wizard', 'step');
|
|
348
|
+
*
|
|
349
|
+
* nextStep() {
|
|
350
|
+
* this.currentStep.update(step => (step ?? 0) + 1);
|
|
351
|
+
* }
|
|
352
|
+
* }
|
|
353
|
+
* ```
|
|
354
|
+
*
|
|
355
|
+
* @example
|
|
356
|
+
* ```typescript
|
|
357
|
+
* // Form data that shouldn't persist across sessions
|
|
358
|
+
* interface FormData {
|
|
359
|
+
* email: string;
|
|
360
|
+
* message: string;
|
|
361
|
+
* }
|
|
362
|
+
*
|
|
363
|
+
* const formData = sessionStorageSignal<FormData>('contact', 'draft');
|
|
364
|
+
*
|
|
365
|
+
* // Auto-save form data (cleared when tab closes)
|
|
366
|
+
* formData.set({ email: 'user@example.com', message: 'Hello!' });
|
|
367
|
+
*
|
|
368
|
+
* // Update specific field
|
|
369
|
+
* formData.update(current => ({
|
|
370
|
+
* ...current,
|
|
371
|
+
* message: 'Updated message'
|
|
372
|
+
* }));
|
|
373
|
+
* ```
|
|
374
|
+
*
|
|
375
|
+
* @throws Error if {@link provideSessionStorage} was not called in application config
|
|
376
|
+
*
|
|
377
|
+
* @public
|
|
378
|
+
*/
|
|
56
379
|
function sessionStorageSignal(store, key) {
|
|
57
380
|
const storage = inject(SESSION_STORAGE);
|
|
58
381
|
return storageSignal(storage, store, key);
|
|
59
382
|
}
|
|
60
383
|
|
|
384
|
+
var _a;
|
|
385
|
+
/**
|
|
386
|
+
* Internal provider that manages ReactiveCookieStorage instances with different configurations.
|
|
387
|
+
*
|
|
388
|
+
* @remarks
|
|
389
|
+
* This class implements a registry pattern to ensure that cookie storages with identical
|
|
390
|
+
* configurations share the same underlying ReactiveCookieStorage instance. This is critical for:
|
|
391
|
+
* - Preventing duplicate cookie writes
|
|
392
|
+
* - Ensuring consistent change detection across components
|
|
393
|
+
* - Optimizing memory usage
|
|
394
|
+
*
|
|
395
|
+
* **Linker Pattern:**
|
|
396
|
+
* All instances created by this provider share a common linker (Symbol) to enable
|
|
397
|
+
* cross-instance synchronization even when using different option combinations.
|
|
398
|
+
*
|
|
399
|
+
* **Instance Deduplication:**
|
|
400
|
+
* Two `cookieStorageSignal` calls with the same options will reuse the same storage instance,
|
|
401
|
+
* preventing redundant cookie operations.
|
|
402
|
+
*
|
|
403
|
+
* @internal This class is not exported publicly
|
|
404
|
+
*/
|
|
405
|
+
class CookieStorageProvider {
|
|
406
|
+
appName;
|
|
407
|
+
baseOptions;
|
|
408
|
+
/**
|
|
409
|
+
* Valid cookie option keys that can be used to configure cookie storage.
|
|
410
|
+
* Filters out any invalid properties from being passed to cookie operations.
|
|
411
|
+
*/
|
|
412
|
+
static #VALID_OPTIONS = ['path', 'domain', 'secure', 'sameSite', 'expires', 'maxAge'];
|
|
413
|
+
/**
|
|
414
|
+
* Shared linker symbol used to synchronize all cookie storage instances
|
|
415
|
+
* created by this provider, enabling cross-instance change notifications.
|
|
416
|
+
*/
|
|
417
|
+
#linker = Symbol('ReactiveCookieStorage Linker');
|
|
418
|
+
/**
|
|
419
|
+
* Registry of ReactiveCookieStorage instances keyed by their configuration options.
|
|
420
|
+
* Ensures instances with identical options are reused.
|
|
421
|
+
*/
|
|
422
|
+
#stores = new Map();
|
|
423
|
+
/**
|
|
424
|
+
* Creates a new CookieStorageProvider with base configuration.
|
|
425
|
+
*
|
|
426
|
+
* @param appName - Optional application name for namespacing cookie keys
|
|
427
|
+
* @param baseOptions - Default cookie options applied to all storage instances
|
|
428
|
+
*/
|
|
429
|
+
constructor(appName, baseOptions = {}) {
|
|
430
|
+
this.appName = appName;
|
|
431
|
+
this.baseOptions = baseOptions;
|
|
432
|
+
const parsedOptions = this.#parseOptions(baseOptions);
|
|
433
|
+
this.#stores.set(parsedOptions, new ReactiveCookieStorage(appName, parsedOptions, this.#linker));
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Gets or creates a ReactiveCookieStorage instance with the specified options.
|
|
437
|
+
*
|
|
438
|
+
* @param options - Cookie options to override base options
|
|
439
|
+
* @returns A ReactiveCookieStorage instance (reused if options match existing instance)
|
|
440
|
+
*
|
|
441
|
+
* @remarks
|
|
442
|
+
* This method implements option merging and instance deduplication:
|
|
443
|
+
* 1. Merges provided options with base options (provided options take precedence)
|
|
444
|
+
* 2. Checks if an instance with these exact options already exists
|
|
445
|
+
* 3. Returns existing instance if found, otherwise creates and caches a new one
|
|
446
|
+
*
|
|
447
|
+
* All instances share the same linker for cross-instance synchronization.
|
|
448
|
+
*/
|
|
449
|
+
getStore(options = {}) {
|
|
450
|
+
const parsedOptions = this.#parseOptions({ ...this.baseOptions, ...options });
|
|
451
|
+
let existingKey = this.#getStoredOptions(parsedOptions);
|
|
452
|
+
if (!existingKey) {
|
|
453
|
+
this.#stores.set(parsedOptions, new ReactiveCookieStorage(this.appName, parsedOptions, this.#linker));
|
|
454
|
+
existingKey = parsedOptions;
|
|
455
|
+
}
|
|
456
|
+
return this.#stores.get(existingKey);
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Filters cookie options to include only valid properties.
|
|
460
|
+
*
|
|
461
|
+
* @param options - Raw options object that may contain invalid properties
|
|
462
|
+
* @returns Filtered options containing only valid cookie configuration properties
|
|
463
|
+
*/
|
|
464
|
+
#parseOptions(options) {
|
|
465
|
+
return Object.fromEntries(Object.entries(options).filter(([key]) => _a.#VALID_OPTIONS.includes(key)));
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Searches the registry for an existing options object that matches the provided options.
|
|
469
|
+
*
|
|
470
|
+
* @param options - Cookie options to search for
|
|
471
|
+
* @returns The stored options object if found, undefined otherwise
|
|
472
|
+
*
|
|
473
|
+
* @remarks
|
|
474
|
+
* Two options objects are considered equal if all valid cookie properties have identical values.
|
|
475
|
+
*/
|
|
476
|
+
#getStoredOptions(options) {
|
|
477
|
+
const stored = this.#stores.keys();
|
|
478
|
+
return stored.find(storedOpts => {
|
|
479
|
+
return _a.#VALID_OPTIONS.every(key => storedOpts[key] === options[key]);
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
_a = CookieStorageProvider;
|
|
484
|
+
/**
|
|
485
|
+
* Provides a CookieStorageProvider instance for dependency injection.
|
|
486
|
+
*
|
|
487
|
+
* @param appName - Optional application name for namespacing cookie keys
|
|
488
|
+
* @param options - Default cookie options applied to all cookie storage signals
|
|
489
|
+
* @returns An Angular provider configuration
|
|
490
|
+
*
|
|
491
|
+
* @remarks
|
|
492
|
+
* This provider creates a singleton CookieStorageProvider that manages all cookie storage
|
|
493
|
+
* instances in your application. All `cookieStorageSignal` calls will use this shared provider.
|
|
494
|
+
*
|
|
495
|
+
* **Cookie Namespacing:**
|
|
496
|
+
* The `appName` parameter prefixes all cookie names to prevent conflicts:
|
|
497
|
+
* - With appName: `my-app:store:key`
|
|
498
|
+
* - Without appName: `store:key`
|
|
499
|
+
*
|
|
500
|
+
* **Base Options:**
|
|
501
|
+
* The `options` parameter provides default cookie configuration for all signals.
|
|
502
|
+
* Individual signals can override these options when created.
|
|
503
|
+
*
|
|
504
|
+
* **Common Options:**
|
|
505
|
+
* - `path` - Cookie path (default: current path)
|
|
506
|
+
* - `domain` - Cookie domain (default: current domain)
|
|
507
|
+
* - `secure` - Require HTTPS (recommended for production)
|
|
508
|
+
* - `sameSite` - CSRF protection ('strict' | 'lax' | 'none')
|
|
509
|
+
* - `maxAge` - Expiry time in seconds
|
|
510
|
+
* - `expires` - Expiry as Date object
|
|
511
|
+
*
|
|
512
|
+
* **Instance Management:**
|
|
513
|
+
* The provider automatically deduplicates storage instances:
|
|
514
|
+
* - Signals with identical options share the same storage instance
|
|
515
|
+
* - All instances are linked for cross-instance synchronization
|
|
516
|
+
* - Memory-efficient for applications with many cookie signals
|
|
517
|
+
*
|
|
518
|
+
* @example
|
|
519
|
+
* ```typescript
|
|
520
|
+
* // app.config.ts
|
|
521
|
+
* import { ApplicationConfig } from '@angular/core';
|
|
522
|
+
* import { provideCookieStorage } from '@telperion/ng-pack/storage-signals';
|
|
523
|
+
*
|
|
524
|
+
* export const appConfig: ApplicationConfig = {
|
|
525
|
+
* providers: [
|
|
526
|
+
* provideCookieStorage('my-app', {
|
|
527
|
+
* path: '/',
|
|
528
|
+
* secure: true,
|
|
529
|
+
* sameSite: 'strict',
|
|
530
|
+
* maxAge: 86400 // 24 hours
|
|
531
|
+
* }),
|
|
532
|
+
* ]
|
|
533
|
+
* };
|
|
534
|
+
* ```
|
|
535
|
+
*
|
|
536
|
+
* @example
|
|
537
|
+
* ```typescript
|
|
538
|
+
* // Minimal configuration
|
|
539
|
+
* export const appConfig: ApplicationConfig = {
|
|
540
|
+
* providers: [
|
|
541
|
+
* provideCookieStorage('my-app'),
|
|
542
|
+
* ]
|
|
543
|
+
* };
|
|
544
|
+
* ```
|
|
545
|
+
*
|
|
546
|
+
* @public
|
|
547
|
+
*/
|
|
548
|
+
function provideCookieStorage(appName, options) {
|
|
549
|
+
return {
|
|
550
|
+
provide: CookieStorageProvider,
|
|
551
|
+
useValue: new CookieStorageProvider(appName, options)
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* Creates a reactive signal connected to browser cookies.
|
|
556
|
+
*
|
|
557
|
+
* @template T - The type of value stored
|
|
558
|
+
* @param store - The cookie namespace/store name
|
|
559
|
+
* @param key - The cookie key within the store
|
|
560
|
+
* @param options - Optional cookie-specific options that override provider defaults
|
|
561
|
+
* @returns A StorageSignal that reactively tracks the cookie value
|
|
562
|
+
*
|
|
563
|
+
* @remarks
|
|
564
|
+
* This function creates an Angular signal that automatically syncs with browser cookies.
|
|
565
|
+
* Any changes to the value (via `set`, `update`, or `delete`) are immediately written
|
|
566
|
+
* to cookies and propagate to all components using the same signal.
|
|
567
|
+
*
|
|
568
|
+
* **Requirements:**
|
|
569
|
+
* Must call {@link provideCookieStorage} in application config before using this function.
|
|
570
|
+
*
|
|
571
|
+
* **Cookie Name:**
|
|
572
|
+
* The actual cookie name is formed as:
|
|
573
|
+
* - With app name: `appName:store:key`
|
|
574
|
+
* - Without app name: `store:key`
|
|
575
|
+
*
|
|
576
|
+
* **Size Limitations:**
|
|
577
|
+
* Cookies have a ~4KB size limit. The underlying ReactiveCookieStorage enforces a 4000 byte
|
|
578
|
+
* limit and throws an error if exceeded. Use localStorage for larger data.
|
|
579
|
+
*
|
|
580
|
+
* **Options Merging:**
|
|
581
|
+
* Cookie options are merged in this order (later overrides earlier):
|
|
582
|
+
* 1. Provider base options (from `provideCookieStorage`)
|
|
583
|
+
* 2. Signal-specific options (from this function's `options` parameter)
|
|
584
|
+
*
|
|
585
|
+
* **Instance Deduplication:**
|
|
586
|
+
* Signals with identical merged options share the same underlying storage instance:
|
|
587
|
+
* ```typescript
|
|
588
|
+
* // These share the same storage instance
|
|
589
|
+
* const token1 = cookieStorageSignal('auth', 'token', { secure: true });
|
|
590
|
+
* const token2 = cookieStorageSignal('auth', 'token', { secure: true });
|
|
591
|
+
*
|
|
592
|
+
* // These use different instances (different options)
|
|
593
|
+
* const temp = cookieStorageSignal('auth', 'token', { maxAge: 300 });
|
|
594
|
+
* ```
|
|
595
|
+
*
|
|
596
|
+
* **Type Safety:**
|
|
597
|
+
* Values are automatically serialized/deserialized as JSON:
|
|
598
|
+
* - Objects and arrays are preserved
|
|
599
|
+
* - Primitives (string, number, boolean) work correctly
|
|
600
|
+
* - Functions and undefined values are not supported
|
|
601
|
+
*
|
|
602
|
+
* **Security Best Practices:**
|
|
603
|
+
* - Use `secure: true` in production (HTTPS only)
|
|
604
|
+
* - Use `sameSite: 'strict'` for CSRF protection
|
|
605
|
+
* - Set appropriate `maxAge` or `expires` for sensitive data
|
|
606
|
+
* - Never store sensitive data in cookies without encryption
|
|
607
|
+
*
|
|
608
|
+
* @example
|
|
609
|
+
* ```typescript
|
|
610
|
+
* import { Component } from '@angular/core';
|
|
611
|
+
* import { cookieStorageSignal } from '@telperion/ng-pack/storage-signals';
|
|
612
|
+
*
|
|
613
|
+
* @Component({
|
|
614
|
+
* selector: 'app-auth',
|
|
615
|
+
* template: `
|
|
616
|
+
* <div>
|
|
617
|
+
* <p>Token: {{ authToken() }}</p>
|
|
618
|
+
* <button (click)="login()">Login</button>
|
|
619
|
+
* <button (click)="logout()">Logout</button>
|
|
620
|
+
* </div>
|
|
621
|
+
* `
|
|
622
|
+
* })
|
|
623
|
+
* export class AuthComponent {
|
|
624
|
+
* // Secure cookie with 1 hour expiry
|
|
625
|
+
* authToken = cookieStorageSignal<string>('auth', 'token', {
|
|
626
|
+
* secure: true,
|
|
627
|
+
* sameSite: 'strict',
|
|
628
|
+
* maxAge: 3600
|
|
629
|
+
* });
|
|
630
|
+
*
|
|
631
|
+
* login() {
|
|
632
|
+
* this.authToken.set('abc123');
|
|
633
|
+
* }
|
|
634
|
+
*
|
|
635
|
+
* logout() {
|
|
636
|
+
* this.authToken.delete();
|
|
637
|
+
* }
|
|
638
|
+
* }
|
|
639
|
+
* ```
|
|
640
|
+
*
|
|
641
|
+
* @example
|
|
642
|
+
* ```typescript
|
|
643
|
+
* // User preferences with different cookie lifetimes
|
|
644
|
+
* const theme = cookieStorageSignal<string>('prefs', 'theme', {
|
|
645
|
+
* maxAge: 31536000 // 1 year
|
|
646
|
+
* });
|
|
647
|
+
*
|
|
648
|
+
* const tempSetting = cookieStorageSignal<boolean>('prefs', 'banner-dismissed', {
|
|
649
|
+
* maxAge: 86400 // 1 day
|
|
650
|
+
* });
|
|
651
|
+
*
|
|
652
|
+
* theme.set('dark');
|
|
653
|
+
* tempSetting.set(true);
|
|
654
|
+
* ```
|
|
655
|
+
*
|
|
656
|
+
* @throws Error if {@link provideCookieStorage} was not called in application config
|
|
657
|
+
* @throws Error if the serialized value exceeds 4000 bytes
|
|
658
|
+
*
|
|
659
|
+
* @public
|
|
660
|
+
*/
|
|
661
|
+
function cookieStorageSignal(store, key, options) {
|
|
662
|
+
const provider = inject(CookieStorageProvider);
|
|
663
|
+
const storage = provider.getStore(options);
|
|
664
|
+
return storageSignal(storage, store, key);
|
|
665
|
+
}
|
|
666
|
+
|
|
61
667
|
/**
|
|
62
668
|
* Generated bundle index. Do not edit.
|
|
63
669
|
*/
|
|
64
670
|
|
|
65
|
-
export { localStorageSignal, provideLocalStorage, provideSessionStorage, sessionStorageSignal };
|
|
671
|
+
export { cookieStorageSignal, localStorageSignal, provideCookieStorage, provideLocalStorage, provideSessionStorage, sessionStorageSignal };
|
|
66
672
|
//# sourceMappingURL=telperion-ng-pack-storage-signals.mjs.map
|