@efficimo/storage-react 0.2.0 → 0.3.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/README.md CHANGED
@@ -18,21 +18,24 @@ Built on [`@efficimo/observable`](https://www.npmjs.com/package/@efficimo/observ
18
18
 
19
19
  ## Installation
20
20
 
21
+ `@efficimo/observable` is a **peer dependency** — it must resolve to a single instance shared
22
+ with the rest of your app, so install it explicitly:
23
+
21
24
  ```bash
22
25
  # core only
23
- npm install @efficimo/storage
26
+ npm install @efficimo/storage @efficimo/observable
24
27
 
25
28
  # with React hook
26
- npm install @efficimo/storage @efficimo/storage-react
29
+ npm install @efficimo/storage @efficimo/storage-react @efficimo/observable
27
30
  ```
28
31
 
29
32
  ## Quick start
30
33
 
31
34
  ```typescript
32
- import { BaseStorage } from '@efficimo/storage';
35
+ import { BaseStorage, NativeStorageAdapter } from '@efficimo/storage';
33
36
 
34
37
  type Keys = 'auth.token' | 'user.theme';
35
- const store = new BaseStorage<Keys>(localStorage);
38
+ const store = new BaseStorage<Keys>(new NativeStorageAdapter(localStorage));
36
39
 
37
40
  // reactive subscription
38
41
  store.getObservable('auth.token').subscribe(token => {
@@ -90,10 +93,41 @@ const prefs = LocalStorage.getSerializedObservable('user.prefs', schema, { theme
90
93
 
91
94
  prefs.subscribe(v => console.log(v)); // { theme: 'light', lang: 'en' }
92
95
 
93
- await prefs.next({ theme: 'dark', lang: 'fr' });
96
+ prefs.next({ theme: 'dark', lang: 'fr' });
94
97
  // localStorage now holds '{"theme":"dark","lang":"fr"}'
95
98
  ```
96
99
 
100
+ ## Cookies
101
+
102
+ `CookieStorage` is a singleton backed by `CookieStorageAdapter`, which uses the
103
+ [Cookie Store API](https://developer.mozilla.org/en-US/docs/Web/API/CookieStore).
104
+ Keys are typed through the `DefaultCookieStorageKeys` interface, like the other singletons:
105
+
106
+ ```typescript
107
+ import { CookieStorage } from '@efficimo/storage';
108
+
109
+ declare module '@efficimo/storage' {
110
+ interface DefaultCookieStorageKeys {
111
+ 'consent.analytics': true;
112
+ }
113
+ }
114
+
115
+ CookieStorage.getObservable('consent.analytics').subscribe(v => console.log(v));
116
+ CookieStorage.set('consent.analytics', 'granted');
117
+ ```
118
+
119
+ Use `CookieStorageAdapter` directly to set cookie attributes (`path`, `domain`, `maxAge`, `sameSite`):
120
+
121
+ ```typescript
122
+ import { BaseStorage, CookieStorageAdapter } from '@efficimo/storage';
123
+
124
+ const cookies = new BaseStorage(
125
+ new CookieStorageAdapter({ path: '/', sameSite: 'lax', maxAge: 60 * 60 * 24 * 365 }),
126
+ );
127
+ ```
128
+
129
+ External cookie changes are picked up through `cookieStore.onchange` when the browser supports it.
130
+
97
131
  ## Cross-tab sync
98
132
 
99
133
  `BaseStorage` listens to the browser `storage` event automatically. Any change made in another tab propagates to all active observables in the current tab — no extra setup required.
@@ -120,7 +154,7 @@ Implements `ObservableValueInterface<string | null>` from `@efficimo/observable`
120
154
  |---|---|
121
155
  | `getValue()` | Returns the current storage value. |
122
156
  | `subscribe(fn)` | Registers subscriber and immediately calls it with the current value. |
123
- | `next(value \| setter)` | Updates the value. Supports async setter `(prev) => newValue`. No-op if unchanged. |
157
+ | `next(value \| setter)` | Updates the value. Supports a setter function `(prev) => newValue`. No-op if unchanged. |
124
158
 
125
159
  ---
126
160
 
@@ -1,3 +1,3 @@
1
- export * from "./useStorageObservable";
2
- export * from "./useStorageSerializedObservable";
1
+ export * from "./useStorageObservable.ts";
2
+ export * from "./useStorageSerializedObservable.ts";
3
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,kCAAkC,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qCAAqC,CAAC"}
package/dist/cjs/index.js CHANGED
@@ -14,6 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./useStorageObservable"), exports);
18
- __exportStar(require("./useStorageSerializedObservable"), exports);
17
+ __exportStar(require("./useStorageObservable.js"), exports);
18
+ __exportStar(require("./useStorageSerializedObservable.js"), exports);
19
19
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yDAAuC;AACvC,mEAAiD"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4DAA0C;AAC1C,sEAAoD"}
@@ -1 +1 @@
1
- {"version":3,"file":"useStorageObservable.js","sourceRoot":"","sources":["../../src/useStorageObservable.ts"],"names":[],"mappings":";;AAOA,oDAWC;AAjBD,iCAA4C;AAE5C;;;GAGG;AACH,SAAgB,oBAAoB,CAClC,GAAyB;IAEzB,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,IAAA,gBAAQ,EAAgB,GAAG,EAAE,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC;IAExE,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,MAAM,YAAY,GAAG,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;QACvD,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,WAAW,EAAE,CAAC;IAC1C,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IAEV,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;AACrC,CAAC"}
1
+ {"version":3,"file":"useStorageObservable.js","sourceRoot":"","sources":["../../src/useStorageObservable.ts"],"names":[],"mappings":";;;AACA,iCAA4C;AAE5C;;;GAGG;AACH,8BACE,GAAyB;IAEzB,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,IAAA,gBAAQ,EAAgB,GAAG,EAAE,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC;IAExE,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,MAAM,YAAY,GAAG,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;QACvD,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,WAAW,EAAE,CAAC;IAC1C,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IAEV,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;AACrC,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"useStorageSerializedObservable.js","sourceRoot":"","sources":["../../src/useStorageSerializedObservable.ts"],"names":[],"mappings":";;AAOA,wEAWC;AAjBD,iCAA4C;AAE5C;;;GAGG;AACH,SAAgB,8BAA8B,CAC5C,GAAoC;IAEpC,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,IAAA,gBAAQ,EAAW,GAAG,EAAE,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC;IAEnE,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,MAAM,YAAY,GAAG,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;QACvD,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,WAAW,EAAE,CAAC;IAC1C,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IAEV,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;AACrC,CAAC"}
1
+ {"version":3,"file":"useStorageSerializedObservable.js","sourceRoot":"","sources":["../../src/useStorageSerializedObservable.ts"],"names":[],"mappings":";;;AACA,iCAA4C;AAE5C;;;GAGG;AACH,wCACE,GAAoC;IAEpC,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,IAAA,gBAAQ,EAAW,GAAG,EAAE,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC;IAEnE,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,MAAM,YAAY,GAAG,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;QACvD,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,WAAW,EAAE,CAAC;IAC1C,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IAEV,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;AACrC,CAAC"}
@@ -1,3 +1,3 @@
1
- export * from "./useStorageObservable";
2
- export * from "./useStorageSerializedObservable";
1
+ export * from "./useStorageObservable.ts";
2
+ export * from "./useStorageSerializedObservable.ts";
3
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,kCAAkC,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qCAAqC,CAAC"}
package/dist/esm/index.js CHANGED
@@ -1,3 +1,3 @@
1
- export * from "./useStorageObservable";
2
- export * from "./useStorageSerializedObservable";
1
+ export * from "./useStorageObservable.js";
2
+ export * from "./useStorageSerializedObservable.js";
3
3
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,kCAAkC,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qCAAqC,CAAC"}
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@efficimo/storage-react",
3
- "version": "0.2.0",
4
- "description": "React bindings for @efficimo/storage: hooks for localStorage and sessionStorage observables.",
3
+ "version": "0.3.0",
4
+ "description": "React bindings for @efficimo/storage: hooks for localStorage, sessionStorage and cookie observables.",
5
5
  "author": "efficimo",
6
6
  "license": "MIT",
7
7
  "keywords": [
8
8
  "storage",
9
9
  "localstorage",
10
10
  "sessionstorage",
11
+ "cookies",
11
12
  "observable",
12
13
  "react",
13
14
  "hooks",
@@ -15,7 +16,7 @@
15
16
  ],
16
17
  "repository": {
17
18
  "type": "git",
18
- "url": "https://github.com/efficimo/storage.git"
19
+ "url": "git+https://github.com/efficimo/storage.git"
19
20
  },
20
21
  "bugs": {
21
22
  "url": "https://github.com/efficimo/storage/issues"
@@ -24,6 +25,8 @@
24
25
  "engines": {
25
26
  "node": ">=18"
26
27
  },
28
+ "type": "commonjs",
29
+ "sideEffects": false,
27
30
  "main": "./dist/cjs/index.js",
28
31
  "module": "./dist/esm/index.js",
29
32
  "types": "./dist/esm/index.d.ts",
@@ -40,7 +43,8 @@
40
43
  }
41
44
  },
42
45
  "files": [
43
- "dist"
46
+ "dist",
47
+ "src"
44
48
  ],
45
49
  "publishConfig": {
46
50
  "registry": "https://registry.npmjs.org",
@@ -52,14 +56,16 @@
52
56
  "prepublishOnly": "npm run build && npm run typecheck"
53
57
  },
54
58
  "peerDependencies": {
55
- "@efficimo/storage": ">=0.1.0",
59
+ "@efficimo/observable": ">=0.5.0",
60
+ "@efficimo/storage": ">=0.3.0",
56
61
  "react": ">=18.0.0"
57
62
  },
58
63
  "devDependencies": {
64
+ "@efficimo/observable": "^0.5.1",
59
65
  "@efficimo/storage": "file:../core",
60
- "@types/node": "^25.0.0",
61
- "@types/react": "^18.0.0",
62
- "react": "^18.0.0",
63
- "typescript": "^5.0.0"
66
+ "@types/node": "^25.9.7",
67
+ "@types/react": "^19.3.0",
68
+ "react": "^19.3.0",
69
+ "typescript": "^7.0.2"
64
70
  }
65
71
  }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./useStorageObservable.ts";
