jazz-tools 0.15.16 → 0.16.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/.svelte-kit/__package__/jazz.class.svelte.d.ts +2 -2
- package/.svelte-kit/__package__/jazz.class.svelte.d.ts.map +1 -1
- package/.svelte-kit/__package__/jazz.class.svelte.js +5 -5
- package/.svelte-kit/__package__/jazz.svelte.d.ts +2 -2
- package/.svelte-kit/__package__/jazz.svelte.d.ts.map +1 -1
- package/.turbo/turbo-build.log +46 -46
- package/CHANGELOG.md +25 -0
- package/dist/browser/index.d.ts +2 -2
- package/dist/browser/index.d.ts.map +1 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/browser-media-images/index.d.ts +3 -1
- package/dist/browser-media-images/index.d.ts.map +1 -1
- package/dist/browser-media-images/index.js.map +1 -1
- package/dist/{chunk-OSVAAVWQ.js → chunk-MLCNE3TL.js} +594 -598
- package/dist/chunk-MLCNE3TL.js.map +1 -0
- package/dist/index.js +8 -10
- package/dist/index.js.map +1 -1
- package/dist/react/hooks.d.ts +2 -2
- package/dist/react/hooks.d.ts.map +1 -1
- package/dist/react/index.js.map +1 -1
- package/dist/react-core/hooks.d.ts +2 -2
- package/dist/react-core/hooks.d.ts.map +1 -1
- package/dist/react-core/index.js +3 -3
- package/dist/react-core/index.js.map +1 -1
- package/dist/react-native-core/hooks.d.ts +2 -2
- package/dist/react-native-core/hooks.d.ts.map +1 -1
- package/dist/react-native-core/index.js.map +1 -1
- package/dist/svelte/jazz.class.svelte.d.ts +2 -2
- package/dist/svelte/jazz.class.svelte.d.ts.map +1 -1
- package/dist/svelte/jazz.class.svelte.js +5 -5
- package/dist/svelte/jazz.svelte.d.ts +2 -2
- package/dist/svelte/jazz.svelte.d.ts.map +1 -1
- package/dist/testing.js +3 -3
- package/dist/testing.js.map +1 -1
- package/dist/tools/coValues/CoValueBase.d.ts +3 -13
- package/dist/tools/coValues/CoValueBase.d.ts.map +1 -1
- package/dist/tools/coValues/account.d.ts +2 -2
- package/dist/tools/coValues/account.d.ts.map +1 -1
- package/dist/tools/coValues/coFeed.d.ts.map +1 -1
- package/dist/tools/coValues/coList.d.ts.map +1 -1
- package/dist/tools/coValues/coMap.d.ts +5 -18
- package/dist/tools/coValues/coMap.d.ts.map +1 -1
- package/dist/tools/coValues/deepLoading.d.ts +4 -1
- package/dist/tools/coValues/deepLoading.d.ts.map +1 -1
- package/dist/tools/coValues/extensions/imageDef.d.ts +4 -7
- package/dist/tools/coValues/extensions/imageDef.d.ts.map +1 -1
- package/dist/tools/coValues/inbox.d.ts +2 -2
- package/dist/tools/coValues/inbox.d.ts.map +1 -1
- package/dist/tools/coValues/interfaces.d.ts +2 -17
- package/dist/tools/coValues/interfaces.d.ts.map +1 -1
- package/dist/tools/coValues/request.d.ts +2 -2
- package/dist/tools/coValues/request.d.ts.map +1 -1
- package/dist/tools/coValues/schemaUnion.d.ts +5 -1
- package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
- package/dist/tools/exports.d.ts +3 -3
- package/dist/tools/exports.d.ts.map +1 -1
- package/dist/tools/implementation/createContext.d.ts +4 -4
- package/dist/tools/implementation/createContext.d.ts.map +1 -1
- package/dist/tools/implementation/invites.d.ts +2 -2
- package/dist/tools/implementation/invites.d.ts.map +1 -1
- package/dist/tools/implementation/schemaUtils.d.ts +8 -0
- package/dist/tools/implementation/schemaUtils.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/coExport.d.ts +11 -1
- package/dist/tools/implementation/zodSchema/coExport.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts +22 -0
- package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts +10 -0
- package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +11 -11
- package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +34 -24
- package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +16 -14
- package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +23 -17
- package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +24 -16
- package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts +20 -9
- package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +18 -12
- package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoValueSchema.d.ts +18 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/CoValueSchema.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +14 -9
- package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +14 -9
- package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +14 -9
- package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +15 -13
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +12 -15
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +17 -20
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +17 -20
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/unionUtils.d.ts +3 -5
- package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodCo.d.ts +10 -8
- package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodReExport.d.ts +4 -8
- package/dist/tools/implementation/zodSchema/zodReExport.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodSchema.d.ts +21 -30
- package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
- package/dist/tools/internal.d.ts +3 -2
- package/dist/tools/internal.d.ts.map +1 -1
- package/dist/tools/lib/utilityTypes.d.ts +10 -0
- package/dist/tools/lib/utilityTypes.d.ts.map +1 -0
- package/dist/tools/subscribe/utils.d.ts.map +1 -1
- package/dist/tools/testing.d.ts +2 -2
- package/dist/tools/testing.d.ts.map +1 -1
- package/dist/worker/index.d.ts.map +1 -1
- package/dist/worker/index.js +2 -2
- package/dist/worker/index.js.map +1 -1
- package/package.json +5 -5
- package/src/browser/index.ts +2 -4
- package/src/browser-media-images/index.ts +1 -1
- package/src/react/hooks.tsx +2 -2
- package/src/react-core/hooks.ts +6 -6
- package/src/react-core/tests/useAccount.test.ts +2 -2
- package/src/react-core/tests/useCoState.test.ts +3 -2
- package/src/react-native-core/hooks.tsx +2 -2
- package/src/svelte/jazz.class.svelte.ts +10 -7
- package/src/svelte/jazz.svelte.ts +2 -2
- package/src/tools/coValues/CoValueBase.ts +8 -20
- package/src/tools/coValues/account.ts +18 -14
- package/src/tools/coValues/coFeed.ts +0 -4
- package/src/tools/coValues/coList.ts +7 -9
- package/src/tools/coValues/coMap.ts +1 -6
- package/src/tools/coValues/coPlainText.ts +4 -4
- package/src/tools/coValues/deepLoading.ts +4 -1
- package/src/tools/coValues/extensions/imageDef.ts +3 -3
- package/src/tools/coValues/inbox.ts +4 -4
- package/src/tools/coValues/interfaces.ts +4 -71
- package/src/tools/coValues/request.ts +15 -13
- package/src/tools/coValues/schemaUnion.ts +8 -4
- package/src/tools/exports.ts +3 -14
- package/src/tools/implementation/createContext.ts +9 -9
- package/src/tools/implementation/invites.ts +2 -2
- package/src/tools/implementation/schemaUtils.ts +18 -0
- package/src/tools/implementation/zodSchema/coExport.ts +14 -0
- package/src/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.ts +156 -0
- package/src/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.ts +133 -0
- package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +36 -17
- package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +101 -52
- package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +54 -50
- package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +54 -46
- package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +187 -137
- package/src/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.ts +29 -27
- package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +28 -18
- package/src/tools/implementation/zodSchema/schemaTypes/CoValueSchema.ts +18 -0
- package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +45 -36
- package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +47 -35
- package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +43 -30
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +28 -23
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +28 -25
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +86 -78
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +91 -82
- package/src/tools/implementation/zodSchema/unionUtils.ts +47 -51
- package/src/tools/implementation/zodSchema/zodCo.ts +42 -75
- package/src/tools/implementation/zodSchema/zodReExport.ts +44 -24
- package/src/tools/implementation/zodSchema/zodSchema.ts +64 -102
- package/src/tools/internal.ts +3 -2
- package/src/tools/lib/utilityTypes.ts +7 -0
- package/src/tools/subscribe/utils.ts +4 -2
- package/src/tools/testing.ts +4 -4
- package/src/tools/tests/ContextManager.test.ts +8 -9
- package/src/tools/tests/account.test.ts +65 -3
- package/src/tools/tests/coDiscriminatedUnion.test-d.ts +38 -0
- package/src/tools/tests/coDiscriminatedUnion.test.ts +219 -1
- package/src/tools/tests/coFeed.test-d.ts +4 -3
- package/src/tools/tests/coList.test-d.ts +32 -3
- package/src/tools/tests/coList.test.ts +20 -2
- package/src/tools/tests/coMap.record.test-d.ts +31 -3
- package/src/tools/tests/coMap.record.test.ts +9 -9
- package/src/tools/tests/coMap.test-d.ts +8 -8
- package/src/tools/tests/coMap.test.ts +19 -5
- package/src/tools/tests/coOptional.test.ts +63 -1
- package/src/tools/tests/createContext.test.ts +7 -9
- package/src/tools/tests/deepLoading.test.ts +4 -10
- package/src/tools/tests/exportImport.test.ts +2 -2
- package/src/tools/tests/groupsAndAccounts.test.ts +5 -4
- package/src/tools/tests/inbox.test.ts +3 -2
- package/src/tools/tests/load.test.ts +3 -29
- package/src/tools/tests/schemaUnion.test.ts +2 -2
- package/src/tools/tests/subscribe.test.ts +22 -114
- package/src/tools/tests/testing.test.ts +6 -6
- package/src/tools/tests/zod.test-d.ts +27 -0
- package/src/tools/tests/zod.test.ts +50 -45
- package/src/worker/index.ts +0 -1
- package/dist/chunk-OSVAAVWQ.js.map +0 -1
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +0 -12
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +0 -1
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +0 -9
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +0 -1
- package/src/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +0 -172
- package/src/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +0 -218
@@ -0,0 +1,133 @@
|
|
1
|
+
import { CoValueClass, isCoValueClass } from "../../../internal.js";
|
2
|
+
import { coField } from "../../schema.js";
|
3
|
+
import { CoreCoValueSchema } from "../schemaTypes/CoValueSchema.js";
|
4
|
+
import { isUnionOfPrimitivesDeeply } from "../unionUtils.js";
|
5
|
+
import {
|
6
|
+
ZodCatch,
|
7
|
+
ZodDefault,
|
8
|
+
ZodLazy,
|
9
|
+
ZodReadonly,
|
10
|
+
z,
|
11
|
+
} from "../zodReExport.js";
|
12
|
+
import { ZodPrimitiveSchema } from "../zodSchema.js";
|
13
|
+
import { isCoValueSchema } from "./coValueSchemaTransformation.js";
|
14
|
+
|
15
|
+
/**
|
16
|
+
* Types of objects that can be nested inside CoValue schema containers
|
17
|
+
*/
|
18
|
+
export type SchemaField =
|
19
|
+
// Schemas created with co.map(), co.record(), co.list(), etc.
|
20
|
+
| CoreCoValueSchema
|
21
|
+
// CoValue classes created with class syntax, or framework-provided classes like Group
|
22
|
+
| CoValueClass
|
23
|
+
| ZodPrimitiveSchema
|
24
|
+
| z.core.$ZodOptional<z.core.$ZodType>
|
25
|
+
| z.core.$ZodUnion<z.core.$ZodType[]>
|
26
|
+
| z.core.$ZodDiscriminatedUnion<z.core.$ZodType[]>
|
27
|
+
| z.core.$ZodObject<z.core.$ZodLooseShape>
|
28
|
+
| z.core.$ZodArray<z.core.$ZodType>
|
29
|
+
| z.core.$ZodTuple<z.core.$ZodType[]>
|
30
|
+
| z.core.$ZodReadonly<z.core.$ZodType>
|
31
|
+
| z.core.$ZodLazy<z.core.$ZodType>
|
32
|
+
| z.core.$ZodTemplateLiteral<any>
|
33
|
+
| z.core.$ZodLiteral<any>
|
34
|
+
| z.core.$ZodCatch<z.core.$ZodType>
|
35
|
+
| z.core.$ZodEnum<any>
|
36
|
+
| z.core.$ZodDefault<z.core.$ZodType>
|
37
|
+
| z.core.$ZodCatch<z.core.$ZodType>;
|
38
|
+
|
39
|
+
export function schemaFieldToCoFieldDef(schema: SchemaField) {
|
40
|
+
if (isCoValueClass(schema)) {
|
41
|
+
return coField.ref(schema);
|
42
|
+
} else if (isCoValueSchema(schema)) {
|
43
|
+
if (schema.builtin === "CoOptional") {
|
44
|
+
return coField.ref(schema.getCoValueClass(), {
|
45
|
+
optional: true,
|
46
|
+
});
|
47
|
+
}
|
48
|
+
return coField.ref(schema.getCoValueClass());
|
49
|
+
} else {
|
50
|
+
if ("_zod" in schema) {
|
51
|
+
const zodSchemaDef = schema._zod.def;
|
52
|
+
if (zodSchemaDef.type === "optional") {
|
53
|
+
const inner = zodSchemaDef.innerType as ZodPrimitiveSchema;
|
54
|
+
return schemaFieldToCoFieldDef(inner);
|
55
|
+
} else if (zodSchemaDef.type === "string") {
|
56
|
+
return coField.string;
|
57
|
+
} else if (zodSchemaDef.type === "number") {
|
58
|
+
return coField.number;
|
59
|
+
} else if (zodSchemaDef.type === "boolean") {
|
60
|
+
return coField.boolean;
|
61
|
+
} else if (zodSchemaDef.type === "null") {
|
62
|
+
return coField.null;
|
63
|
+
} else if (zodSchemaDef.type === "enum") {
|
64
|
+
return coField.string;
|
65
|
+
} else if (zodSchemaDef.type === "readonly") {
|
66
|
+
return schemaFieldToCoFieldDef(
|
67
|
+
(schema as unknown as ZodReadonly).def.innerType as SchemaField,
|
68
|
+
);
|
69
|
+
} else if (zodSchemaDef.type === "date") {
|
70
|
+
return coField.optional.Date;
|
71
|
+
} else if (zodSchemaDef.type === "template_literal") {
|
72
|
+
return coField.string;
|
73
|
+
} else if (zodSchemaDef.type === "lazy") {
|
74
|
+
// Mostly to support z.json()
|
75
|
+
return schemaFieldToCoFieldDef(
|
76
|
+
(schema as unknown as ZodLazy).unwrap() as SchemaField,
|
77
|
+
);
|
78
|
+
} else if (
|
79
|
+
zodSchemaDef.type === "default" ||
|
80
|
+
zodSchemaDef.type === "catch"
|
81
|
+
) {
|
82
|
+
console.warn(
|
83
|
+
"z.default()/z.catch() are not supported in collaborative schemas. They will be ignored.",
|
84
|
+
);
|
85
|
+
|
86
|
+
return schemaFieldToCoFieldDef(
|
87
|
+
(schema as unknown as ZodDefault | ZodCatch).def
|
88
|
+
.innerType as SchemaField,
|
89
|
+
);
|
90
|
+
} else if (zodSchemaDef.type === "literal") {
|
91
|
+
if (
|
92
|
+
zodSchemaDef.values.some((literal) => typeof literal === "undefined")
|
93
|
+
) {
|
94
|
+
throw new Error("z.literal() with undefined is not supported");
|
95
|
+
}
|
96
|
+
if (zodSchemaDef.values.some((literal) => literal === null)) {
|
97
|
+
throw new Error("z.literal() with null is not supported");
|
98
|
+
}
|
99
|
+
if (
|
100
|
+
zodSchemaDef.values.some((literal) => typeof literal === "bigint")
|
101
|
+
) {
|
102
|
+
throw new Error("z.literal() with bigint is not supported");
|
103
|
+
}
|
104
|
+
return coField.literal(
|
105
|
+
...(zodSchemaDef.values as Exclude<
|
106
|
+
(typeof zodSchemaDef.values)[number],
|
107
|
+
undefined | null | bigint
|
108
|
+
>[]),
|
109
|
+
);
|
110
|
+
} else if (
|
111
|
+
zodSchemaDef.type === "object" ||
|
112
|
+
zodSchemaDef.type === "array" ||
|
113
|
+
zodSchemaDef.type === "tuple"
|
114
|
+
) {
|
115
|
+
return coField.json();
|
116
|
+
} else if (zodSchemaDef.type === "union") {
|
117
|
+
if (isUnionOfPrimitivesDeeply(schema)) {
|
118
|
+
return coField.json();
|
119
|
+
} else {
|
120
|
+
throw new Error(
|
121
|
+
"z.union()/z.discriminatedUnion() of collaborative types is not supported. Use co.discriminatedUnion() instead.",
|
122
|
+
);
|
123
|
+
}
|
124
|
+
} else {
|
125
|
+
throw new Error(
|
126
|
+
`Unsupported zod type: ${(schema._zod?.def as any)?.type || JSON.stringify(schema)}`,
|
127
|
+
);
|
128
|
+
}
|
129
|
+
} else {
|
130
|
+
throw new Error(`Unsupported zod type: ${schema}`);
|
131
|
+
}
|
132
|
+
}
|
133
|
+
}
|
@@ -9,8 +9,13 @@ import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
|
|
9
9
|
import { InstanceOrPrimitiveOfSchema } from "../typeConverters/InstanceOrPrimitiveOfSchema.js";
|
10
10
|
import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "../typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
|
11
11
|
import { z } from "../zodReExport.js";
|
12
|
-
import { Loaded, ResolveQuery } from "../zodSchema.js";
|
13
|
-
import {
|
12
|
+
import { AnyZodOrCoValueSchema, Loaded, ResolveQuery } from "../zodSchema.js";
|
13
|
+
import {
|
14
|
+
CoMapSchema,
|
15
|
+
CoMapSchemaDefinition,
|
16
|
+
CoreCoMapSchema,
|
17
|
+
createCoreCoMapSchema,
|
18
|
+
} from "./CoMapSchema.js";
|
14
19
|
|
15
20
|
export type BaseProfileShape = {
|
16
21
|
name: z.core.$ZodString<string>;
|
@@ -19,8 +24,8 @@ export type BaseProfileShape = {
|
|
19
24
|
};
|
20
25
|
|
21
26
|
export type BaseAccountShape = {
|
22
|
-
profile:
|
23
|
-
root:
|
27
|
+
profile: CoreCoMapSchema<BaseProfileShape>;
|
28
|
+
root: CoreCoMapSchema;
|
24
29
|
};
|
25
30
|
|
26
31
|
export type DefaultAccountShape = {
|
@@ -28,9 +33,18 @@ export type DefaultAccountShape = {
|
|
28
33
|
root: CoMapSchema<{}>;
|
29
34
|
};
|
30
35
|
|
31
|
-
export
|
36
|
+
export interface AccountSchema<
|
32
37
|
Shape extends BaseAccountShape = DefaultAccountShape,
|
33
|
-
>
|
38
|
+
> extends CoreAccountSchema<Shape>,
|
39
|
+
Omit<
|
40
|
+
CoMapSchema<Shape>,
|
41
|
+
| "builtin"
|
42
|
+
| "getDefinition"
|
43
|
+
| "create"
|
44
|
+
| "load"
|
45
|
+
| "withMigration"
|
46
|
+
| "getCoValueClass"
|
47
|
+
> {
|
34
48
|
builtin: "Account";
|
35
49
|
|
36
50
|
create: (options: {
|
@@ -63,10 +77,19 @@ export type AccountSchema<
|
|
63
77
|
): AccountSchema<Shape>;
|
64
78
|
|
65
79
|
getCoValueClass: () => typeof Account;
|
66
|
-
}
|
80
|
+
}
|
81
|
+
|
82
|
+
export function createCoreAccountSchema<Shape extends BaseAccountShape>(
|
83
|
+
shape: Shape,
|
84
|
+
): CoreAccountSchema<Shape> {
|
85
|
+
return {
|
86
|
+
...createCoreCoMapSchema(shape),
|
87
|
+
builtin: "Account" as const,
|
88
|
+
};
|
89
|
+
}
|
67
90
|
|
68
91
|
export function enrichAccountSchema<Shape extends BaseAccountShape>(
|
69
|
-
schema:
|
92
|
+
schema: CoreAccountSchema<Shape>,
|
70
93
|
coValueClass: typeof Account,
|
71
94
|
): AccountSchema<Shape> {
|
72
95
|
const enrichedSchema = Object.assign(schema, {
|
@@ -90,9 +113,6 @@ export function enrichAccountSchema<Shape extends BaseAccountShape>(
|
|
90
113
|
// @ts-expect-error
|
91
114
|
return coValueClass.subscribe(...args);
|
92
115
|
},
|
93
|
-
withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
|
94
|
-
return Object.assign(schema, helpers(schema));
|
95
|
-
},
|
96
116
|
fromRaw: (...args: any[]) => {
|
97
117
|
// @ts-expect-error
|
98
118
|
return coValueClass.fromRaw(...args);
|
@@ -127,16 +147,15 @@ export type DefaultProfileShape = {
|
|
127
147
|
|
128
148
|
export type CoProfileSchema<
|
129
149
|
Shape extends z.core.$ZodLooseShape = DefaultProfileShape,
|
130
|
-
|
131
|
-
> = CoMapSchema<Shape & DefaultProfileShape,
|
150
|
+
CatchAll extends AnyZodOrCoValueSchema | unknown = unknown,
|
151
|
+
> = CoMapSchema<Shape & DefaultProfileShape, CatchAll, Group>;
|
132
152
|
|
133
153
|
// less precise version to avoid circularity issues and allow matching against
|
134
|
-
export
|
154
|
+
export interface CoreAccountSchema<
|
135
155
|
Shape extends z.core.$ZodLooseShape = z.core.$ZodLooseShape,
|
136
|
-
>
|
137
|
-
collaborative: true;
|
156
|
+
> extends Omit<CoreCoMapSchema<Shape>, "builtin"> {
|
138
157
|
builtin: "Account";
|
139
|
-
}
|
158
|
+
}
|
140
159
|
|
141
160
|
export type AccountInstance<Shape extends z.core.$ZodLooseShape> = {
|
142
161
|
-readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<Shape[key]>;
|
@@ -1,34 +1,61 @@
|
|
1
1
|
import {
|
2
2
|
Account,
|
3
3
|
AnonymousJazzAgent,
|
4
|
-
|
4
|
+
InstanceOfSchema,
|
5
5
|
InstanceOrPrimitiveOfSchemaCoValuesNullable,
|
6
|
-
RefsToResolve,
|
7
|
-
RefsToResolveStrict,
|
8
6
|
Resolved,
|
9
7
|
SchemaUnion,
|
8
|
+
SchemaUnionConcreteSubclass,
|
10
9
|
SubscribeListenerOptions,
|
10
|
+
coOptionalDefiner,
|
11
11
|
} from "../../../internal.js";
|
12
12
|
import { z } from "../zodReExport.js";
|
13
|
+
import { CoOptionalSchema } from "./CoOptionalSchema.js";
|
14
|
+
import { CoreCoValueSchema } from "./CoValueSchema.js";
|
13
15
|
|
14
|
-
export
|
15
|
-
z.core.$
|
16
|
+
export interface DiscriminableCoValueSchemaDefinition {
|
17
|
+
discriminatorMap: z.core.$ZodDiscriminatedUnionInternals["propValues"];
|
18
|
+
}
|
19
|
+
|
20
|
+
export interface DiscriminableCoreCoValueSchema extends CoreCoValueSchema {
|
21
|
+
getDefinition: () => DiscriminableCoValueSchemaDefinition;
|
22
|
+
}
|
23
|
+
|
24
|
+
export interface CoDiscriminatedUnionSchemaDefinition<
|
25
|
+
Options extends DiscriminableCoValueSchemas,
|
26
|
+
> extends DiscriminableCoValueSchemaDefinition {
|
27
|
+
discriminator: string;
|
28
|
+
options: Options;
|
29
|
+
}
|
30
|
+
|
31
|
+
export type DiscriminableCoValueSchemas = [
|
32
|
+
DiscriminableCoreCoValueSchema,
|
33
|
+
...DiscriminableCoreCoValueSchema[],
|
34
|
+
];
|
35
|
+
|
36
|
+
export interface CoreCoDiscriminatedUnionSchema<
|
37
|
+
Options extends DiscriminableCoValueSchemas = DiscriminableCoValueSchemas,
|
38
|
+
> extends DiscriminableCoreCoValueSchema {
|
39
|
+
builtin: "CoDiscriminatedUnion";
|
40
|
+
getDefinition: () => CoDiscriminatedUnionSchemaDefinition<Options>;
|
41
|
+
}
|
42
|
+
export class CoDiscriminatedUnionSchema<
|
43
|
+
Options extends DiscriminableCoValueSchemas,
|
44
|
+
> implements CoreCoDiscriminatedUnionSchema<Options>
|
45
|
+
{
|
46
|
+
readonly collaborative = true as const;
|
47
|
+
readonly builtin = "CoDiscriminatedUnion" as const;
|
48
|
+
readonly getDefinition: () => CoDiscriminatedUnionSchemaDefinition<Options>;
|
16
49
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
}
|
50
|
+
constructor(
|
51
|
+
coreSchema: CoreCoDiscriminatedUnionSchema<Options>,
|
52
|
+
private coValueClass: SchemaUnionConcreteSubclass<
|
53
|
+
InstanceOfSchema<Options[number]>
|
54
|
+
>,
|
55
|
+
) {
|
56
|
+
this.getDefinition = coreSchema.getDefinition;
|
57
|
+
}
|
25
58
|
|
26
|
-
export type CoDiscriminatedUnionSchema<
|
27
|
-
Types extends readonly [
|
28
|
-
AnyDiscriminableCoSchema,
|
29
|
-
...AnyDiscriminableCoSchema[],
|
30
|
-
],
|
31
|
-
> = AnyCoDiscriminatedUnionSchema<Types> & {
|
32
59
|
load(
|
33
60
|
id: string,
|
34
61
|
options?: {
|
@@ -36,55 +63,77 @@ export type CoDiscriminatedUnionSchema<
|
|
36
63
|
skipRetry?: boolean;
|
37
64
|
},
|
38
65
|
): Promise<Resolved<
|
39
|
-
CoDiscriminatedUnionInstanceCoValuesNullable<
|
66
|
+
CoDiscriminatedUnionInstanceCoValuesNullable<Options> & SchemaUnion,
|
40
67
|
true
|
41
|
-
> | null
|
68
|
+
> | null> {
|
69
|
+
return this.coValueClass.load(id, options) as any;
|
70
|
+
}
|
42
71
|
|
43
72
|
subscribe(
|
44
73
|
id: string,
|
45
74
|
options: SubscribeListenerOptions<
|
46
|
-
CoDiscriminatedUnionInstanceCoValuesNullable<
|
75
|
+
CoDiscriminatedUnionInstanceCoValuesNullable<Options> & SchemaUnion,
|
47
76
|
true
|
48
77
|
>,
|
49
78
|
listener: (
|
50
79
|
value: Resolved<
|
51
|
-
CoDiscriminatedUnionInstanceCoValuesNullable<
|
80
|
+
CoDiscriminatedUnionInstanceCoValuesNullable<Options> & SchemaUnion,
|
52
81
|
true
|
53
82
|
>,
|
54
83
|
unsubscribe: () => void,
|
55
84
|
) => void,
|
56
|
-
): () => void
|
85
|
+
): () => void {
|
86
|
+
// @ts-expect-error
|
87
|
+
return this.coValueClass.subscribe(id, options, listener);
|
88
|
+
}
|
57
89
|
|
58
|
-
getCoValueClass
|
59
|
-
|
90
|
+
getCoValueClass(): SchemaUnionConcreteSubclass<
|
91
|
+
InstanceOfSchema<Options[number]>
|
92
|
+
> {
|
93
|
+
return this.coValueClass;
|
94
|
+
}
|
95
|
+
|
96
|
+
optional(): CoOptionalSchema<this> {
|
97
|
+
return coOptionalDefiner(this);
|
98
|
+
}
|
99
|
+
}
|
60
100
|
|
61
|
-
export function
|
62
|
-
|
63
|
-
AnyDiscriminableCoSchema,
|
64
|
-
...AnyDiscriminableCoSchema[],
|
65
|
-
],
|
101
|
+
export function createCoreCoDiscriminatedUnionSchema<
|
102
|
+
Options extends DiscriminableCoValueSchemas,
|
66
103
|
>(
|
67
|
-
|
68
|
-
|
69
|
-
):
|
70
|
-
return
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
104
|
+
discriminator: string,
|
105
|
+
schemas: Options,
|
106
|
+
): CoreCoDiscriminatedUnionSchema<Options> {
|
107
|
+
return {
|
108
|
+
collaborative: true as const,
|
109
|
+
builtin: "CoDiscriminatedUnion" as const,
|
110
|
+
getDefinition: () => ({
|
111
|
+
discriminator,
|
112
|
+
get discriminatorMap() {
|
113
|
+
const propValues: DiscriminableCoValueSchemaDefinition["discriminatorMap"] =
|
114
|
+
{};
|
115
|
+
for (const option of schemas) {
|
116
|
+
const dm = option.getDefinition().discriminatorMap;
|
117
|
+
if (!dm || Object.keys(dm).length === 0)
|
118
|
+
throw new Error(
|
119
|
+
`Invalid discriminated union option at index "${schemas.indexOf(option)}"`,
|
120
|
+
);
|
121
|
+
for (const [k, v] of Object.entries(dm)) {
|
122
|
+
propValues[k] ??= new Set();
|
123
|
+
for (const val of v) {
|
124
|
+
propValues[k].add(val);
|
125
|
+
}
|
126
|
+
}
|
127
|
+
}
|
128
|
+
return propValues;
|
129
|
+
},
|
130
|
+
get options() {
|
131
|
+
return schemas;
|
132
|
+
},
|
133
|
+
}),
|
134
|
+
};
|
83
135
|
}
|
84
136
|
|
85
137
|
type CoDiscriminatedUnionInstanceCoValuesNullable<
|
86
|
-
|
87
|
-
|
88
|
-
...AnyDiscriminableCoSchema[],
|
89
|
-
],
|
90
|
-
> = NonNullable<InstanceOrPrimitiveOfSchemaCoValuesNullable<Types[number]>>;
|
138
|
+
Options extends DiscriminableCoValueSchemas,
|
139
|
+
> = NonNullable<InstanceOrPrimitiveOfSchemaCoValuesNullable<Options[number]>>;
|
@@ -1,36 +1,43 @@
|
|
1
1
|
import {
|
2
2
|
Account,
|
3
|
+
AnyZodOrCoValueSchema,
|
3
4
|
CoFeed,
|
4
5
|
Group,
|
6
|
+
NotNull,
|
5
7
|
RefsToResolve,
|
6
8
|
RefsToResolveStrict,
|
7
9
|
Resolved,
|
10
|
+
Simplify,
|
8
11
|
SubscribeListenerOptions,
|
12
|
+
coOptionalDefiner,
|
9
13
|
} from "../../../internal.js";
|
10
14
|
import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
|
11
|
-
import { InstanceOfSchema } from "../typeConverters/InstanceOfSchema.js";
|
12
15
|
import { InstanceOrPrimitiveOfSchema } from "../typeConverters/InstanceOrPrimitiveOfSchema.js";
|
13
16
|
import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "../typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
|
14
|
-
import {
|
17
|
+
import { CoOptionalSchema } from "./CoOptionalSchema.js";
|
18
|
+
import { CoreCoValueSchema } from "./CoValueSchema.js";
|
15
19
|
|
16
|
-
type CoFeedInit<T extends
|
17
|
-
T
|
18
|
-
? InstanceOrPrimitiveOfSchemaCoValuesNullable<T>
|
19
|
-
: NonNullable<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>>
|
20
|
+
type CoFeedInit<T extends AnyZodOrCoValueSchema> = Simplify<
|
21
|
+
Array<NotNull<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>>>
|
20
22
|
>;
|
21
23
|
|
22
|
-
export
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
24
|
+
export class CoFeedSchema<T extends AnyZodOrCoValueSchema>
|
25
|
+
implements CoreCoFeedSchema<T>
|
26
|
+
{
|
27
|
+
collaborative = true as const;
|
28
|
+
builtin = "CoFeed" as const;
|
29
|
+
|
30
|
+
constructor(
|
31
|
+
public element: T,
|
32
|
+
private coValueClass: typeof CoFeed,
|
33
|
+
) {}
|
29
34
|
|
30
35
|
create(
|
31
36
|
init: CoFeedInit<T>,
|
32
37
|
options?: { owner: Account | Group } | Account | Group,
|
33
|
-
): CoFeedInstance<T
|
38
|
+
): CoFeedInstance<T> {
|
39
|
+
return this.coValueClass.create(init, options) as CoFeedInstance<T>;
|
40
|
+
}
|
34
41
|
|
35
42
|
load<const R extends RefsToResolve<CoFeedInstanceCoValuesNullable<T>> = true>(
|
36
43
|
id: string,
|
@@ -38,7 +45,10 @@ export type CoFeedSchema<T extends z.core.$ZodType> = z.core.$ZodCustom<
|
|
38
45
|
resolve?: RefsToResolveStrict<CoFeedInstanceCoValuesNullable<T>, R>;
|
39
46
|
loadAs?: Account | AnonymousJazzAgent;
|
40
47
|
},
|
41
|
-
): Promise<Resolved<CoFeedInstanceCoValuesNullable<T>, R> | null
|
48
|
+
): Promise<Resolved<CoFeedInstanceCoValuesNullable<T>, R> | null> {
|
49
|
+
// @ts-expect-error
|
50
|
+
return this.coValueClass.load(id, options);
|
51
|
+
}
|
42
52
|
|
43
53
|
subscribe(
|
44
54
|
id: string,
|
@@ -57,47 +67,41 @@ export type CoFeedSchema<T extends z.core.$ZodType> = z.core.$ZodCustom<
|
|
57
67
|
unsubscribe: () => void,
|
58
68
|
) => void,
|
59
69
|
): () => void;
|
70
|
+
subscribe(...args: [any, ...any[]]) {
|
71
|
+
// @ts-expect-error
|
72
|
+
return this.coValueClass.subscribe(...args);
|
73
|
+
}
|
60
74
|
|
61
|
-
getCoValueClass
|
62
|
-
|
75
|
+
getCoValueClass(): typeof CoFeed {
|
76
|
+
return this.coValueClass;
|
77
|
+
}
|
63
78
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
): CoFeedSchema<T> {
|
68
|
-
return Object.assign(schema, {
|
69
|
-
create: (...args: [any, ...any[]]) => {
|
70
|
-
return coValueClass.create(...args);
|
71
|
-
},
|
72
|
-
load: (...args: [any, ...any[]]) => {
|
73
|
-
// @ts-expect-error
|
74
|
-
return coValueClass.load(...args);
|
75
|
-
},
|
76
|
-
subscribe: (...args: [any, ...any[]]) => {
|
77
|
-
// @ts-expect-error
|
78
|
-
return coValueClass.subscribe(...args);
|
79
|
-
},
|
80
|
-
withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
|
81
|
-
return Object.assign(schema, helpers(schema));
|
82
|
-
},
|
83
|
-
getCoValueClass: () => {
|
84
|
-
return coValueClass;
|
85
|
-
},
|
86
|
-
}) as unknown as CoFeedSchema<T>;
|
79
|
+
optional(): CoOptionalSchema<this> {
|
80
|
+
return coOptionalDefiner(this);
|
81
|
+
}
|
87
82
|
}
|
88
83
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
84
|
+
export function createCoreCoFeedSchema<T extends AnyZodOrCoValueSchema>(
|
85
|
+
element: T,
|
86
|
+
): CoreCoFeedSchema<T> {
|
87
|
+
return {
|
88
|
+
collaborative: true as const,
|
89
|
+
builtin: "CoFeed" as const,
|
90
|
+
element,
|
95
91
|
};
|
92
|
+
}
|
93
|
+
|
94
|
+
// less precise version to avoid circularity issues and allow matching against
|
95
|
+
export interface CoreCoFeedSchema<
|
96
|
+
T extends AnyZodOrCoValueSchema = AnyZodOrCoValueSchema,
|
97
|
+
> extends CoreCoValueSchema {
|
98
|
+
builtin: "CoFeed";
|
99
|
+
element: T;
|
100
|
+
}
|
96
101
|
|
97
|
-
export type CoFeedInstance<T extends
|
102
|
+
export type CoFeedInstance<T extends AnyZodOrCoValueSchema> = CoFeed<
|
98
103
|
InstanceOrPrimitiveOfSchema<T>
|
99
104
|
>;
|
100
105
|
|
101
|
-
export type CoFeedInstanceCoValuesNullable<T extends
|
102
|
-
InstanceOrPrimitiveOfSchemaCoValuesNullable<T
|
103
|
-
>;
|
106
|
+
export type CoFeedInstanceCoValuesNullable<T extends AnyZodOrCoValueSchema> =
|
107
|
+
CoFeed<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>>;
|