seitu 0.10.7 → 0.10.9
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/dist/utils/validation.d.ts +3 -3
- package/dist/utils.js +1 -1
- package/dist/web.js +34 -34
- package/package.json +1 -1
|
@@ -7,7 +7,7 @@ import type { ValidationSchemaErrorProps } from '../validate';
|
|
|
7
7
|
* @example
|
|
8
8
|
* ```ts
|
|
9
9
|
* import { createWebStorageValue } from 'seitu/web'
|
|
10
|
-
* import {
|
|
10
|
+
* import { repairValueObjectWithDefault } from 'seitu/utils'
|
|
11
11
|
* import * as z from 'zod'
|
|
12
12
|
*
|
|
13
13
|
* const value = createWebStorageValue({
|
|
@@ -15,13 +15,13 @@ import type { ValidationSchemaErrorProps } from '../validate';
|
|
|
15
15
|
* schema: z.object({ a: z.number(), b: z.string() }),
|
|
16
16
|
* key: 'storage-key',
|
|
17
17
|
* defaultValue: { a: 0, b: 'default' },
|
|
18
|
-
* onValidationError:
|
|
18
|
+
* onValidationError: repairValueObjectWithDefault,
|
|
19
19
|
* })
|
|
20
20
|
* value.get() // { a: 0, b: 'default' }
|
|
21
21
|
* window.localStorage.setItem('storage-key', JSON.stringify({ a: 1 }))
|
|
22
22
|
* value.get() // { a: 1, b: 'default' }
|
|
23
23
|
* ```
|
|
24
24
|
*/
|
|
25
|
-
export declare function
|
|
25
|
+
export declare function repairValueObjectWithDefault<O extends Record<string, unknown>>(props: ValidationSchemaErrorProps<O>): O & {
|
|
26
26
|
[k: string]: any;
|
|
27
27
|
};
|
package/dist/utils.js
CHANGED
package/dist/web.js
CHANGED
|
@@ -67,23 +67,23 @@ function s(e) {
|
|
|
67
67
|
//#endregion
|
|
68
68
|
//#region src/web/web-storage.ts
|
|
69
69
|
function c(n) {
|
|
70
|
-
let i =
|
|
71
|
-
let e = () => {
|
|
72
|
-
i
|
|
70
|
+
let i = { ...n.defaultValues }, { subscribe: a, notify: o } = t({ onFirstSubscribe: () => {
|
|
71
|
+
let e = (e) => {
|
|
72
|
+
Object.keys(i).some((t) => String(n.keyTransform ? n.keyTransform(t) : t) === e.key) && o();
|
|
73
73
|
};
|
|
74
74
|
return typeof window < "u" && window.addEventListener("storage", e), () => {
|
|
75
75
|
typeof window < "u" && window.removeEventListener("storage", e);
|
|
76
76
|
};
|
|
77
|
-
} }), s =
|
|
77
|
+
} }), s = Object.keys(n.defaultValues), c = /* @__PURE__ */ new Map(), l, u = () => {
|
|
78
78
|
if (typeof window > "u") return n.defaultValues;
|
|
79
|
-
let t = window[n.type], r =
|
|
80
|
-
for (let e of
|
|
79
|
+
let t = window[n.type], r = l !== void 0, i = {};
|
|
80
|
+
for (let e of s) {
|
|
81
81
|
let a = String(n.keyTransform ? n.keyTransform(e) : e), o = t.getItem(a);
|
|
82
|
-
i[e] = o, r &&
|
|
82
|
+
i[e] = o, r && c.get(e) !== o && (r = !1);
|
|
83
83
|
}
|
|
84
|
-
if (r) return
|
|
84
|
+
if (r) return l;
|
|
85
85
|
let a = { ...n.defaultValues };
|
|
86
|
-
for (let t of
|
|
86
|
+
for (let t of s) {
|
|
87
87
|
let r = i[t];
|
|
88
88
|
r === null ? a[t] = n.defaultValues[t] : a[t] = e(n.schemas[t], r, {
|
|
89
89
|
defaultValue: n.defaultValues[t],
|
|
@@ -94,27 +94,27 @@ function c(n) {
|
|
|
94
94
|
value: r,
|
|
95
95
|
defaultValue: n.defaultValues[t]
|
|
96
96
|
}) : void 0
|
|
97
|
-
}),
|
|
97
|
+
}), c.set(String(t), i[String(t)]);
|
|
98
98
|
}
|
|
99
|
-
return
|
|
100
|
-
},
|
|
99
|
+
return l = a, a;
|
|
100
|
+
}, d = r(u, a, o);
|
|
101
101
|
return {
|
|
102
|
-
...
|
|
102
|
+
...d,
|
|
103
103
|
set: (e) => {
|
|
104
|
-
let t = typeof e == "function" ? e(
|
|
104
|
+
let t = typeof e == "function" ? e(u()) : e;
|
|
105
105
|
if (typeof window > "u") return;
|
|
106
106
|
let r = window[n.type];
|
|
107
|
-
|
|
107
|
+
Object.entries(t).forEach(([e, t]) => {
|
|
108
108
|
let i = typeof t == "string" ? t : JSON.stringify(t);
|
|
109
109
|
r.setItem(n.keyTransform ? n.keyTransform(e) : e, i), window.dispatchEvent(new StorageEvent("storage", {
|
|
110
110
|
key: n.keyTransform ? n.keyTransform(e) : e,
|
|
111
111
|
newValue: i
|
|
112
112
|
}));
|
|
113
|
-
}),
|
|
113
|
+
}), l = void 0;
|
|
114
114
|
},
|
|
115
115
|
"~": {
|
|
116
|
-
...
|
|
117
|
-
getDefaultValue: (e) =>
|
|
116
|
+
...d["~"],
|
|
117
|
+
getDefaultValue: (e) => i[e],
|
|
118
118
|
type: n.type
|
|
119
119
|
}
|
|
120
120
|
};
|
|
@@ -123,45 +123,45 @@ function c(n) {
|
|
|
123
123
|
//#region src/web/web-storage-value.ts
|
|
124
124
|
var l = Symbol("no-cache");
|
|
125
125
|
function u(i) {
|
|
126
|
-
let a = "storage" in i ? i.storage["~"].type : i.type, o =
|
|
126
|
+
let a = "storage" in i ? i.storage["~"].type : i.type, o = ("schema" in i ? i.defaultValue : i.storage["~"].getDefaultValue(i.key)) ?? null, { subscribe: s, notify: c } = t({ onFirstSubscribe: () => {
|
|
127
127
|
let e = (e) => {
|
|
128
|
-
|
|
128
|
+
e.key === i.key && c();
|
|
129
129
|
};
|
|
130
130
|
return typeof window < "u" && window.addEventListener("storage", e), () => {
|
|
131
131
|
typeof window < "u" && window.removeEventListener("storage", e);
|
|
132
132
|
};
|
|
133
|
-
} }),
|
|
134
|
-
if (typeof window > "u") return
|
|
133
|
+
} }), u = l, d, f = () => {
|
|
134
|
+
if (typeof window > "u") return o;
|
|
135
135
|
let t = window[a].getItem(i.key);
|
|
136
|
-
if (
|
|
137
|
-
if (
|
|
136
|
+
if (u !== l && t === u) return d;
|
|
137
|
+
if (u = t, t === null) return d = o, d;
|
|
138
138
|
let r = n(t);
|
|
139
139
|
try {
|
|
140
|
-
return "schema" in i ? (
|
|
141
|
-
defaultValue:
|
|
140
|
+
return "schema" in i ? (d = e(i.schema, t, {
|
|
141
|
+
defaultValue: o,
|
|
142
142
|
label: `createWebStorageValue:${i.key}`,
|
|
143
143
|
onError: i.onValidationError ? (e, t) => i.onValidationError({
|
|
144
|
-
defaultValue:
|
|
144
|
+
defaultValue: o,
|
|
145
145
|
issues: [...e],
|
|
146
146
|
value: t
|
|
147
147
|
}) : void 0
|
|
148
|
-
}),
|
|
148
|
+
}), d) : (d = r, d);
|
|
149
149
|
} catch {
|
|
150
|
-
return
|
|
150
|
+
return d = o !== void 0 && typeof o != "string" ? o : r, d;
|
|
151
151
|
}
|
|
152
152
|
};
|
|
153
153
|
return {
|
|
154
|
-
...r(
|
|
154
|
+
...r(f, s, c),
|
|
155
155
|
set: (e) => {
|
|
156
156
|
if (typeof window > "u") return;
|
|
157
|
-
let t = window[a], n = typeof e == "function" ? e(
|
|
158
|
-
|
|
157
|
+
let t = window[a], n = typeof e == "function" ? e(f()) : e;
|
|
158
|
+
t.setItem(i.key, typeof n == "string" ? n : JSON.stringify(n)), window.dispatchEvent(new StorageEvent("storage", {
|
|
159
159
|
key: i.key,
|
|
160
160
|
newValue: n
|
|
161
|
-
})),
|
|
161
|
+
})), u = l;
|
|
162
162
|
},
|
|
163
163
|
remove: () => {
|
|
164
|
-
typeof window > "u" || (window[a].removeItem(i.key),
|
|
164
|
+
typeof window > "u" || (window[a].removeItem(i.key), u = l);
|
|
165
165
|
}
|
|
166
166
|
};
|
|
167
167
|
}
|