2
+ export * from "./useStorageSerializedObservable.ts";
@@ -0,0 +1,19 @@
1
+ import type { StorageObservable } from "@efficimo/storage";
2
+ import { useEffect, useState } from "react";
3
+
4
+ /**
5
+ * Subscribes to a StorageObservable and returns a [value, setter] tuple,
6
+ * re-rendering the component whenever the storage value changes.
7
+ */
8
+ export function useStorageObservable<K extends string>(
9
+ obs: StorageObservable<K>,
10
+ ): [string | null, (value: string | null) => void] {
11
+ const [value, setValue] = useState<string | null>(() => obs.getValue());
12
+
13
+ useEffect(() => {
14
+ const subscription = obs.subscribe((v) => setValue(v));
15
+ return () => subscription.unsubscribe();
16
+ }, [obs]);
17
+
18
+ return [value, (v) => obs.next(v)];
19
+ }
@@ -0,0 +1,19 @@
1
+ import type { JsonSerializeObservableValue } from "@efficimo/observable";
2
+ import { useEffect, useState } from "react";
3
+
4
+ /**
5
+ * Subscribes to a JsonSerializeObservableValue (from BaseStorage.getSerializedObservable)
6
+ * and returns a typed [value, setter] tuple.
7
+ */
8
+ export function useStorageSerializedObservable<T>(
9
+ obs: JsonSerializeObservableValue<T>,
10
+ ): [T | null, (value: T | null) => void] {
11
+ const [value, setValue] = useState<T | null>(() => obs.getValue());
12
+
13
+ useEffect(() => {
14
+ const subscription = obs.subscribe((v) => setValue(v));
15
+ return () => subscription.unsubscribe();
16
+ }, [obs]);
17
+
18
+ return [value, (v) => obs.next(v)];
19
+ }