vue-context-storage 0.1.42 → 0.1.44

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/index.d.ts CHANGED
@@ -1,12 +1,13 @@
1
- import * as vue9 from "vue";
1
+ import * as vue0 from "vue";
2
2
  import { ComputedRef, InjectionKey, MaybeRefOrGetter, Plugin, PropType, UnwrapNestedRefs } from "vue";
3
3
  import { LocationQuery, LocationQueryValue } from "vue-router";
4
+ import { ZodObject, ZodRawShape, z } from "zod";
4
5
 
5
6
  //#region src/components/ContextStorageActivator.vue.d.ts
6
7
  declare const _default$1: typeof __VLS_export$4;
7
- declare const __VLS_export$4: vue9.DefineComponent<{}, () => vue9.VNode<vue9.RendererNode, vue9.RendererElement, {
8
+ declare const __VLS_export$4: vue0.DefineComponent<{}, () => vue0.VNode<vue0.RendererNode, vue0.RendererElement, {
8
9
  [key: string]: any;
9
- }>, {}, {}, {}, vue9.ComponentOptionsMixin, vue9.ComponentOptionsMixin, {}, string, vue9.PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, vue9.ComponentProvideOptions, true, {}, any>;
10
+ }>, {}, {}, {}, vue0.ComponentOptionsMixin, vue0.ComponentOptionsMixin, {}, string, vue0.PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, vue0.ComponentProvideOptions, true, {}, any>;
10
11
  //#endregion
11
12
  //#region src/handlers/types.d.ts
