seitu 0.5.0 → 0.5.1

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.
@@ -6,11 +6,21 @@ export interface WebStorageValue<V> extends Subscribable<V>, Readable<V>, Writab
6
6
  export interface WebStorageValueOptionsWithStorage<Storage extends WebStorage<any>, K extends keyof Storage['~']['output']> {
7
7
  storage: Storage;
8
8
  key: K;
9
+ /**
10
+ * If true, the value will be cleared if the validation fails.
11
+ *
12
+ * @default true
13
+ */
14
+ clearOnValidationFailure?: boolean;
9
15
  }
10
16
  export interface WebStorageValueOptionsWithSchema<S extends StandardSchemaV1<unknown>> {
11
17
  schema: S;
12
18
  key: string;
13
19
  defaultValue: StandardSchemaV1.InferOutput<S>;
20
+ /**
21
+ * If true, the value will be cleared if the validation fails.
22
+ */
23
+ clearOnValidationFailure?: boolean;
14
24
  }
15
25
  export declare function createWebStorageValue<Storage extends WebStorage<any>, K extends keyof Storage['~']['output']>(options: WebStorageValueOptionsWithStorage<Storage, K>): WebStorageValue<Storage['~']['output'][K]>;
16
26
  export declare function createWebStorageValue<S extends StandardSchemaV1<unknown>>(options: WebStorageValueOptionsWithSchema<S> & {
@@ -1,6 +1,12 @@
1
1
  import type { SchemaStore, SchemaStoreOptions, SchemaStoreOutput, SchemaStoreSchema } from '../core/index';
2
2
  export interface WebStorageOptions<S extends SchemaStoreSchema> extends Omit<SchemaStoreOptions<S>, 'provider'> {
3
3
  keyTransform?: (key: keyof S) => string;
4
+ /**
5
+ * If true, the stored value will be cleared if the validation fails.
6
+ *
7
+ * @default true
8
+ */
9
+ clearOnValidationFailure?: boolean;
4
10
  }
5
11
  export interface WebStorage<O extends Record<string, unknown>> extends SchemaStore<O> {
6
12
  '~': {
package/dist/web.js CHANGED
@@ -17,7 +17,7 @@ function r() {
17
17
  //#endregion
18
18
  //#region src/web/web-storage.ts
19
19
  function i(n) {
20
- let { kind: r, keyTransform: i, defaultValues: a, schemas: o } = n, s = !1, c = t({
20
+ let { kind: r, keyTransform: i, defaultValues: a, schemas: o, clearOnValidationFailure: s } = n, c = !1, l = t({
21
21
  defaultValues: a,
22
22
  schemas: o,
23
23
  provider: {
@@ -25,37 +25,37 @@ function i(n) {
25
25
  if (typeof window > "u") return a;
26
26
  let t = window[r], n = { ...a };
27
27
  for (let r in n) {
28
- let s = t.getItem(i ? i(r) : r);
29
- if (s === null) {
28
+ let c = t.getItem(i ? i(r) : r);
29
+ if (c === null) {
30
30
  n[r] = a[r];
31
31
  continue;
32
32
  }
33
- let c = e(s), l = o[r]["~standard"].validate(c);
34
- if (l instanceof Promise) throw TypeError("[createWebSchemaStore] Validation schema should not return a Promise.");
35
- l.issues && console.warn(JSON.stringify(l.issues, null, 2), { cause: l.issues }), n[r] = l.issues ? a[r] : l.value;
33
+ let l = e(c), u = o[r]["~standard"].validate(l);
34
+ if (u instanceof Promise) throw TypeError("[createWebSchemaStore] Validation schema should not return a Promise.");
35
+ u.issues && (s ? t.removeItem(i ? i(r) : r) : console.warn(JSON.stringify(u.issues, null, 2), { cause: u.issues })), n[r] = u.issues ? a[r] : u.value;
36
36
  }
37
37
  return n;
38
38
  },
39
39
  set: (e) => {
40
40
  if (typeof window > "u") return;
41
41
  let t = window[r];
42
- s = !0, Object.entries(e).forEach(([e, n]) => {
42
+ c = !0, Object.entries(e).forEach(([e, n]) => {
43
43
  let r = typeof n == "string" ? n : JSON.stringify(n);
44
44
  t.setItem(i ? i(e) : e, r);
45
- }), window.dispatchEvent(new Event("storage")), s = !1;
45
+ }), window.dispatchEvent(new Event("storage")), c = !1;
46
46
  }
47
47
  }
48
- }), l = () => {
49
- s || c["~"].notify();
48
+ }), u = () => {
49
+ c || l["~"].notify();
50
50
  };
51
- return typeof window < "u" && window.addEventListener("storage", l), {
52
- ...c,
51
+ return typeof window < "u" && window.addEventListener("storage", u), {
52
+ ...l,
53
53
  destroy: () => {
54
- c.destroy?.(), typeof window < "u" && window.removeEventListener("storage", l);
54
+ l.destroy?.(), typeof window < "u" && window.removeEventListener("storage", u);
55
55
  },
56
56
  "~": {
57
57
  kind: r,
58
- ...c["~"]
58
+ ...l["~"]
59
59
  }
60
60
  };
61
61
  }
@@ -76,17 +76,17 @@ function o(t) {
76
76
  if (!("schema" in t || "storage" in t)) throw Error(`[${a}] Either schema or storage must be provided`);
77
77
  let o = ("schema" in t ? t.defaultValue : t.storage.getDefaultValue(t.key)) ?? null, { subscribe: s, notify: c } = n(), l = () => {
78
78
  if (typeof window > "u") return o;
79
- let n = window[r].getItem(t.key);
80
- if (n === null) return o;
81
- let i = e(n);
79
+ let n = window[r], i = n.getItem(t.key);
80
+ if (i === null) return o;
81
+ let a = e(i);
82
82
  try {
83
83
  if ("schema" in t) {
84
- let e = t.schema["~standard"].validate(i);
84
+ let e = t.schema["~standard"].validate(a);
85
85
  if (e instanceof Promise) throw TypeError("Validation schema should not return a Promise.");
86
- return e.issues ? (console.error(JSON.stringify(e.issues, null, 2), { cause: e.issues }), o) : e.value;
87
- } else return i;
86
+ return e.issues ? (t.clearOnValidationFailure ? n.removeItem(t.key) : console.warn(JSON.stringify(e.issues, null, 2), { cause: e.issues }), o) : e.value;
87
+ } else return a;
88
88
  } catch {
89
- return o !== void 0 && typeof o != "string" ? o : i;
89
+ return o !== void 0 && typeof o != "string" ? o : a;
90
90
  }
91
91
  }, u = (e) => {
92
92
  i || e.key === t.key && c();
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "seitu",
3
3
  "displayName": "Seitu",
4
4
  "type": "module",
5
- "version": "0.5.0",
5
+ "version": "0.5.1",
6
6
  "private": false,
7
7
  "author": "Valerii Strilets",
8
8
  "license": "MIT",