@telperion/reactive-storage 1.1.0

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Telperion Technology
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,307 @@
1
+ # @telperion/reactive-storage
2
+
3
+ [![npm version](https://badge.fury.io/js/%40telperion%2Freactive-storage.svg)](https://badge.fury.io/js/%40telperion%2Freactive-storage)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ Reactive RxJS-based wrapper for browser storage APIs (localStorage and sessionStorage). Provides an Observable-based API for real-time storage updates with support for nested property access and automatic synchronization.
7
+
8
+ ## Features
9
+
10
+ - 🔄 **Reactive API**: Built on RxJS Observables for automatic change detection
11
+ - 📦 **Type-Safe**: Full TypeScript support with generics
12
+ - 🎯 **Nested Access**: Support for dot notation to access deep object properties
13
+ - 🔒 **Immutable**: Deep-frozen data guarantees immutability
14
+ - ⚡ **FIFO Queue**: Ordered write operations prevent race conditions
15
+ - 🌲 **Tree-Shakeable**: Import only what you need
16
+ - 🎨 **Framework Agnostic**: Works with any framework or vanilla JS
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ npm install @telperion/reactive-storage
22
+ ```
23
+
24
+ ```bash
25
+ yarn add @telperion/reactive-storage
26
+ ```
27
+
28
+ ```bash
29
+ pnpm add @telperion/reactive-storage
30
+ ```
31
+
32
+ ## Quick Start
33
+
34
+ ```typescript
35
+ import { ReactiveWebLocalStorage } from '@telperion/reactive-storage';
36
+
37
+ // Create storage instance
38
+ const storage = new ReactiveWebLocalStorage('my-app');
39
+
40
+ // Subscribe to changes
41
+ storage.get('user', 'profile.name').subscribe(name => {
42
+ console.log('User name:', name);
43
+ });
44
+
45
+ // Update storage
46
+ await storage.set('user', 'profile.name', 'John Doe');
47
+ // Console logs: "User name: John Doe"
48
+ ```
49
+
50
+ ## API
51
+
52
+ ### Classes
53
+
54
+ #### `ReactiveWebLocalStorage<S extends string>`
55
+
56
+ Reactive wrapper for `localStorage`. Data persists across browser sessions.
57
+
58
+ ```typescript
59
+ const storage = new ReactiveWebLocalStorage(appName?: string);
60
+ ```
61
+
62
+ **Parameters:**
63
+ - `appName` (optional): Application namespace (default: `'app'`). Used to prefix storage keys.
64
+
65
+ #### `ReactiveWebSessionStorage<S extends string>`
66
+
67
+ Reactive wrapper for `sessionStorage`. Data is cleared when the browser tab is closed.
68
+
69
+ ```typescript
70
+ const storage = new ReactiveWebSessionStorage(appName?: string);
71
+ ```
72
+
73
+ **Parameters:**
74
+ - `appName` (optional): Application namespace (default: `'app'`)
75
+
76
+ #### `ReactiveWebStorage<S extends string>`
77
+
78
+ Generic reactive storage wrapper. Allows using any `Storage` implementation.
79
+
80
+ ```typescript
81
+ const storage = new ReactiveWebStorage(
82
+ storage: Storage,
83
+ appName?: string
84
+ );
85
+ ```
86
+
87
+ ### Methods
88
+
89
+ All methods are available on storage instances:
90
+
91
+ #### `get<T>(storeName: string, key?: string): Observable<T>`
92
+
93
+ Get an Observable that emits the current value and all future updates.
94
+
95
+ **Parameters:**
96
+ - `storeName`: Name of the storage store
97
+ - `key` (optional): Property path using dot notation (e.g., `'user.settings.theme'`). Empty string or omit for entire store.
98
+
99
+ **Returns:** `Observable<T>` that emits on value changes
100
+
101
+ **Example:**
102
+ ```typescript
103
+ // Get entire store
104
+ storage.get('user').subscribe(user => console.log(user));
105
+
106
+ // Get nested property
107
+ storage.get('user', 'profile.email').subscribe(email => console.log(email));
108
+
109
+ // Typed return
110
+ storage.get<string>('user', 'profile.name').subscribe(name => {
111
+ // name is typed as string
112
+ });
113
+ ```
114
+
115
+ #### `set(storeName: string, key: string, value: any): Promise<void>`
116
+
117
+ Set a value in storage. Overwrites existing value. Pass `undefined` to remove.
118
+
119
+ **Example:**
120
+ ```typescript
121
+ // Set entire store
122
+ await storage.set('user', '', { name: 'John', age: 30 });
123
+
124
+ // Set nested property
125
+ await storage.set('user', 'profile.name', 'John Doe');
126
+
127
+ // Remove a value
128
+ await storage.set('user', 'profile.name', undefined);
129
+ ```
130
+
131
+ #### `patch(storeName: string, key: string, value: any): Promise<void>`
132
+
133
+ Merge a partial object into existing value. Only works with objects.
134
+
135
+ **Example:**
136
+ ```typescript
137
+ await storage.set('user', '', { name: 'John', age: 30 });
138
+
139
+ // Merge partial data
140
+ await storage.patch('user', '', { age: 31, email: 'john@example.com' });
141
+ // Result: { name: 'John', age: 31, email: 'john@example.com' }
142
+ ```
143
+
144
+ #### `delete(storeName: string, key?: string): Promise<void>`
145
+
146
+ Delete a value from storage.
147
+
148
+ **Example:**
149
+ ```typescript
150
+ // Delete a nested property
151
+ await storage.delete('user', 'profile.email');
152
+
153
+ // Delete entire store
154
+ await storage.delete('user');
155
+ ```
156
+
157
+ #### `push(storeName: string, key: string, ...values: any[]): Promise<void>`
158
+
159
+ Add items to an array in storage.
160
+
161
+ **Example:**
162
+ ```typescript
163
+ await storage.set('todos', 'items', ['Task 1']);
164
+
165
+ await storage.push('todos', 'items', 'Task 2', 'Task 3');
166
+ // Result: ['Task 1', 'Task 2', 'Task 3']
167
+ ```
168
+
169
+ #### `drop(storeName: string, key: string, value: any): Promise<void>`
170
+
171
+ Remove a specific value from an array in storage.
172
+
173
+ **Example:**
174
+ ```typescript
175
+ await storage.set('todos', 'items', ['Task 1', 'Task 2', 'Task 3']);
176
+
177
+ await storage.drop('todos', 'items', 'Task 2');
178
+ // Result: ['Task 1', 'Task 3']
179
+ ```
180
+
181
+ ## Usage Examples
182
+
183
+ ### Basic Usage
184
+
185
+ ```typescript
186
+ import { ReactiveWebLocalStorage } from '@telperion/reactive-storage';
187
+
188
+ const storage = new ReactiveWebLocalStorage('my-app');
189
+
190
+ // Subscribe to changes
191
+ const subscription = storage.get('settings', 'theme').subscribe(theme => {
192
+ document.body.setAttribute('data-theme', theme || 'light');
193
+ });
194
+
195
+ // Update value
196
+ await storage.set('settings', 'theme', 'dark');
197
+
198
+ // Clean up
199
+ subscription.unsubscribe();
200
+ ```
201
+
202
+ ### Working with Nested Objects
203
+
204
+ ```typescript
205
+ // Initialize user data
206
+ await storage.set('user', '', {
207
+ profile: {
208
+ name: 'John Doe',
209
+ email: 'john@example.com'
210
+ },
211
+ preferences: {
212
+ notifications: true,
213
+ language: 'en'
214
+ }
215
+ });
216
+
217
+ // Subscribe to nested property
218
+ storage.get('user', 'preferences.notifications').subscribe(enabled => {
219
+ console.log('Notifications:', enabled);
220
+ });
221
+
222
+ // Update nested property
223
+ await storage.set('user', 'preferences.language', 'es');
224
+ ```
225
+
226
+ ### Working with Arrays
227
+
228
+ ```typescript
229
+ // Initialize array
230
+ await storage.set('todos', 'items', []);
231
+
232
+ // Add items
233
+ await storage.push('todos', 'items',
234
+ { id: 1, text: 'Buy milk', done: false },
235
+ { id: 2, text: 'Walk dog', done: false }
236
+ );
237
+
238
+ // Subscribe to changes
239
+ storage.get('todos', 'items').subscribe(todos => {
240
+ console.log('Total todos:', todos.length);
241
+ });
242
+
243
+ // Remove item
244
+ const itemToRemove = { id: 1, text: 'Buy milk', done: false };
245
+ await storage.drop('todos', 'items', itemToRemove);
246
+ ```
247
+
248
+ ### Type Safety
249
+
250
+ ```typescript
251
+ interface User {
252
+ name: string;
253
+ email: string;
254
+ age: number;
255
+ }
256
+
257
+ type StoreName = 'user' | 'settings' | 'todos';
258
+
259
+ const storage = new ReactiveWebLocalStorage<StoreName>('my-app');
260
+
261
+ // TypeScript will enforce valid store names
262
+ storage.get<User>('user').subscribe(user => {
263
+ // user is typed as User
264
+ console.log(user.name.toUpperCase());
265
+ });
266
+
267
+ // This would cause a TypeScript error:
268
+ // storage.get('invalid-store'); ❌
269
+ ```
270
+
271
+ ### Integration with Framework-Specific Wrappers
272
+
273
+ This library serves as the foundation for framework-specific integrations:
274
+
275
+ - **Angular**: See [@telperion/ng-pack](https://www.npmjs.com/package/@telperion/ng-pack) for Signal-based storage
276
+ - **React**: See [@telperion/react-utils](https://www.npmjs.com/package/@telperion/react-utils) for hooks-based storage
277
+
278
+ ## How It Works
279
+
280
+ 1. **Storage Namespacing**: Data is stored with app-prefixed keys (e.g., `my-app/user`)
281
+ 2. **JSON Serialization**: All data is serialized to JSON for storage compatibility
282
+ 3. **Change Tracking**: Internal Subject emits changes that update all subscribers
283
+ 4. **FIFO Queue**: Write operations are queued using `@FifoPromise` decorator to prevent race conditions
284
+ 5. **Immutability**: All emitted values are deep-frozen to prevent accidental mutations
285
+
286
+ ## Browser Compatibility
287
+
288
+ Works in all modern browsers that support:
289
+ - Web Storage API (localStorage/sessionStorage)
290
+ - ES2015+ features
291
+ - RxJS 7+
292
+
293
+ ## Building
294
+
295
+ ```bash
296
+ pnpm nx run reactive-storage:build
297
+ ```
298
+
299
+ ## Testing
300
+
301
+ ```bash
302
+ pnpm nx run reactive-storage:test
303
+ ```
304
+
305
+ ## License
306
+
307
+ MIT © Telperion Technology
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ // Copied from https://github.com/ReactiveX/rxjs/blob/master/packages/observable/src/observable.ts
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.toAsyncIterator = toAsyncIterator;
5
+ function toAsyncIterator(observable) {
6
+ let subscription;
7
+ let hasError = false;
8
+ let error;
9
+ let completed = false;
10
+ const values = [];
11
+ const deferreds = [];
12
+ const handleError = (err) => {
13
+ hasError = true;
14
+ error = err;
15
+ while (deferreds.length) {
16
+ const [_, reject] = deferreds.shift();
17
+ reject(err);
18
+ }
19
+ };
20
+ const handleComplete = () => {
21
+ completed = true;
22
+ while (deferreds.length) {
23
+ const [resolve] = deferreds.shift();
24
+ resolve({ value: undefined, done: true });
25
+ }
26
+ };
27
+ return {
28
+ next: () => {
29
+ if (!subscription) {
30
+ // We only want to start the subscription when the user starts iterating.
31
+ subscription = observable.subscribe({
32
+ next: (value) => {
33
+ if (deferreds.length) {
34
+ const [resolve] = deferreds.shift();
35
+ resolve({ value, done: false });
36
+ }
37
+ else {
38
+ values.push(value);
39
+ }
40
+ },
41
+ error: handleError,
42
+ complete: handleComplete,
43
+ });
44
+ }
45
+ // If we already have some values in our buffer, we'll return the next one.
46
+ if (values.length) {
47
+ return Promise.resolve({ value: values.shift(), done: false });
48
+ }
49
+ // This was already completed, so we're just going to return a done result.
50
+ if (completed) {
51
+ return Promise.resolve({ value: undefined, done: true });
52
+ }
53
+ // There was an error, so we're going to return an error result.
54
+ if (hasError) {
55
+ return Promise.reject(error);
56
+ }
57
+ // Otherwise, we need to make them wait for a value.
58
+ return new Promise((resolve, reject) => {
59
+ deferreds.push([resolve, reject]);
60
+ });
61
+ },
62
+ throw: (err) => {
63
+ subscription?.unsubscribe();
64
+ // NOTE: I did some research on this, and as of Feb 2023, Chrome doesn't seem to do
65
+ // anything with pending promises returned from `next()` when `throw()` is called.
66
+ // However, for consumption of observables, I don't want RxJS taking the heat for that
67
+ // quirk/leak of the type. So we're going to reject all pending promises we've nexted out here.
68
+ handleError(err);
69
+ return Promise.reject(err);
70
+ },
71
+ return: () => {
72
+ subscription?.unsubscribe();
73
+ // NOTE: I did some research on this, and as of Feb 2023, Chrome doesn't seem to do
74
+ // anything with pending promises returned from `next()` when `throw()` is called.
75
+ // However, for consumption of observables, I don't want RxJS taking the heat for that
76
+ // quirk/leak of the type. So we're going to resolve all pending promises we've nexted out here.
77
+ handleComplete();
78
+ return Promise.resolve({ value: undefined, done: true });
79
+ },
80
+ [Symbol.asyncIterator]() {
81
+ return this;
82
+ },
83
+ async [Symbol.asyncDispose]() {
84
+ subscription?.unsubscribe();
85
+ },
86
+ };
87
+ }
@@ -0,0 +1,2 @@
1
+ import type { Observable } from "rxjs";
2
+ export declare function toAsyncIterator<T>(observable: Observable<T>): AsyncGenerator<T, void, void>;
@@ -0,0 +1,86 @@
1
+ // Copied from https://github.com/ReactiveX/rxjs/blob/master/packages/observable/src/observable.ts
2
+ export function toAsyncIterator(observable) {
3
+ let subscription;
4
+ let hasError = false;
5
+ let error;
6
+ let completed = false;
7
+ const values = [];
8
+ const deferreds = [];
9
+ const handleError = (err) => {
10
+ hasError = true;
11
+ error = err;
12
+ while (deferreds.length) {
13
+ const [_, reject] = deferreds.shift();
14
+ reject(err);
15
+ }
16
+ };
17
+ const handleComplete = () => {
18
+ completed = true;
19
+ while (deferreds.length) {
20
+ const [resolve] = deferreds.shift();
21
+ resolve({ value: undefined, done: true });
22
+ }
23
+ };
24
+ return {
25
+ next: () => {
26
+ if (!subscription) {
27
+ // We only want to start the subscription when the user starts iterating.
28
+ subscription = observable.subscribe({
29
+ next: (value) => {
30
+ if (deferreds.length) {
31
+ const [resolve] = deferreds.shift();
32
+ resolve({ value, done: false });
33
+ }
34
+ else {
35
+ values.push(value);
36
+ }
37
+ },
38
+ error: handleError,
39
+ complete: handleComplete,
40
+ });
41
+ }
42
+ // If we already have some values in our buffer, we'll return the next one.
43
+ if (values.length) {
44
+ return Promise.resolve({ value: values.shift(), done: false });
45
+ }
46
+ // This was already completed, so we're just going to return a done result.
47
+ if (completed) {
48
+ return Promise.resolve({ value: undefined, done: true });
49
+ }
50
+ // There was an error, so we're going to return an error result.
51
+ if (hasError) {
52
+ return Promise.reject(error);
53
+ }
54
+ // Otherwise, we need to make them wait for a value.
55
+ return new Promise((resolve, reject) => {
56
+ deferreds.push([resolve, reject]);
57
+ });
58
+ },
59
+ throw: (err) => {
60
+ subscription?.unsubscribe();
61
+ // NOTE: I did some research on this, and as of Feb 2023, Chrome doesn't seem to do
62
+ // anything with pending promises returned from `next()` when `throw()` is called.
63
+ // However, for consumption of observables, I don't want RxJS taking the heat for that
64
+ // quirk/leak of the type. So we're going to reject all pending promises we've nexted out here.
65
+ handleError(err);
66
+ return Promise.reject(err);
67
+ },
68
+ return: () => {
69
+ subscription?.unsubscribe();
70
+ // NOTE: I did some research on this, and as of Feb 2023, Chrome doesn't seem to do
71
+ // anything with pending promises returned from `next()` when `throw()` is called.
72
+ // However, for consumption of observables, I don't want RxJS taking the heat for that
73
+ // quirk/leak of the type. So we're going to resolve all pending promises we've nexted out here.
74
+ handleComplete();
75
+ return Promise.resolve({ value: undefined, done: true });
76
+ },
77
+ [Symbol.asyncIterator]() {
78
+ return this;
79
+ },
80
+ async [Symbol.asyncDispose]() {
81
+ subscription?.unsubscribe();
82
+ },
83
+ };
84
+ }
85
+
86
+ //# sourceMappingURL=async-iterator.polyfill.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../libs/reactive-storage/src/async-iterator.polyfill.ts"],"names":[],"mappings":"AAAA,kGAAkG;AAIlG,MAAM,UAAU,eAAe,CAAI,UAAyB;IAE1D,IAAI,YAAsC,CAAC;IAC3C,IAAI,QAAQ,GAAG,KAAK,CAAC;IACrB,IAAI,KAAc,CAAC;IACnB,IAAI,SAAS,GAAG,KAAK,CAAC;IACtB,MAAM,MAAM,GAAQ,EAAE,CAAC;IACvB,MAAM,SAAS,GAAsE,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;KACF,CAAC;AACJ,CAAC","file":"async-iterator.polyfill.js","sourcesContent":["// Copied from https://github.com/ReactiveX/rxjs/blob/master/packages/observable/src/observable.ts\n\nimport type { Observable, Subscription } from \"rxjs\";\n\nexport function toAsyncIterator<T>(observable: Observable<T>): AsyncGenerator<T, void, void> {\n\n let subscription: Subscription | undefined;\n let hasError = false;\n let error: unknown;\n let completed = false;\n const values: T[] = [];\n const deferreds: [(value: IteratorResult<T>) => void, (reason: unknown) => void][] = [];\n const handleError = (err: unknown) => {\n hasError = true;\n error = err;\n\n while (deferreds.length) {\n const [_, reject] = deferreds.shift()!;\n reject(err);\n }\n };\n const handleComplete = () => {\n completed = true;\n\n while (deferreds.length) {\n const [resolve] = deferreds.shift()!;\n resolve({ value: undefined, done: true });\n }\n };\n\n return {\n next: (): Promise<IteratorResult<T>> => {\n if (!subscription) {\n // We only want to start the subscription when the user starts iterating.\n subscription = observable.subscribe({\n next: (value) => {\n if (deferreds.length) {\n const [resolve] = deferreds.shift()!;\n resolve({ value, done: false });\n } else {\n values.push(value);\n }\n },\n error: handleError,\n complete: handleComplete,\n });\n }\n\n // If we already have some values in our buffer, we'll return the next one.\n if (values.length) {\n return Promise.resolve({ value: values.shift()!, done: false });\n }\n\n // This was already completed, so we're just going to return a done result.\n if (completed) {\n return Promise.resolve({ value: undefined, done: true });\n }\n\n // There was an error, so we're going to return an error result.\n if (hasError) {\n return Promise.reject(error);\n }\n\n // Otherwise, we need to make them wait for a value.\n return new Promise((resolve, reject) => {\n deferreds.push([resolve, reject]);\n });\n },\n throw: (err): Promise<IteratorResult<T>> => {\n subscription?.unsubscribe();\n // NOTE: I did some research on this, and as of Feb 2023, Chrome doesn't seem to do\n // anything with pending promises returned from `next()` when `throw()` is called.\n // However, for consumption of observables, I don't want RxJS taking the heat for that\n // quirk/leak of the type. So we're going to reject all pending promises we've nexted out here.\n handleError(err);\n\n return Promise.reject(err);\n },\n return: (): Promise<IteratorResult<T>> => {\n subscription?.unsubscribe();\n // NOTE: I did some research on this, and as of Feb 2023, Chrome doesn't seem to do\n // anything with pending promises returned from `next()` when `throw()` is called.\n // However, for consumption of observables, I don't want RxJS taking the heat for that\n // quirk/leak of the type. So we're going to resolve all pending promises we've nexted out here.\n handleComplete();\n\n return Promise.resolve({ value: undefined, done: true });\n },\n [Symbol.asyncIterator]() {\n return this;\n },\n async [Symbol.asyncDispose]() {\n subscription?.unsubscribe();\n },\n };\n}\n"]}
package/index.cjs ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./reactive-storage"), exports);
5
+ tslib_1.__exportStar(require("./reactive-web-storage"), exports);
6
+ tslib_1.__exportStar(require("./reactive-web-local-storage"), exports);
7
+ tslib_1.__exportStar(require("./reactive-web-session-storage"), exports);
package/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './reactive-storage';
2
+ export * from './reactive-web-storage';
3
+ export * from './reactive-web-local-storage';
4
+ export * from './reactive-web-session-storage';
package/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export * from './reactive-storage';
2
+ export * from './reactive-web-storage';
3
+ export * from './reactive-web-local-storage';
4
+ export * from './reactive-web-session-storage';
5
+
6
+ //# sourceMappingURL=index.js.map
package/index.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../libs/reactive-storage/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC;AACvC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,gCAAgC,CAAC","file":"index.js","sourcesContent":["export * from './reactive-storage';\nexport * from './reactive-web-storage';\nexport * from './reactive-web-local-storage';\nexport * from './reactive-web-session-storage';\n"]}
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@telperion/reactive-storage",
3
+ "version": "1.1.0",
4
+ "dependencies": {
5
+ "tslib": "^2.3.0",
6
+ "rxjs": "^7.8.1",
7
+ "@thalesrc/js-utils": "^3.3.1",
8
+ "@thalesrc/rx-utils": "^1.0.0",
9
+ "@thalesrc/ts-utils": "^1.0.1"
10
+ },
11
+ "type": "module",
12
+ "module": "./index.js",
13
+ "main": "./index.cjs",
14
+ "typings": "./index.d.ts",
15
+ "bugs": "https://github.com/telperiontech/telperion/issues",
16
+ "license": "MIT",
17
+ "homepage": "https://open-source.telperion.tr",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/telperiontech/telperion.git"
21
+ },
22
+ "author": {
23
+ "name": "Ali Şahin Özçelik",
24
+ "email": "alisahinozcelik@gmail.com",
25
+ "url": "https://alisah.in"
26
+ },
27
+ "funding": {
28
+ "type": "patreon",
29
+ "url": "https://www.patreon.com/alisahin"
30
+ },
31
+ "exports": {
32
+ ".": {
33
+ "default": "./index.cjs",
34
+ "import": "./index.js",
35
+ "node": "./index.cjs",
36
+ "require": "./index.cjs",
37
+ "types": "./index.d.ts"
38
+ },
39
+ "./reactive-storage": {
40
+ "default": "./reactive-storage.cjs",
41
+ "import": "./reactive-storage.js",
42
+ "node": "./reactive-storage.cjs",
43
+ "require": "./reactive-storage.cjs",
44
+ "types": "./reactive-storage.d.ts"
45
+ },
46
+ "./reactive-web-storage": {
47
+ "default": "./reactive-web-storage.cjs",
48
+ "import": "./reactive-web-storage.js",
49
+ "node": "./reactive-web-storage.cjs",
50
+ "require": "./reactive-web-storage.cjs",
51
+ "types": "./reactive-web-storage.d.ts"
52
+ },
53
+ "./reactive-web-local-storage": {
54
+ "default": "./reactive-web-local-storage.cjs",
55
+ "import": "./reactive-web-local-storage.js",
56
+ "node": "./reactive-web-local-storage.cjs",
57
+ "require": "./reactive-web-local-storage.cjs",
58
+ "types": "./reactive-web-local-storage.d.ts"
59
+ },
60
+ "./reactive-web-session-storage": {
61
+ "default": "./reactive-web-session-storage.cjs",
62
+ "import": "./reactive-web-session-storage.js",
63
+ "node": "./reactive-web-session-storage.cjs",
64
+ "require": "./reactive-web-session-storage.cjs",
65
+ "types": "./reactive-web-session-storage.d.ts"
66
+ }
67
+ }
68
+ }