12
13
  interface HandlerSchema<T> {
@@ -39,7 +40,7 @@ interface RegisterBaseOptions<T> {
39
40
  *
40
41
  * const FiltersSchema = z.object({
41
42
  * search: z.string().default(''),
42
- * page: z.coerce.number().int().positive().default(1),
43
+ * page: z.number().int().positive().default(1),
43
44
  * status: z.enum(['active', 'inactive']).default('active'),
44
45
  * })
45
46
  *
@@ -65,7 +66,7 @@ interface ContextStorageHandler<T, O> {
65
66
  //#endregion
66
67
  //#region src/components/ContextStorageCollection.vue.d.ts
67
68
  declare const _default$2: typeof __VLS_export$3;
68
- declare const __VLS_export$3: vue9.DefineComponent<vue9.ExtractPropTypes<{
69
+ declare const __VLS_export$3: vue0.DefineComponent<vue0.ExtractPropTypes<{
69
70
  handlers: {
70
71
  type: PropType<ContextStorageHandlerFactory[]>;
71
72
  default: () => ContextStorageHandlerFactory<{}, RegisterOptions<{}>>[];
@@ -81,9 +82,9 @@ declare const __VLS_export$3: vue9.DefineComponent<vue9.ExtractPropTypes<{
81
82
  type: PropType<ContextStorageHandlerFactory[]>;
82
83
  default: undefined;
83
84
  };
84
- }>, () => vue9.VNode<vue9.RendererNode, vue9.RendererElement, {
85
+ }>, () => vue0.VNode<vue0.RendererNode, vue0.RendererElement, {
85
86
  [key: string]: any;
86
- }>[] | undefined, {}, {}, {}, vue9.ComponentOptionsMixin, vue9.ComponentOptionsMixin, {}, string, vue9.PublicProps, Readonly<vue9.ExtractPropTypes<{
87
+ }>[] | undefined, {}, {}, {}, vue0.ComponentOptionsMixin, vue0.ComponentOptionsMixin, {}, string, vue0.PublicProps, Readonly<vue0.ExtractPropTypes<{
87
88
  handlers: {
88
89
  type: PropType<ContextStorageHandlerFactory[]>;
89
90
  default: () => ContextStorageHandlerFactory<{}, RegisterOptions<{}>>[];
@@ -102,27 +103,27 @@ declare const __VLS_export$3: vue9.DefineComponent<vue9.ExtractPropTypes<{
102
103
  }>> & Readonly<{}>, {
103
104
  handlers: ContextStorageHandlerFactory<{}, RegisterOptions<{}>>[];
104
105
  additionalHandlers: ContextStorageHandlerFactory<{}, RegisterOptions<{}>>[];
105
- }, {}, {}, {}, string, vue9.ComponentProvideOptions, true, {}, any>;
106
+ }, {}, {}, {}, string, vue0.ComponentProvideOptions, true, {}, any>;
106
107
  //#endregion
107
108
  //#region src/components/ContextStorageProvider.vue.d.ts
108
109
  declare const _default$4: typeof __VLS_export$2;
109
- declare const __VLS_export$2: vue9.DefineComponent<vue9.ExtractPropTypes<{
110
+ declare const __VLS_export$2: vue0.DefineComponent<vue0.ExtractPropTypes<{
110
111
  itemKey: {
111
112
  type: StringConstructor;
112
113
  required: true;
113
114
  };
114
- }>, () => vue9.VNode<vue9.RendererNode, vue9.RendererElement, {
115
+ }>, () => vue0.VNode<vue0.RendererNode, vue0.RendererElement, {
115
116
  [key: string]: any;
116
- }>[] | undefined, {}, {}, {}, vue9.ComponentOptionsMixin, vue9.ComponentOptionsMixin, {}, string, vue9.PublicProps, Readonly<vue9.ExtractPropTypes<{
117
+ }>[] | undefined, {}, {}, {}, vue0.ComponentOptionsMixin, vue0.ComponentOptionsMixin, {}, string, vue0.PublicProps, Readonly<vue0.ExtractPropTypes<{
117
118
  itemKey: {
118
119
  type: StringConstructor;
119
120
  required: true;
120
121
  };
121
- }>> & Readonly<{}>, {}, {}, {}, {}, string, vue9.ComponentProvideOptions, true, {}, any>;
122
+ }>> & Readonly<{}>, {}, {}, {}, {}, string, vue0.ComponentProvideOptions, true, {}, any>;
122
123
  //#endregion
123
124
  //#region src/components/ContextStorage.vue.d.ts
124
125
  declare const _default: typeof __VLS_export$1;
125
- declare const __VLS_export$1: vue9.DefineComponent<vue9.ExtractPropTypes<{
126
+ declare const __VLS_export$1: vue0.DefineComponent<vue0.ExtractPropTypes<{
126
127
  handlers: {
127
128
  type: PropType<ContextStorageHandlerFactory[]>;
128
129
  default: () => ContextStorageHandlerFactory<{}, RegisterOptions<{}>>[];
@@ -138,9 +139,9 @@ declare const __VLS_export$1: vue9.DefineComponent<vue9.ExtractPropTypes<{
138
139
  type: PropType<ContextStorageHandlerFactory[]>;
139
140
  default: undefined;
140
141
  };
141
- }>, () => vue9.VNode<vue9.RendererNode, vue9.RendererElement, {
142
+ }>, () => vue0.VNode<vue0.RendererNode, vue0.RendererElement, {
142
143
  [key: string]: any;
143
- }>[] | undefined, {}, {}, {}, vue9.ComponentOptionsMixin, vue9.ComponentOptionsMixin, {}, string, vue9.PublicProps, Readonly<vue9.ExtractPropTypes<{
144
+ }>[] | undefined, {}, {}, {}, vue0.ComponentOptionsMixin, vue0.ComponentOptionsMixin, {}, string, vue0.PublicProps, Readonly<vue0.ExtractPropTypes<{
144
145
  handlers: {
145
146
  type: PropType<ContextStorageHandlerFactory[]>;
146
147
  default: () => ContextStorageHandlerFactory<{}, RegisterOptions<{}>>[];
@@ -159,7 +160,7 @@ declare const __VLS_export$1: vue9.DefineComponent<vue9.ExtractPropTypes<{
159
160
  }>> & Readonly<{}>, {
160
161
  handlers: ContextStorageHandlerFactory<{}, RegisterOptions<{}>>[];
161
162
  additionalHandlers: ContextStorageHandlerFactory<{}, RegisterOptions<{}>>[];
162
- }, {}, {}, {}, string, vue9.ComponentProvideOptions, true, {}, any>;
163
+ }, {}, {}, {}, string, vue0.ComponentProvideOptions, true, {}, any>;
163
164
  //#endregion
164
165
  //#region src/prefix.d.ts
165
166
  /**
@@ -181,19 +182,19 @@ declare const contextStoragePrefixSegmentsInjectKey: InjectionKey<MaybeRefOrGett
181
182
  //#endregion
182
183
  //#region src/components/ContextStoragePrefix.vue.d.ts
183
184
  declare const _default$3: typeof __VLS_export;
184
- declare const __VLS_export: vue9.DefineComponent<vue9.ExtractPropTypes<{
185
+ declare const __VLS_export: vue0.DefineComponent<vue0.ExtractPropTypes<{
185
186
  name: {
186
187
  type: PropType<ContextStoragePrefixSegment>;
187
188
  required: true;
188
189
  };
189
- }>, () => vue9.VNode<vue9.RendererNode, vue9.RendererElement, {
190
+ }>, () => vue0.VNode<vue0.RendererNode, vue0.RendererElement, {
190
191
  [key: string]: any;
191
- }>, {}, {}, {}, vue9.ComponentOptionsMixin, vue9.ComponentOptionsMixin, {}, string, vue9.PublicProps, Readonly<vue9.ExtractPropTypes<{
192
+ }>, {}, {}, {}, vue0.ComponentOptionsMixin, vue0.ComponentOptionsMixin, {}, string, vue0.PublicProps, Readonly<vue0.ExtractPropTypes<{
192
193
  name: {
193
194
  type: PropType<ContextStoragePrefixSegment>;
194
195
  required: true;
195
196
  };
196
- }>> & Readonly<{}>, {}, {}, {}, {}, string, vue9.ComponentProvideOptions, true, {}, any>;
197
+ }>> & Readonly<{}>, {}, {}, {}, {}, string, vue0.ComponentProvideOptions, true, {}, any>;
197
198
  //#endregion
198
199
  //#region src/plugin.d.ts
199
200
  declare const VueContextStoragePlugin: Plugin;
@@ -365,10 +366,12 @@ interface RegisterQueryHandlerOptions<T> extends RegisterBaseOptions<T>, Registe
365
366
  //#region src/handlers/web-storage-base/types.d.ts
366
367
  interface WebStorageHandlerBaseOptions {
367
368
  /**
368
- * Default: true for localStorage, false for sessionStorage
369
+ * Default: true
369
370
  *
370
- * If enabled - storage events will be listened to for cross-tab synchronization.
371
- * Only works with localStorage (sessionStorage is per-tab).
371
+ * If enabled - storage events will be listened to so external changes are synced
372
+ * back into the reactive data. For localStorage this covers other tabs; for
373
+ * sessionStorage it covers other contexts of the same session (iframes,
374
+ * `window.open` windows) and manual edits via DevTools.
372
375
  */
373
376
  listenToStorageEvents?: boolean;
374
377
  }
@@ -456,6 +459,34 @@ declare function createLocalStorageHandler(customOptions?: WebStorageHandlerBase
456
459
  //#region src/handlers/session-storage/index.d.ts
457
460
  declare function createSessionStorageHandler(customOptions?: WebStorageHandlerBaseOptions): ContextStorageHandlerFactory;
458
461
  //#endregion
462
+ //#region src/zod.d.ts
463
+ declare const SCHEMA_SYMBOL: unique symbol;
464
+ /**
465
+ * This type is "Maybe", but always has a schema attached to it.
466
+ * It's used to avoid any type errors with transformations.
467
+ */
468
+ type MaybeWithSchema<T extends ZodRawShape> = z.infer<ZodObject<T>> & {
469
+ [SCHEMA_SYMBOL]?: ZodObject<T>;
470
+ };
471
+ /**
472
+ * Creates an object with empty values based on a Zod schema.
473
+ * Useful for initializing forms.
474
+ *
475
+ * When `withSchema` is `true`, attaches the schema to the result via `SCHEMA_SYMBOL`
476
+ * (wrapped with `markRaw` to prevent Vue reactivity on the schema instance).
477
+ *
478
+ * Implemented via duck-typed introspection (`_zod.def.type`) — `zod` is only
479
+ * imported as types, so the main bundle never depends on it at runtime.
480
+ */
481
+ declare function createEmptyZodObject<T extends ZodRawShape>(schema: ZodObject<T>, options: {
482
+ useDefaults?: boolean;
483
+ withSchema: true;
484
+ }): MaybeWithSchema<T>;
485
+ declare function createEmptyZodObject<T extends ZodRawShape>(schema: ZodObject<T>, options?: {
486
+ useDefaults?: boolean;
487
+ withSchema?: false;
488
+ }): z.infer<ZodObject<T>>;
489
+ //#endregion
459
490
  //#region src/handlers/query/transform-helpers.d.ts
460
491
  declare function asNumber(value: QueryValue | number | undefined): number;
461
492
  declare function asNumber(value: QueryValue | number | undefined, options: {
@@ -660,4 +691,4 @@ declare const contextStorageSessionStorageHandlerInjectKey: InjectionKey<Context
660
691
  //#region src/constants.d.ts
661
692
  declare const defaultHandlers: ContextStorageHandlerFactory[];
662
693
  //#endregion
663
- export { type CollectionManager, type CollectionManagerItem, _default as ContextStorage, _default$1 as ContextStorageActivator, _default$2 as ContextStorageCollection, type ContextStorageHandler, type ContextStorageHandlerFactory, type ContextStorageHandlerMap, _default$3 as ContextStoragePrefix, type ContextStoragePrefixSegment, _default$4 as ContextStorageProvider, type WebStorageHandlerBaseOptions as LocalStorageHandlerBaseOptions, type QueryValue, type RegisterBaseOptions, type RegisterWebStorageHandlerBaseOptions as RegisterLocalStorageHandlerBaseOptions, VueContextStoragePlugin, asArray, asBoolean, asNumber, asNumberArray, asObjectArray, asString, contextStorageCollectionInjectKey, contextStorageCollectionItemInjectKey, contextStorageHandlersInjectKey, contextStorageLocalStorageHandlerInjectKey, contextStoragePrefixSegmentsInjectKey, contextStorageQueryHandlerInjectKey, contextStorageSessionStorageHandlerInjectKey, createCollectionManager, createLocalStorageHandler, createQueryHandler, createSessionStorageHandler, defaultHandlers, defineContextStorageHandler, deserializeParams as deserializeQueryParams, resolveHandlerInjectionKey, serializeParams as serializeQueryParams, transform, useContextStorage, useContextStorageActivator, useContextStorageCollection, useContextStorageProvider };
694
+ export { type CollectionManager, type CollectionManagerItem, _default as ContextStorage, _default$1 as ContextStorageActivator, _default$2 as ContextStorageCollection, type ContextStorageHandler, type ContextStorageHandlerFactory, type ContextStorageHandlerMap, _default$3 as ContextStoragePrefix, type ContextStoragePrefixSegment, _default$4 as ContextStorageProvider, type WebStorageHandlerBaseOptions as LocalStorageHandlerBaseOptions, type MaybeWithSchema, type QueryValue, type RegisterBaseOptions, type RegisterWebStorageHandlerBaseOptions as RegisterLocalStorageHandlerBaseOptions, SCHEMA_SYMBOL, VueContextStoragePlugin, asArray, asBoolean, asNumber, asNumberArray, asObjectArray, asString, contextStorageCollectionInjectKey, contextStorageCollectionItemInjectKey, contextStorageHandlersInjectKey, contextStorageLocalStorageHandlerInjectKey, contextStoragePrefixSegmentsInjectKey, contextStorageQueryHandlerInjectKey, contextStorageSessionStorageHandlerInjectKey, createCollectionManager, createEmptyZodObject, createLocalStorageHandler, createQueryHandler, createSessionStorageHandler, defaultHandlers, defineContextStorageHandler, deserializeParams as deserializeQueryParams, resolveHandlerInjectionKey, serializeParams as serializeQueryParams, transform, useContextStorage, useContextStorageActivator, useContextStorageCollection, useContextStorageProvider };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { computed, defineComponent, getCurrentInstance, h, inject, onBeforeUnmount, onUnmounted, provide, toValue, watch } from "vue";
1
+ import { computed, defineComponent, getCurrentInstance, h, inject, markRaw, onBeforeUnmount, onUnmounted, provide, toValue, watch } from "vue";
2
2
  import { useRoute, useRouter } from "vue-router";
3
3
  import { cloneDeep, isEqual, merge, omit, pick } from "lodash";
4
4
 
@@ -202,6 +202,21 @@ function extractDefaultsFromSchema(schema) {
202
202
  }
203
203
  return result;
204
204
  }
205
+ function coerceScalar(value, baseType) {
206
+ if (baseType === "boolean") {
207
+ if (value === "1") return true;
208
+ if (value === "0") return false;
209
+ return value;
210
+ }
211
+ if (baseType === "number") {
212
+ if (typeof value === "string" && value.trim() !== "") {
213
+ const n = Number(value);
214
+ if (!Number.isNaN(n)) return n;
215
+ }
216
+ return value;
217
+ }
218
+ return value;
219
+ }
205
220
  function coerceDataForSchema(data, schema) {
206
221
  const shape = schema?.shape;
207
222
  if (!shape || typeof shape !== "object") return data;
@@ -210,13 +225,20 @@ function coerceDataForSchema(data, schema) {
210
225
  if (!(key in result)) continue;
211
226
  const base = unwrapZodField(shape[key]);
212
227
  const baseType = zodDefType(base);
213
- if (baseType === "boolean") {
214
- const value = result[key];
215
- if (value === "1") result[key] = true;
216
- else if (value === "0") result[key] = false;
217
- } else if (baseType === "array") {
228
+ if (baseType === "boolean" || baseType === "number") result[key] = coerceScalar(result[key], baseType);
229
+ else if (baseType === "array") {
218
230
  const value = result[key];
219
- if (value !== void 0 && value !== null && !Array.isArray(value)) result[key] = [value];
231
+ if (value !== void 0 && value !== null) {
232
+ let arr;
233
+ if (Array.isArray(value)) arr = value;
234
+ else if (typeof value === "object") arr = Object.entries(value).sort(([a], [b]) => Number(a) - Number(b)).map(([, v]) => v);
235
+ else arr = [value];
236
+ const elementBase = unwrapZodField(base.element ?? base._zod?.def?.element);
237
+ const elementType = zodDefType(elementBase);
238
+ if (elementType === "object" && elementBase.shape) arr = arr.map((v) => v && typeof v === "object" && !Array.isArray(v) ? coerceDataForSchema(v, elementBase) : v);
239
+ else if (elementType === "number" || elementType === "boolean") arr = arr.map((v) => coerceScalar(v, elementType));
240
+ result[key] = arr;
241
+ }
220
242
  } else if (baseType === "object" && base.shape) {
221
243
  const nested = result[key];
222
244
  if (nested && typeof nested === "object" && !Array.isArray(nested)) result[key] = coerceDataForSchema(nested, base);
@@ -627,6 +649,7 @@ function createWebStorageHandlerInstance(config) {
627
649
  }
628
650
  function handleStorageEvent(event) {
629
651
  if (!enabled) return;
652
+ if (event.storageArea && event.storageArea !== config.storage) return;
630
653
  registered.forEach((item) => {
631
654
  if (event.key === item.options.key) syncStorageToRegisteredItem(item);
632
655
  });
@@ -749,7 +772,7 @@ function createSessionStorageHandler(customOptions) {
749
772
  injectionKey: contextStorageSessionStorageHandler,
750
773
  handlerName: "sessionStorage",
751
774
  options: {
752
- listenToStorageEvents: false,
775
+ listenToStorageEvents: true,
753
776
  ...customOptions
754
777
  }
755
778
  });
@@ -1010,6 +1033,59 @@ function useContextStorage(type, data, options) {
1010
1033
  return buildContextStorageHandler(handler, data, options);
1011
1034
  }
1012
1035
 
1036
+ //#endregion
1037
+ //#region src/zod.ts
1038
+ const SCHEMA_SYMBOL = /* @__PURE__ */ Symbol("schema");
1039
+ function createEmptyZodObject(schema, options) {
1040
+ const shape = schema.shape;
1041
+ const result = {};
1042
+ const { useDefaults = true } = options || {};
1043
+ for (const key in shape) {
1044
+ const field = shape[key];
1045
+ let hasDefault = false;
1046
+ let isNullable = false;
1047
+ let isOptional = false;
1048
+ let base = field;
1049
+ while (true) {
1050
+ const type = zodDefType(base);
1051
+ if (type === "default") hasDefault = true;
1052
+ else if (type === "nullable") isNullable = true;
1053
+ else if (type === "optional") isOptional = true;
1054
+ else break;
1055
+ base = base.unwrap();
1056
+ }
1057
+ if (useDefaults && hasDefault) {
1058
+ result[key] = field.parse(void 0);
1059
+ continue;
1060
+ }
1061
+ if (isNullable) {
1062
+ result[key] = null;
1063
+ continue;
1064
+ }
1065
+ if (isOptional) {
1066
+ result[key] = void 0;
1067
+ continue;
1068
+ }
1069
+ const baseType = zodDefType(base);
1070
+ if (baseType === "string") result[key] = "";
1071
+ else if (baseType === "number") {
1072
+ const minValue = base.minValue;
1073
+ let defaultValue = minValue !== null && minValue !== void 0 && isFinite(minValue) ? minValue : 0;
1074
+ if (!base.safeParse(defaultValue).success) defaultValue += 1;
1075
+ result[key] = defaultValue;
1076
+ } else if (baseType === "boolean") result[key] = false;
1077
+ else if (baseType === "array") result[key] = [];
1078
+ else if (baseType === "object" && base.shape) result[key] = createEmptyZodObject(base, {
1079
+ useDefaults: options?.useDefaults,
1080
+ withSchema: options?.withSchema
1081
+ });
1082
+ else if (baseType === "date") result[key] = null;
1083
+ else result[key] = void 0;
1084
+ }
1085
+ if (options?.withSchema) result[SCHEMA_SYMBOL] = markRaw(schema);
1086
+ return result;
1087
+ }
1088
+
1013
1089
  //#endregion
1014
1090
  //#region src/handlers/query/transform-helpers.ts
1015
1091
  function asNumber(value, options) {
@@ -1087,4 +1163,4 @@ const transform = {
1087
1163
  };
1088
1164
 
1089
1165
  //#endregion
1090
- export { ContextStorage_default as ContextStorage, ContextStorageActivator_default as ContextStorageActivator, ContextStorageCollection_default as ContextStorageCollection, ContextStoragePrefix_default as ContextStoragePrefix, ContextStorageProvider_default as ContextStorageProvider, VueContextStoragePlugin, asArray, asBoolean, asNumber, asNumberArray, asObjectArray, asString, contextStorageCollectionInjectKey, contextStorageCollectionItemInjectKey, contextStorageHandlersInjectKey, contextStorageLocalStorageHandlerInjectKey, contextStoragePrefixSegmentsInjectKey, contextStorageQueryHandlerInjectKey, contextStorageSessionStorageHandlerInjectKey, createCollectionManager, createLocalStorageHandler, createQueryHandler, createSessionStorageHandler, defaultHandlers, defineContextStorageHandler, deserializeParams as deserializeQueryParams, resolveHandlerInjectionKey, serializeParams as serializeQueryParams, transform, useContextStorage, useContextStorageActivator, useContextStorageCollection, useContextStorageProvider };
1166
+ export { ContextStorage_default as ContextStorage, ContextStorageActivator_default as ContextStorageActivator, ContextStorageCollection_default as ContextStorageCollection, ContextStoragePrefix_default as ContextStoragePrefix, ContextStorageProvider_default as ContextStorageProvider, SCHEMA_SYMBOL, VueContextStoragePlugin, asArray, asBoolean, asNumber, asNumberArray, asObjectArray, asString, contextStorageCollectionInjectKey, contextStorageCollectionItemInjectKey, contextStorageHandlersInjectKey, contextStorageLocalStorageHandlerInjectKey, contextStoragePrefixSegmentsInjectKey, contextStorageQueryHandlerInjectKey, contextStorageSessionStorageHandlerInjectKey, createCollectionManager, createEmptyZodObject, createLocalStorageHandler, createQueryHandler, createSessionStorageHandler, defaultHandlers, defineContextStorageHandler, deserializeParams as deserializeQueryParams, resolveHandlerInjectionKey, serializeParams as serializeQueryParams, transform, useContextStorage, useContextStorageActivator, useContextStorageCollection, useContextStorageProvider };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vue-context-storage",
3
3
  "type": "module",
4
- "version": "0.1.42",
4
+ "version": "0.1.44",
5
5
  "description": "Vue 3 reactive state management — sync state with the URL query, localStorage, and sessionStorage",
6
6
  "author": "",
7
7
  "license": "MIT",
@@ -15,7 +15,6 @@
15
15
  },
16
16
  "exports": {
17
17
  ".": "./dist/index.js",
18
- "./zod": "./dist/zod.js",
19
18
  "./package.json": "./package.json"
20
19
  },
21
20
  "main": "./dist/index.js",
@@ -37,8 +36,8 @@
37
36
  "lint": "eslint . --fix",
38
37
  "lint:check": "eslint .",
39
38
  "dependency-cruiser:check": "depcruise --config .dependency-cruiser.cjs src playground/src",
40
- "play": "vite",
41
- "play:e2e": "vite --config vite.e2e.config.ts",
39
+ "play": "vite --port 5173",
40
+ "play:e2e": "vite --port 5174 --config vite.e2e.config.ts",
42
41
  "build:playground": "vite build",
43
42
  "preview:playground": "vite preview --outDir playground/dist",
44
43
  "test": "npm run test:unit && npm run test:e2e",
package/dist/zod.d.ts DELETED
@@ -1,59 +0,0 @@
1
- import { ZodObject, ZodRawShape, z } from "zod";
2
-
3
- //#region src/zod.d.ts
4
- declare const SCHEMA_SYMBOL: unique symbol;
5
- /**
6
- * This type is "Maybe", but always has a schema attached to it.
7
- * It's used to avoid any type errors with transformations.
8
- */
9
- type MaybeWithSchema<T extends ZodRawShape> = z.infer<ZodObject<T>> & {
10
- [SCHEMA_SYMBOL]?: ZodObject<T>;
11
- };
12
- /**
13
- * Creates a Zod schema for arrays of objects serialized as indexed query parameters.
14
- *
15
- * URL query parameters serialize arrays of objects as indexed keys:
16
- * `items[0][product]=Apple&items[0][quantity]=5&items[1][product]=Banana&items[1][quantity]=10`
17
- *
18
- * After deserialization, these become indexed objects:
19
- * `{ '0': { product: 'Apple', quantity: '5' }, '1': { product: 'Banana', quantity: '10' } }`
20
- *
21
- * This helper wraps `z.record()` + `.transform()` to convert them back to a typed array,
22
- * so you don't have to write the boilerplate yourself.
23
- *
24
- * @example
25
- * ```ts
26
- * import { z } from 'zod'
27
- * import { zObjectArray } from 'vue-context-storage/zod'
28
- *
29
- * const ItemSchema = z.object({
30
- * product: z.string().default(''),
31
- * quantity: z.coerce.number().default(0),
32
- * })
33
- *
34
- * const DataSchema = z.object({
35
- * title: z.string().default(''),
36
- * items: zObjectArray(ItemSchema),
37
- * })
38
- *
39
- * useContextStorage('query', data, { schema: DataSchema })
40
- * ```
41
- */
42
- declare function zObjectArray<T extends z.ZodTypeAny>(itemSchema: T): z.ZodPipe<z.ZodDefault<z.ZodRecord<z.ZodString, T>>, z.ZodTransform<z.core.output<T>[], Record<string, z.core.output<T>>>>;
43
- /**
44
- * Creates an object with empty values based on a Zod schema.
45
- * Useful for initializing forms.
46
- *
47
- * When `withSchema` is `true`, attaches the schema to the result via `SCHEMA_SYMBOL`
48
- * (wrapped with `markRaw` to prevent Vue reactivity on the schema instance).
49
- */
50
- declare function createEmptyObject<T extends ZodRawShape>(schema: ZodObject<T>, options: {
51
- useDefaults?: boolean;
52
- withSchema: true;
53
- }): MaybeWithSchema<T>;
54
- declare function createEmptyObject<T extends ZodRawShape>(schema: ZodObject<T>, options?: {
55
- useDefaults?: boolean;
56
- withSchema?: false;
57
- }): z.infer<ZodObject<T>>;
58
- //#endregion
59
- export { MaybeWithSchema, SCHEMA_SYMBOL, createEmptyObject, zObjectArray };
package/dist/zod.js DELETED
@@ -1,68 +0,0 @@
1
- import { markRaw } from "vue";
2
- import { z } from "zod";
3
-
4
- //#region src/zod.ts
5
- const SCHEMA_SYMBOL = /* @__PURE__ */ Symbol("schema");
6
- function zObjectArray(itemSchema) {
7
- return z.record(z.string(), itemSchema).default({}).transform((record) => Object.entries(record).sort(([a], [b]) => Number(a) - Number(b)).map(([, v]) => v));
8
- }
9
- function createEmptyObject(schema, options) {
10
- const shape = schema.shape;
11
- const result = {};
12
- const { useDefaults = true } = options || {};
13
- for (const key in shape) {
14
- const field = shape[key];
15
- if (useDefaults) {
16
- let hasDefault = false;
17
- let currentType = field;
18
- while (currentType instanceof z.ZodOptional || currentType instanceof z.ZodNullable || currentType instanceof z.ZodDefault) {
19
- if (currentType instanceof z.ZodDefault) {
20
- hasDefault = true;
21
- break;
22
- }
23
- currentType = currentType.unwrap();
24
- }
25
- if (hasDefault) {
26
- result[key] = field.parse(void 0);
27
- continue;
28
- }
29
- }
30
- let isNullable = false;
31
- let isOptional = false;
32
- let checkType = field;
33
- while (checkType instanceof z.ZodOptional || checkType instanceof z.ZodNullable || checkType instanceof z.ZodDefault) {
34
- if (checkType instanceof z.ZodNullable) isNullable = true;
35
- if (checkType instanceof z.ZodOptional) isOptional = true;
36
- checkType = checkType.unwrap();
37
- }
38
- if (isNullable) {
39
- result[key] = null;
40
- continue;
41
- }
42
- if (isOptional) {
43
- result[key] = void 0;
44
- continue;
45
- }
46
- let baseType = field;
47
- while (baseType instanceof z.ZodOptional || baseType instanceof z.ZodNullable || baseType instanceof z.ZodDefault) baseType = baseType.unwrap();
48
- if (baseType instanceof z.ZodString) result[key] = "";
49
- else if (baseType instanceof z.ZodNumber) {
50
- let defaultValue = baseType.minValue !== null && baseType.minValue !== void 0 && isFinite(baseType.minValue) ? baseType.minValue : 0;
51
- if (!baseType.safeParse(defaultValue).success) defaultValue += 1;
52
- result[key] = defaultValue;
53
- } else if (baseType instanceof z.ZodBoolean) result[key] = false;
54
- else if (baseType instanceof z.ZodArray) result[key] = [];
55
- else if (baseType instanceof z.ZodObject) result[key] = createEmptyObject(baseType, {
56
- useDefaults: options?.useDefaults,
57
- withSchema: options?.withSchema
58
- });
59
- else if (baseType instanceof z.ZodDate) result[key] = null;
60
- else if (field instanceof z.ZodOptional || field instanceof z.ZodNullable) result[key] = void 0;
61
- else result[key] = void 0;
62
- }
63
- if (options?.withSchema) result[SCHEMA_SYMBOL] = markRaw(schema);
64
- return result;
65
- }
66
-
67
- //#endregion
68
- export { SCHEMA_SYMBOL, createEmptyObject, zObjectArray };