vue-context-storage 0.1.30 → 0.1.32
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 +45 -22
- package/dist/index.js +29 -5
- package/dist/zod.d.ts +81 -0
- package/dist/zod.js +74 -0
- package/package.json +91 -90
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as vue9 from "vue";
|
|
2
2
|
import { ComputedRef, InjectionKey, MaybeRefOrGetter, Plugin, PropType, UnwrapNestedRefs } from "vue";
|
|
3
3
|
import { LocationQuery, LocationQueryValue } from "vue-router";
|
|
4
4
|
|
|
5
5
|
//#region src/components/ContextStorageActivator.vue.d.ts
|
|
6
6
|
declare const _default$1: typeof __VLS_export$4;
|
|
7
|
-
declare const __VLS_export$4:
|
|
7
|
+
declare const __VLS_export$4: vue9.DefineComponent<{}, () => vue9.VNode<vue9.RendererNode, vue9.RendererElement, {
|
|
8
8
|
[key: string]: any;
|
|
9
|
-
}>, {}, {}, {},
|
|
9
|
+
}>, {}, {}, {}, vue9.ComponentOptionsMixin, vue9.ComponentOptionsMixin, {}, string, vue9.PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, vue9.ComponentProvideOptions, true, {}, any>;
|
|
10
10
|
//#endregion
|
|
11
11
|
//#region src/handlers/types.d.ts
|
|
12
12
|
interface HandlerSchema<T> {
|
|
@@ -65,55 +65,55 @@ interface ContextStorageHandler<T, O> {
|
|
|
65
65
|
//#endregion
|
|
66
66
|
//#region src/components/ContextStorageCollection.vue.d.ts
|
|
67
67
|
declare const _default$2: typeof __VLS_export$3;
|
|
68
|
-
declare const __VLS_export$3:
|
|
68
|
+
declare const __VLS_export$3: vue9.DefineComponent<vue9.ExtractPropTypes<{
|
|
69
69
|
handlers: {
|
|
70
70
|
type: PropType<ContextStorageHandlerFactory[]>;
|
|
71
71
|
default: () => ContextStorageHandlerFactory<{}, RegisterOptions<{}>>[];
|
|
72
72
|
};
|
|
73
|
-
}>, () =>
|
|
73
|
+
}>, () => vue9.VNode<vue9.RendererNode, vue9.RendererElement, {
|
|
74
74
|
[key: string]: any;
|
|
75
|
-
}>[] | undefined, {}, {}, {},
|
|
75
|
+
}>[] | undefined, {}, {}, {}, vue9.ComponentOptionsMixin, vue9.ComponentOptionsMixin, {}, string, vue9.PublicProps, Readonly<vue9.ExtractPropTypes<{
|
|
76
76
|
handlers: {
|
|
77
77
|
type: PropType<ContextStorageHandlerFactory[]>;
|
|
78
78
|
default: () => ContextStorageHandlerFactory<{}, RegisterOptions<{}>>[];
|
|
79
79
|
};
|
|
80
80
|
}>> & Readonly<{}>, {
|
|
81
81
|
handlers: ContextStorageHandlerFactory<{}, RegisterOptions<{}>>[];
|
|
82
|
-
}, {}, {}, {}, string,
|
|
82
|
+
}, {}, {}, {}, string, vue9.ComponentProvideOptions, true, {}, any>;
|
|
83
83
|
//#endregion
|
|
84
84
|
//#region src/components/ContextStorageProvider.vue.d.ts
|
|
85
85
|
declare const _default$4: typeof __VLS_export$2;
|
|
86
|
-
declare const __VLS_export$2:
|
|
86
|
+
declare const __VLS_export$2: vue9.DefineComponent<vue9.ExtractPropTypes<{
|
|
87
87
|
itemKey: {
|
|
88
88
|
type: StringConstructor;
|
|
89
89
|
required: true;
|
|
90
90
|
};
|
|
91
|
-
}>, () =>
|
|
91
|
+
}>, () => vue9.VNode<vue9.RendererNode, vue9.RendererElement, {
|
|
92
92
|
[key: string]: any;
|
|
93
|
-
}>[] | undefined, {}, {}, {},
|
|
93
|
+
}>[] | undefined, {}, {}, {}, vue9.ComponentOptionsMixin, vue9.ComponentOptionsMixin, {}, string, vue9.PublicProps, Readonly<vue9.ExtractPropTypes<{
|
|
94
94
|
itemKey: {
|
|
95
95
|
type: StringConstructor;
|
|
96
96
|
required: true;
|
|
97
97
|
};
|
|
98
|
-
}>> & Readonly<{}>, {}, {}, {}, {}, string,
|
|
98
|
+
}>> & Readonly<{}>, {}, {}, {}, {}, string, vue9.ComponentProvideOptions, true, {}, any>;
|
|
99
99
|
//#endregion
|
|
100
100
|
//#region src/components/ContextStorage.vue.d.ts
|
|
101
101
|
declare const _default: typeof __VLS_export$1;
|
|
102
|
-
declare const __VLS_export$1:
|
|
102
|
+
declare const __VLS_export$1: vue9.DefineComponent<vue9.ExtractPropTypes<{
|
|
103
103
|
handlers: {
|
|
104
104
|
type: PropType<ContextStorageHandlerFactory[]>;
|
|
105
105
|
default: () => ContextStorageHandlerFactory<{}, RegisterOptions<{}>>[];
|
|
106
106
|
};
|
|
107
|
-
}>, () =>
|
|
107
|
+
}>, () => vue9.VNode<vue9.RendererNode, vue9.RendererElement, {
|
|
108
108
|
[key: string]: any;
|
|
109
|
-
}>[] | undefined, {}, {}, {},
|
|
109
|
+
}>[] | undefined, {}, {}, {}, vue9.ComponentOptionsMixin, vue9.ComponentOptionsMixin, {}, string, vue9.PublicProps, Readonly<vue9.ExtractPropTypes<{
|
|
110
110
|
handlers: {
|
|
111
111
|
type: PropType<ContextStorageHandlerFactory[]>;
|
|
112
112
|
default: () => ContextStorageHandlerFactory<{}, RegisterOptions<{}>>[];
|
|
113
113
|
};
|
|
114
114
|
}>> & Readonly<{}>, {
|
|
115
115
|
handlers: ContextStorageHandlerFactory<{}, RegisterOptions<{}>>[];
|
|
116
|
-
}, {}, {}, {}, string,
|
|
116
|
+
}, {}, {}, {}, string, vue9.ComponentProvideOptions, true, {}, any>;
|
|
117
117
|
//#endregion
|
|
118
118
|
//#region src/prefix.d.ts
|
|
119
119
|
/**
|
|
@@ -136,19 +136,19 @@ declare const contextStoragePrefixSegmentsInjectKey: InjectionKey<MaybeRefOrGett
|
|
|
136
136
|
//#endregion
|
|
137
137
|
//#region src/components/ContextStoragePrefix.vue.d.ts
|
|
138
138
|
declare const _default$3: typeof __VLS_export;
|
|
139
|
-
declare const __VLS_export:
|
|
139
|
+
declare const __VLS_export: vue9.DefineComponent<vue9.ExtractPropTypes<{
|
|
140
140
|
name: {
|
|
141
141
|
type: PropType<ContextStoragePrefixSegment>;
|
|
142
142
|
required: true;
|
|
143
143
|
};
|
|
144
|
-
}>, () =>
|
|
144
|
+
}>, () => vue9.VNode<vue9.RendererNode, vue9.RendererElement, {
|
|
145
145
|
[key: string]: any;
|
|
146
|
-
}>, {}, {}, {},
|
|
146
|
+
}>, {}, {}, {}, vue9.ComponentOptionsMixin, vue9.ComponentOptionsMixin, {}, string, vue9.PublicProps, Readonly<vue9.ExtractPropTypes<{
|
|
147
147
|
name: {
|
|
148
148
|
type: PropType<ContextStoragePrefixSegment>;
|
|
149
149
|
required: true;
|
|
150
150
|
};
|
|
151
|
-
}>> & Readonly<{}>, {}, {}, {}, {}, string,
|
|
151
|
+
}>> & Readonly<{}>, {}, {}, {}, {}, string, vue9.ComponentProvideOptions, true, {}, any>;
|
|
152
152
|
//#endregion
|
|
153
153
|
//#region src/plugin.d.ts
|
|
154
154
|
declare const VueContextStoragePlugin: Plugin;
|
|
@@ -406,11 +406,11 @@ declare function createQueryHandler(baseOptions?: QueryHandlerBaseOptions): Cont
|
|
|
406
406
|
//#endregion
|
|
407
407
|
//#region src/handlers/local-storage/index.d.ts
|
|
408
408
|
declare function createLocalStorageHandler(customOptions?: WebStorageHandlerBaseOptions): ContextStorageHandlerFactory;
|
|
409
|
-
declare const useContextStorageLocalStorage: (data:
|
|
409
|
+
declare const useContextStorageLocalStorage: (data: vue9.MaybeRefOrGetter<Record<string, unknown>>, options: RegisterWebStorageHandlerOptions<Record<string, unknown>>) => UseContextStorageResult<Record<string, unknown>>;
|
|
410
410
|
//#endregion
|
|
411
411
|
//#region src/handlers/session-storage/index.d.ts
|
|
412
412
|
declare function createSessionStorageHandler(customOptions?: WebStorageHandlerBaseOptions): ContextStorageHandlerFactory;
|
|
413
|
-
declare const useContextStorageSessionStorage: (data:
|
|
413
|
+
declare const useContextStorageSessionStorage: (data: vue9.MaybeRefOrGetter<Record<string, unknown>>, options: RegisterWebStorageHandlerOptions<Record<string, unknown>>) => UseContextStorageResult<Record<string, unknown>>;
|
|
414
414
|
//#endregion
|
|
415
415
|
//#region src/handlers/query/transform-helpers.d.ts
|
|
416
416
|
declare function asNumber(value: QueryValue | number | undefined): number;
|
|
@@ -528,12 +528,35 @@ declare function asBoolean(value: QueryValue | undefined, options: {
|
|
|
528
528
|
missable?: false;
|
|
529
529
|
fallbackValue?: boolean;
|
|
530
530
|
}): boolean;
|
|
531
|
+
declare function asObjectArray(value: unknown): Record<string, unknown>[];
|
|
532
|
+
declare function asObjectArray<T>(value: unknown, transform: (value: Record<string, unknown>) => T): T[];
|
|
533
|
+
declare function asObjectArray<T>(value: unknown, options: {
|
|
534
|
+
nullable: true;
|
|
535
|
+
missable: true;
|
|
536
|
+
transform?: (value: Record<string, unknown>) => T;
|
|
537
|
+
}): T[] | null | undefined;
|
|
538
|
+
declare function asObjectArray<T>(value: unknown, options: {
|
|
539
|
+
nullable: true;
|
|
540
|
+
missable?: false;
|
|
541
|
+
transform?: (value: Record<string, unknown>) => T;
|
|
542
|
+
}): T[] | null;
|
|
543
|
+
declare function asObjectArray<T>(value: unknown, options: {
|
|
544
|
+
nullable?: false;
|
|
545
|
+
missable: true;
|
|
546
|
+
transform?: (value: Record<string, unknown>) => T;
|
|
547
|
+
}): T[] | undefined;
|
|
548
|
+
declare function asObjectArray<T>(value: unknown, options: {
|
|
549
|
+
nullable?: false;
|
|
550
|
+
missable?: false;
|
|
551
|
+
transform?: (value: Record<string, unknown>) => T;
|
|
552
|
+
}): T[];
|
|
531
553
|
declare const transform: {
|
|
532
554
|
asString: typeof asString;
|
|
533
555
|
asNumber: typeof asNumber;
|
|
534
556
|
asArray: typeof asArray;
|
|
535
557
|
asNumberArray: typeof asNumberArray;
|
|
536
558
|
asBoolean: typeof asBoolean;
|
|
559
|
+
asObjectArray: typeof asObjectArray;
|
|
537
560
|
};
|
|
538
561
|
//#endregion
|
|
539
562
|
//#region src/handlers/query/helpers.d.ts
|
|
@@ -593,4 +616,4 @@ declare const contextStorageSessionStorageHandlerInjectKey: InjectionKey<Context
|
|
|
593
616
|
//#region src/constants.d.ts
|
|
594
617
|
declare const defaultHandlers: ContextStorageHandlerFactory[];
|
|
595
618
|
//#endregion
|
|
596
|
-
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, 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, useContextStorageLocalStorage, useContextStorageProvider, useContextStorageQueryHandler, useContextStorageSessionStorage };
|
|
619
|
+
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, useContextStorageLocalStorage, useContextStorageProvider, useContextStorageQueryHandler, useContextStorageSessionStorage };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { computed, defineComponent, getCurrentInstance, h, inject, onBeforeUnmount, onUnmounted, provide, toValue, watch } from "vue";
|
|
2
2
|
import { useRoute, useRouter } from "vue-router";
|
|
3
|
-
import { cloneDeep, isEqual, omit, pick } from "lodash";
|
|
3
|
+
import { cloneDeep, isEqual, merge, omit, pick } from "lodash";
|
|
4
4
|
|
|
5
5
|
//#region src/symbols.ts
|
|
6
6
|
const collection = /* @__PURE__ */ Symbol("context-storage-collection");
|
|
@@ -73,7 +73,16 @@ function serializeParams(params, options = {}) {
|
|
|
73
73
|
result[formattedKey] = value;
|
|
74
74
|
return;
|
|
75
75
|
}
|
|
76
|
-
if (typeof value === "object") if (Array.isArray(value))
|
|
76
|
+
if (typeof value === "object") if (Array.isArray(value)) if (value.some((item) => typeof item === "object" && item !== null && !Array.isArray(item))) {
|
|
77
|
+
const indexed = {};
|
|
78
|
+
value.forEach((item, i) => {
|
|
79
|
+
indexed[String(i)] = item;
|
|
80
|
+
});
|
|
81
|
+
Object.assign(result, serializeParams(indexed, {
|
|
82
|
+
...options,
|
|
83
|
+
prefix: formattedKey
|
|
84
|
+
}));
|
|
85
|
+
} else result[formattedKey] = value.map(String);
|
|
77
86
|
else Object.assign(result, serializeParams(value, {
|
|
78
87
|
...options,
|
|
79
88
|
prefix: formattedKey
|
|
@@ -114,7 +123,8 @@ function applyTransform(input) {
|
|
|
114
123
|
const warnings = [];
|
|
115
124
|
let data = input.state;
|
|
116
125
|
if (input.schema) {
|
|
117
|
-
const
|
|
126
|
+
const merged = merge({}, input.initialData, data);
|
|
127
|
+
const result = input.schema.safeParse(merged);
|
|
118
128
|
if (result.success) data = result.data;
|
|
119
129
|
else {
|
|
120
130
|
warnings.push({
|
|
@@ -912,13 +922,27 @@ function asBoolean(value, options) {
|
|
|
912
922
|
}
|
|
913
923
|
return fallbackValue;
|
|
914
924
|
}
|
|
925
|
+
function asObjectArray(value, optionsOrTransform) {
|
|
926
|
+
const { nullable = false, missable = false, transform: transform2 } = typeof optionsOrTransform === "function" ? { transform: optionsOrTransform } : optionsOrTransform || {};
|
|
927
|
+
if (value === null && nullable) return null;
|
|
928
|
+
if (value === void 0 && missable) return;
|
|
929
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return nullable ? null : [];
|
|
930
|
+
const entries = Object.entries(value).sort(([a], [b]) => Number(a) - Number(b));
|
|
931
|
+
if (transform2) return entries.map(([, val]) => {
|
|
932
|
+
return transform2(val && typeof val === "object" && !Array.isArray(val) ? val : {});
|
|
933
|
+
});
|
|
934
|
+
return entries.map(([, val]) => {
|
|
935
|
+
return val && typeof val === "object" && !Array.isArray(val) ? val : {};
|
|
936
|
+
});
|
|
937
|
+
}
|
|
915
938
|
const transform = {
|
|
916
939
|
asString,
|
|
917
940
|
asNumber,
|
|
918
941
|
asArray,
|
|
919
942
|
asNumberArray,
|
|
920
|
-
asBoolean
|
|
943
|
+
asBoolean,
|
|
944
|
+
asObjectArray
|
|
921
945
|
};
|
|
922
946
|
|
|
923
947
|
//#endregion
|
|
924
|
-
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, 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, useContextStorageLocalStorage, useContextStorageProvider, useContextStorageQueryHandler, useContextStorageSessionStorage };
|
|
948
|
+
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, useContextStorageLocalStorage, useContextStorageProvider, useContextStorageQueryHandler, useContextStorageSessionStorage };
|
package/dist/zod.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
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 a Zod schema for booleans serialized as URL query parameters.
|
|
45
|
+
*
|
|
46
|
+
* URL query parameters serialize booleans as `'1'`/`'0'` strings.
|
|
47
|
+
* `z.coerce.boolean()` cannot be used because `Boolean('0')` is `true` in JavaScript.
|
|
48
|
+
*
|
|
49
|
+
* This helper correctly handles `'1'`, `'true'`, `'0'`, `'false'`, and native booleans.
|
|
50
|
+
*
|
|
51
|
+
* @param defaultValue - The default value when the field is missing (defaults to `false`)
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* import { z } from 'zod'
|
|
56
|
+
* import { zUrlBoolean } from 'vue-context-storage/zod'
|
|
57
|
+
*
|
|
58
|
+
* const Schema = z.object({
|
|
59
|
+
* active: zUrlBoolean(), // defaults to false
|
|
60
|
+
* enabled: zUrlBoolean(true), // defaults to true
|
|
61
|
+
* })
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
declare function zUrlBoolean(defaultValue?: boolean): z.ZodDefault<z.ZodPipe<z.ZodUnion<readonly [z.ZodBoolean, z.ZodString]>, z.ZodTransform<boolean, string | boolean>>>;
|
|
65
|
+
/**
|
|
66
|
+
* Creates an object with empty values based on a Zod schema.
|
|
67
|
+
* Useful for initializing forms.
|
|
68
|
+
*
|
|
69
|
+
* When `withSchema` is `true`, attaches the schema to the result via `SCHEMA_SYMBOL`
|
|
70
|
+
* (wrapped with `markRaw` to prevent Vue reactivity on the schema instance).
|
|
71
|
+
*/
|
|
72
|
+
declare function createEmptyObject<T extends ZodRawShape>(schema: ZodObject<T>, options: {
|
|
73
|
+
useDefaults?: boolean;
|
|
74
|
+
withSchema: true;
|
|
75
|
+
}): MaybeWithSchema<T>;
|
|
76
|
+
declare function createEmptyObject<T extends ZodRawShape>(schema: ZodObject<T>, options?: {
|
|
77
|
+
useDefaults?: boolean;
|
|
78
|
+
withSchema?: false;
|
|
79
|
+
}): z.infer<ZodObject<T>>;
|
|
80
|
+
//#endregion
|
|
81
|
+
export { MaybeWithSchema, SCHEMA_SYMBOL, createEmptyObject, zObjectArray, zUrlBoolean };
|
package/dist/zod.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
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 zUrlBoolean(defaultValue = false) {
|
|
10
|
+
return z.union([z.boolean(), z.string()]).transform((val) => {
|
|
11
|
+
if (typeof val === "boolean") return val;
|
|
12
|
+
return val === "1" || val === "true";
|
|
13
|
+
}).default(defaultValue);
|
|
14
|
+
}
|
|
15
|
+
function createEmptyObject(schema, options) {
|
|
16
|
+
const shape = schema.shape;
|
|
17
|
+
const result = {};
|
|
18
|
+
const { useDefaults = true } = options || {};
|
|
19
|
+
for (const key in shape) {
|
|
20
|
+
const field = shape[key];
|
|
21
|
+
if (useDefaults) {
|
|
22
|
+
let hasDefault = false;
|
|
23
|
+
let currentType = field;
|
|
24
|
+
while (currentType instanceof z.ZodOptional || currentType instanceof z.ZodNullable || currentType instanceof z.ZodDefault) {
|
|
25
|
+
if (currentType instanceof z.ZodDefault) {
|
|
26
|
+
hasDefault = true;
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
currentType = currentType.unwrap();
|
|
30
|
+
}
|
|
31
|
+
if (hasDefault) {
|
|
32
|
+
result[key] = field.parse(void 0);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
let isNullable = false;
|
|
37
|
+
let isOptional = false;
|
|
38
|
+
let checkType = field;
|
|
39
|
+
while (checkType instanceof z.ZodOptional || checkType instanceof z.ZodNullable || checkType instanceof z.ZodDefault) {
|
|
40
|
+
if (checkType instanceof z.ZodNullable) isNullable = true;
|
|
41
|
+
if (checkType instanceof z.ZodOptional) isOptional = true;
|
|
42
|
+
checkType = checkType.unwrap();
|
|
43
|
+
}
|
|
44
|
+
if (isNullable) {
|
|
45
|
+
result[key] = null;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (isOptional) {
|
|
49
|
+
result[key] = void 0;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
let baseType = field;
|
|
53
|
+
while (baseType instanceof z.ZodOptional || baseType instanceof z.ZodNullable || baseType instanceof z.ZodDefault) baseType = baseType.unwrap();
|
|
54
|
+
if (baseType instanceof z.ZodString) result[key] = "";
|
|
55
|
+
else if (baseType instanceof z.ZodNumber) {
|
|
56
|
+
let defaultValue = baseType.minValue !== null && baseType.minValue !== void 0 && isFinite(baseType.minValue) ? baseType.minValue : 0;
|
|
57
|
+
if (!baseType.safeParse(defaultValue).success) defaultValue += 1;
|
|
58
|
+
result[key] = defaultValue;
|
|
59
|
+
} else if (baseType instanceof z.ZodBoolean) result[key] = false;
|
|
60
|
+
else if (baseType instanceof z.ZodArray) result[key] = [];
|
|
61
|
+
else if (baseType instanceof z.ZodObject) result[key] = createEmptyObject(baseType, {
|
|
62
|
+
useDefaults: options?.useDefaults,
|
|
63
|
+
withSchema: options?.withSchema
|
|
64
|
+
});
|
|
65
|
+
else if (baseType instanceof z.ZodDate) result[key] = null;
|
|
66
|
+
else if (field instanceof z.ZodOptional || field instanceof z.ZodNullable) result[key] = void 0;
|
|
67
|
+
else result[key] = void 0;
|
|
68
|
+
}
|
|
69
|
+
if (options?.withSchema) result[SCHEMA_SYMBOL] = markRaw(schema);
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
//#endregion
|
|
74
|
+
export { SCHEMA_SYMBOL, createEmptyObject, zObjectArray, zUrlBoolean };
|
package/package.json
CHANGED
|
@@ -1,90 +1,91 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "vue-context-storage",
|
|
3
|
-
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
5
|
-
"description": "Vue 3 context storage system with URL query synchronization support",
|
|
6
|
-
"author": "",
|
|
7
|
-
"license": "MIT",
|
|
8
|
-
"homepage": "https://github.com/lviobio/vue-context-storage#readme",
|
|
9
|
-
"repository": {
|
|
10
|
-
"type": "git",
|
|
11
|
-
"url": "git+https://github.com/lviobio/vue-context-storage.git"
|
|
12
|
-
},
|
|
13
|
-
"bugs": {
|
|
14
|
-
"url": "https://github.com/lviobio/vue-context-storage/issues"
|
|
15
|
-
},
|
|
16
|
-
"exports": {
|
|
17
|
-
".": "./dist/index.js",
|
|
18
|
-
"./
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"ts:check
|
|
34
|
-
"
|
|
35
|
-
"format
|
|
36
|
-
"
|
|
37
|
-
"lint
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
"vue
|
|
49
|
-
"
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
"@
|
|
59
|
-
"@types/
|
|
60
|
-
"@
|
|
61
|
-
"@
|
|
62
|
-
"@vitest/
|
|
63
|
-
"@
|
|
64
|
-
"@vue/
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"eslint
|
|
69
|
-
"eslint-
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
"
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
"
|
|
77
|
-
"
|
|
78
|
-
"
|
|
79
|
-
"typescript
|
|
80
|
-
"
|
|
81
|
-
"
|
|
82
|
-
"vitest
|
|
83
|
-
"vue": "^
|
|
84
|
-
"vue
|
|
85
|
-
"
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "vue-context-storage",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.1.32",
|
|
5
|
+
"description": "Vue 3 context storage system with URL query synchronization support",
|
|
6
|
+
"author": "",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://github.com/lviobio/vue-context-storage#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/lviobio/vue-context-storage.git"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/lviobio/vue-context-storage/issues"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
".": "./dist/index.js",
|
|
18
|
+
"./zod": "./dist/zod.js",
|
|
19
|
+
"./package.json": "./package.json"
|
|
20
|
+
},
|
|
21
|
+
"main": "./dist/index.js",
|
|
22
|
+
"module": "./dist/index.js",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsdown",
|
|
32
|
+
"dev": "tsdown --watch",
|
|
33
|
+
"check": "npm run ts:check && npm run lint:check && npm run format:check && npm run dependency-cruiser:check",
|
|
34
|
+
"ts:check": "vue-tsc --noEmit",
|
|
35
|
+
"format": "prettier --write src/ playground/src",
|
|
36
|
+
"format:check": "prettier --check src/ playground/src",
|
|
37
|
+
"lint": "eslint . --fix",
|
|
38
|
+
"lint:check": "eslint .",
|
|
39
|
+
"dependency-cruiser:check": "depcruise --config .dependency-cruiser.cjs src playground/src",
|
|
40
|
+
"play": "vite",
|
|
41
|
+
"build:playground": "vite build",
|
|
42
|
+
"preview:playground": "vite preview --outDir playground/dist",
|
|
43
|
+
"test": "vitest run",
|
|
44
|
+
"release": "bumpp && npm publish",
|
|
45
|
+
"prepublishOnly": "npm run check && npm run test && npm run build"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"vue": "^3.0.0",
|
|
49
|
+
"vue-router": "^4.0.0 || ^5.0.0",
|
|
50
|
+
"zod": "^4.0.0"
|
|
51
|
+
},
|
|
52
|
+
"peerDependenciesMeta": {
|
|
53
|
+
"zod": {
|
|
54
|
+
"optional": true
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@tailwindcss/vite": "^4.1.18",
|
|
59
|
+
"@types/lodash": "^4.17.21",
|
|
60
|
+
"@types/node": "^25.0.3",
|
|
61
|
+
"@vitejs/plugin-vue": "^6.0.3",
|
|
62
|
+
"@vitest/browser-playwright": "^4.0.16",
|
|
63
|
+
"@vitest/coverage-v8": "^4.0.16",
|
|
64
|
+
"@vue/eslint-config-typescript": "^14.6.0",
|
|
65
|
+
"@vue/test-utils": "^2.4.6",
|
|
66
|
+
"bumpp": "^10.3.2",
|
|
67
|
+
"dependency-cruiser": "^17.3.6",
|
|
68
|
+
"eslint": "^9.39.2",
|
|
69
|
+
"eslint-config-prettier": "^10.1.8",
|
|
70
|
+
"eslint-plugin-vue": "^10.6.2",
|
|
71
|
+
"happy-dom": "^20.1.0",
|
|
72
|
+
"highlight.js": "^11.11.1",
|
|
73
|
+
"jsdom": "^27.4.0",
|
|
74
|
+
"naive-ui": "^2.43.2",
|
|
75
|
+
"playwright": "^1.57.0",
|
|
76
|
+
"prettier": "^3.7.4",
|
|
77
|
+
"tailwindcss": "^4.1.18",
|
|
78
|
+
"tsdown": "^0.18.4",
|
|
79
|
+
"typescript": "^5.9.3",
|
|
80
|
+
"typescript-eslint": "^8.51.0",
|
|
81
|
+
"vite": "^7.3.0",
|
|
82
|
+
"vitest": "^4.0.16",
|
|
83
|
+
"vitest-browser-vue": "^2.0.1",
|
|
84
|
+
"vue": "^3.5.26",
|
|
85
|
+
"vue-tsc": "^3.2.1",
|
|
86
|
+
"zod": "^4.3.5"
|
|
87
|
+
},
|
|
88
|
+
"dependencies": {
|
|
89
|
+
"lodash": "^4.17.23"
|
|
90
|
+
}
|
|
91
|
+
}
|