jazz-tools 0.15.9 → 0.15.11
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/.turbo/turbo-build.log +38 -38
- package/CHANGELOG.md +20 -0
- package/dist/{chunk-5PFEKHX5.js → chunk-MLLG3GBR.js} +422 -303
- package/dist/chunk-MLLG3GBR.js.map +1 -0
- package/dist/index.js +1 -1
- package/dist/react/index.js +2 -0
- package/dist/react/index.js.map +1 -1
- package/dist/react/testing.js +2 -0
- package/dist/react/testing.js.map +1 -1
- package/dist/testing.js +1 -1
- package/dist/tools/coValues/CoValueBase.d.ts.map +1 -1
- package/dist/tools/coValues/coMap.d.ts.map +1 -1
- package/dist/tools/coValues/inbox.d.ts.map +1 -1
- package/dist/tools/coValues/schemaUnion.d.ts +27 -1
- package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/coExport.d.ts +1 -1
- package/dist/tools/implementation/zodSchema/coExport.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +6 -3
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +6 -25
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +2 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +30 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +2 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +3 -3
- package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +3 -3
- package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts +11 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +2 -3
- package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +5 -2
- package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +5 -2
- package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +5 -2
- package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +7 -12
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/unionUtils.d.ts +2 -3
- package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodCo.d.ts +6 -6
- package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodReExport.d.ts +8 -1
- package/dist/tools/implementation/zodSchema/zodReExport.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodSchema.d.ts +18 -13
- package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
- package/dist/tools/internal.d.ts +1 -0
- package/dist/tools/internal.d.ts.map +1 -1
- package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
- package/dist/tools/tests/coDiscriminatedUnion.test-d.d.ts +2 -0
- package/dist/tools/tests/coDiscriminatedUnion.test-d.d.ts.map +1 -0
- package/dist/tools/tests/coDiscriminatedUnion.test.d.ts +2 -0
- package/dist/tools/tests/coDiscriminatedUnion.test.d.ts.map +1 -0
- package/dist/tools/tests/coOptional.test.d.ts +2 -0
- package/dist/tools/tests/coOptional.test.d.ts.map +1 -0
- package/dist/tools/tests/zod.test-d.d.ts +2 -0
- package/dist/tools/tests/zod.test-d.d.ts.map +1 -0
- package/package.json +5 -4
- package/src/react-core/tests/useAccount.test.ts +1 -11
- package/src/tools/coValues/CoValueBase.ts +2 -2
- package/src/tools/coValues/inbox.ts +0 -2
- package/src/tools/coValues/schemaUnion.ts +62 -0
- package/src/tools/implementation/zodSchema/coExport.ts +2 -0
- package/src/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +29 -12
- package/src/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +93 -63
- package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +62 -3
- package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +90 -0
- package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +27 -2
- package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +27 -5
- package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +60 -5
- package/src/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.ts +33 -0
- package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +3 -5
- package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +32 -2
- package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +30 -2
- package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +26 -2
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +10 -13
- package/src/tools/implementation/zodSchema/unionUtils.ts +22 -14
- package/src/tools/implementation/zodSchema/zodCo.ts +68 -256
- package/src/tools/implementation/zodSchema/zodReExport.ts +35 -5
- package/src/tools/implementation/zodSchema/zodSchema.ts +78 -19
- package/src/tools/internal.ts +1 -0
- package/src/tools/subscribe/SubscriptionScope.ts +1 -2
- package/src/tools/tests/ContextManager.test.ts +5 -6
- package/src/tools/tests/coDiscriminatedUnion.test-d.ts +57 -0
- package/src/tools/tests/coDiscriminatedUnion.test.ts +93 -0
- package/src/tools/tests/coList.test.ts +4 -12
- package/src/tools/tests/coMap.record.test.ts +4 -4
- package/src/tools/tests/coMap.test-d.ts +3 -3
- package/src/tools/tests/coMap.test.ts +10 -10
- package/src/tools/tests/coOptional.test.ts +39 -0
- package/src/tools/tests/createContext.test.ts +5 -4
- package/src/tools/tests/deepLoading.test.ts +9 -9
- package/src/tools/tests/inbox.test.ts +3 -11
- package/src/tools/tests/patterns/requestToJoin.test.ts +1 -1
- package/src/tools/tests/schemaUnion.test.ts +11 -22
- package/src/tools/tests/subscribe.test.ts +25 -39
- package/src/tools/tests/zod.test-d.ts +70 -0
- package/src/tools/tests/zod.test.ts +64 -5
- package/dist/chunk-5PFEKHX5.js.map +0 -1
@@ -1,8 +1,19 @@
|
|
1
1
|
import {
|
2
|
+
Account,
|
3
|
+
AnonymousJazzAgent,
|
2
4
|
CoValue,
|
3
5
|
CoValueBase,
|
4
6
|
CoValueClass,
|
5
7
|
CoValueFromRaw,
|
8
|
+
ID,
|
9
|
+
RefsToResolve,
|
10
|
+
RefsToResolveStrict,
|
11
|
+
Resolved,
|
12
|
+
SubscribeListenerOptions,
|
13
|
+
SubscribeRestArgs,
|
14
|
+
loadCoValueWithoutMe,
|
15
|
+
parseSubscribeRestArgs,
|
16
|
+
subscribeToCoValueWithoutMe,
|
6
17
|
} from "../internal.js";
|
7
18
|
|
8
19
|
/**
|
@@ -99,4 +110,55 @@ export abstract class SchemaUnion extends CoValueBase implements CoValue {
|
|
99
110
|
static fromRaw<V extends CoValue>(this: CoValueClass<V>, raw: V["_raw"]): V {
|
100
111
|
throw new Error("Not implemented");
|
101
112
|
}
|
113
|
+
|
114
|
+
/**
|
115
|
+
* Load a `SchemaUnion` with a given ID, as a given account.
|
116
|
+
*
|
117
|
+
* Note: The `resolve` option is not supported for `SchemaUnion`s due to https://github.com/garden-co/jazz/issues/2639
|
118
|
+
*
|
119
|
+
* @category Subscription & Loading
|
120
|
+
*/
|
121
|
+
static load<M extends SchemaUnion>(
|
122
|
+
this: CoValueClass<M>,
|
123
|
+
id: ID<M>,
|
124
|
+
options?: {
|
125
|
+
loadAs?: Account | AnonymousJazzAgent;
|
126
|
+
skipRetry?: boolean;
|
127
|
+
},
|
128
|
+
): Promise<Resolved<M, true> | null> {
|
129
|
+
return loadCoValueWithoutMe(this, id, options);
|
130
|
+
}
|
131
|
+
|
132
|
+
/**
|
133
|
+
* Load and subscribe to a `CoMap` with a given ID, as a given account.
|
134
|
+
*
|
135
|
+
* Automatically also subscribes to updates to all referenced/nested CoValues as soon as they are accessed in the listener.
|
136
|
+
*
|
137
|
+
* Returns an unsubscribe function that you should call when you no longer need updates.
|
138
|
+
*
|
139
|
+
* Also see the `useCoState` hook to reactively subscribe to a CoValue in a React component.
|
140
|
+
*
|
141
|
+
* Note: The `resolve` option is not supported for `SchemaUnion`s due to https://github.com/garden-co/jazz/issues/2639
|
142
|
+
*
|
143
|
+
* @category Subscription & Loading
|
144
|
+
*/
|
145
|
+
static subscribe<M extends SchemaUnion>(
|
146
|
+
this: CoValueClass<M>,
|
147
|
+
id: ID<M>,
|
148
|
+
listener: (value: Resolved<M, true>, unsubscribe: () => void) => void,
|
149
|
+
): () => void;
|
150
|
+
static subscribe<M extends SchemaUnion>(
|
151
|
+
this: CoValueClass<M>,
|
152
|
+
id: ID<M>,
|
153
|
+
options: SubscribeListenerOptions<M, true>,
|
154
|
+
listener: (value: Resolved<M, true>, unsubscribe: () => void) => void,
|
155
|
+
): () => void;
|
156
|
+
static subscribe<M extends SchemaUnion>(
|
157
|
+
this: CoValueClass<M>,
|
158
|
+
id: ID<M>,
|
159
|
+
...args: SubscribeRestArgs<M, true>
|
160
|
+
): () => void {
|
161
|
+
const { options, listener } = parseSubscribeRestArgs(args);
|
162
|
+
return subscribeToCoValueWithoutMe<M, true>(this, id, options, listener);
|
163
|
+
}
|
102
164
|
}
|
@@ -1,5 +1,6 @@
|
|
1
1
|
import { CoMap, CoValueClass, isCoValueClass } from "../../../internal.js";
|
2
2
|
import { coField } from "../../schema.js";
|
3
|
+
import { isAnyCoOptionalSchema } from "../schemaTypes/CoOptionalSchema.js";
|
3
4
|
import {
|
4
5
|
isUnionOfCoMapsDeeply,
|
5
6
|
isUnionOfPrimitivesDeeply,
|
@@ -12,10 +13,19 @@ import {
|
|
12
13
|
ZodReadonly,
|
13
14
|
z,
|
14
15
|
} from "../zodReExport.js";
|
15
|
-
import { ZodPrimitiveSchema } from "../zodSchema.js";
|
16
|
-
import {
|
16
|
+
import { AnyCoSchema, ZodPrimitiveSchema } from "../zodSchema.js";
|
17
|
+
import {
|
18
|
+
isCoValueSchema,
|
19
|
+
zodSchemaToCoSchemaOrKeepPrimitive,
|
20
|
+
} from "./zodSchemaToCoSchema.js";
|
17
21
|
|
18
|
-
|
22
|
+
/**
|
23
|
+
* Types of objects that can be nested inside CoValue schema containers
|
24
|
+
*/
|
25
|
+
type SchemaField =
|
26
|
+
// Schemas created with co.map(), co.record(), co.list(), etc.
|
27
|
+
| AnyCoSchema
|
28
|
+
// CoValue classes created with class syntax, or framework-provided classes like Group
|
19
29
|
| CoValueClass
|
20
30
|
| ZodPrimitiveSchema
|
21
31
|
| z.core.$ZodOptional<z.core.$ZodType>
|
@@ -33,9 +43,16 @@ type FieldSchema =
|
|
33
43
|
| z.core.$ZodCatch<z.core.$ZodType>
|
34
44
|
| (z.core.$ZodCustom<any, any> & { builtin: any });
|
35
45
|
|
36
|
-
export function
|
46
|
+
export function schemaFieldToCoFieldDef(schema: SchemaField) {
|
37
47
|
if (isCoValueClass(schema)) {
|
38
48
|
return coField.ref(schema);
|
49
|
+
} else if (isCoValueSchema(schema)) {
|
50
|
+
if (isAnyCoOptionalSchema(schema)) {
|
51
|
+
return coField.ref(schema.getCoValueClass(), {
|
52
|
+
optional: true,
|
53
|
+
});
|
54
|
+
}
|
55
|
+
return coField.ref(schema.getCoValueClass());
|
39
56
|
} else {
|
40
57
|
if ("_zod" in schema) {
|
41
58
|
if (schema._zod.def.type === "optional") {
|
@@ -45,7 +62,7 @@ export function zodFieldToCoFieldDef(schema: FieldSchema) {
|
|
45
62
|
if (isCoValueClass(inner)) {
|
46
63
|
return coField.ref(inner, { optional: true });
|
47
64
|
} else {
|
48
|
-
return
|
65
|
+
return schemaFieldToCoFieldDef(inner);
|
49
66
|
}
|
50
67
|
} else if (schema._zod.def.type === "string") {
|
51
68
|
return coField.string;
|
@@ -58,8 +75,8 @@ export function zodFieldToCoFieldDef(schema: FieldSchema) {
|
|
58
75
|
} else if (schema._zod.def.type === "enum") {
|
59
76
|
return coField.string;
|
60
77
|
} else if (schema._zod.def.type === "readonly") {
|
61
|
-
return
|
62
|
-
(schema as unknown as ZodReadonly).def.innerType as
|
78
|
+
return schemaFieldToCoFieldDef(
|
79
|
+
(schema as unknown as ZodReadonly).def.innerType as SchemaField,
|
63
80
|
);
|
64
81
|
} else if (schema._zod.def.type === "date") {
|
65
82
|
return coField.optional.Date;
|
@@ -67,8 +84,8 @@ export function zodFieldToCoFieldDef(schema: FieldSchema) {
|
|
67
84
|
return coField.string;
|
68
85
|
} else if (schema._zod.def.type === "lazy") {
|
69
86
|
// Mostly to support z.json()
|
70
|
-
return
|
71
|
-
(schema as unknown as ZodLazy).unwrap() as
|
87
|
+
return schemaFieldToCoFieldDef(
|
88
|
+
(schema as unknown as ZodLazy).unwrap() as SchemaField,
|
72
89
|
);
|
73
90
|
} else if (
|
74
91
|
schema._zod.def.type === "default" ||
|
@@ -78,9 +95,9 @@ export function zodFieldToCoFieldDef(schema: FieldSchema) {
|
|
78
95
|
"z.default()/z.catch() are not supported in collaborative schemas. They will be ignored.",
|
79
96
|
);
|
80
97
|
|
81
|
-
return
|
98
|
+
return schemaFieldToCoFieldDef(
|
82
99
|
(schema as unknown as ZodDefault | ZodCatch).def
|
83
|
-
.innerType as
|
100
|
+
.innerType as SchemaField,
|
84
101
|
);
|
85
102
|
} else if (schema._zod.def.type === "literal") {
|
86
103
|
if (
|
@@ -112,7 +129,7 @@ export function zodFieldToCoFieldDef(schema: FieldSchema) {
|
|
112
129
|
return coField.json();
|
113
130
|
} else if (schema._zod.def.type === "custom") {
|
114
131
|
if ("builtin" in schema) {
|
115
|
-
return
|
132
|
+
return schemaFieldToCoFieldDef(schema.builtin);
|
116
133
|
} else {
|
117
134
|
throw new Error(`Unsupported custom zod type`);
|
118
135
|
}
|
@@ -9,89 +9,140 @@ import {
|
|
9
9
|
CoValueClass,
|
10
10
|
FileStream,
|
11
11
|
SchemaUnion,
|
12
|
+
enrichAccountSchema,
|
13
|
+
enrichCoDiscriminatedUnionSchema,
|
14
|
+
enrichCoFeedSchema,
|
15
|
+
enrichCoListSchema,
|
16
|
+
enrichCoMapSchema,
|
17
|
+
enrichFileStreamSchema,
|
18
|
+
enrichPlainTextSchema,
|
12
19
|
isCoValueClass,
|
13
20
|
} from "../../../internal.js";
|
14
21
|
import { coField } from "../../schema.js";
|
22
|
+
import { isAnyCoOptionalSchema } from "../schemaTypes/CoOptionalSchema.js";
|
23
|
+
import { enrichRichTextSchema } from "../schemaTypes/RichTextSchema.js";
|
15
24
|
import {
|
16
25
|
isUnionOfCoMapsDeeply,
|
17
26
|
schemaUnionDiscriminatorFor,
|
18
27
|
} from "../unionUtils.js";
|
19
28
|
import { z } from "../zodReExport.js";
|
20
29
|
import {
|
21
|
-
|
30
|
+
AnyCoSchema,
|
31
|
+
CoValueClassFromAnySchema,
|
32
|
+
CoValueOrZodSchema,
|
33
|
+
CoValueSchemaFromZodSchema,
|
22
34
|
ZodPrimitiveSchema,
|
23
35
|
getDef,
|
24
36
|
isZodArray,
|
25
37
|
isZodCustom,
|
26
38
|
isZodObject,
|
27
39
|
} from "../zodSchema.js";
|
28
|
-
import {
|
40
|
+
import { schemaFieldToCoFieldDef } from "./zodFieldToCoFieldDef.js";
|
29
41
|
|
30
|
-
let coSchemasForZodSchemas = new Map<z.core.$ZodType,
|
42
|
+
let coSchemasForZodSchemas = new Map<z.core.$ZodType, AnyCoSchema>();
|
31
43
|
|
32
|
-
export function
|
44
|
+
export function isAnyCoValueSchema(
|
45
|
+
schema: z.core.$ZodType | CoValueClass,
|
46
|
+
): schema is AnyCoSchema {
|
47
|
+
return "collaborative" in schema && schema.collaborative === true;
|
48
|
+
}
|
49
|
+
|
50
|
+
export function isCoValueSchema(
|
51
|
+
schema: z.core.$ZodType | CoValueClass,
|
52
|
+
): schema is CoValueSchemaFromZodSchema<AnyCoSchema> {
|
53
|
+
return isAnyCoValueSchema(schema) && "getCoValueClass" in schema;
|
54
|
+
}
|
55
|
+
|
56
|
+
/**
|
57
|
+
* Convert a Zod schema into a CoValue schema.
|
58
|
+
*
|
59
|
+
* @param schema A Zod schema that may represent a CoValue schema
|
60
|
+
* @returns The CoValue schema matching the provided ProtoCoSchema, or `null` if the Zod schema
|
61
|
+
* does not match a CoValue schema.
|
62
|
+
*/
|
63
|
+
function tryZodSchemaToCoSchema<S extends z.core.$ZodType>(
|
33
64
|
schema: S,
|
34
|
-
):
|
35
|
-
if (
|
65
|
+
): CoValueSchemaFromZodSchema<S> | null {
|
66
|
+
if (isAnyCoValueSchema(schema)) {
|
36
67
|
if (coSchemasForZodSchemas.has(schema)) {
|
37
|
-
return coSchemasForZodSchemas.get(
|
68
|
+
return coSchemasForZodSchemas.get(
|
69
|
+
schema,
|
70
|
+
) as CoValueSchemaFromZodSchema<S>;
|
38
71
|
}
|
39
72
|
|
40
|
-
if (
|
73
|
+
if (isAnyCoOptionalSchema(schema)) {
|
74
|
+
// Optional schemas are not supported as top-level schemas
|
75
|
+
return null;
|
76
|
+
} else if (isZodObject(schema)) {
|
41
77
|
const def = getDef(schema);
|
42
78
|
|
43
79
|
const ClassToExtend =
|
44
80
|
"builtin" in schema && schema.builtin === "Account" ? Account : CoMap;
|
45
81
|
|
46
|
-
const
|
82
|
+
const coValueClass = class ZCoMap extends ClassToExtend {
|
47
83
|
constructor(options: { fromRaw: RawCoMap } | undefined) {
|
48
84
|
super(options);
|
49
85
|
for (const [field, fieldType] of Object.entries(
|
50
86
|
def.shape as z.core.$ZodShape,
|
51
87
|
)) {
|
52
|
-
(this as any)[field] =
|
88
|
+
(this as any)[field] = schemaFieldToCoFieldDef(
|
53
89
|
zodSchemaToCoSchemaOrKeepPrimitive(fieldType),
|
54
90
|
);
|
55
91
|
}
|
56
92
|
if (def.catchall) {
|
57
|
-
(this as any)[coField.items] =
|
93
|
+
(this as any)[coField.items] = schemaFieldToCoFieldDef(
|
58
94
|
zodSchemaToCoSchemaOrKeepPrimitive(def.catchall),
|
59
95
|
);
|
60
96
|
}
|
61
97
|
}
|
62
98
|
};
|
63
99
|
|
64
|
-
|
65
|
-
|
100
|
+
const coValueSchema =
|
101
|
+
ClassToExtend === Account
|
102
|
+
? enrichAccountSchema(schema as any, coValueClass as any)
|
103
|
+
: enrichCoMapSchema(schema as any, coValueClass as any);
|
104
|
+
|
105
|
+
coSchemasForZodSchemas.set(schema, coValueSchema);
|
106
|
+
return coValueSchema as unknown as CoValueSchemaFromZodSchema<S>;
|
66
107
|
} else if (isZodArray(schema)) {
|
67
108
|
const def = getDef(schema);
|
68
|
-
const
|
109
|
+
const coValueClass = class ZCoList extends CoList {
|
69
110
|
constructor(options: { fromRaw: RawCoList } | undefined) {
|
70
111
|
super(options);
|
71
|
-
(this as any)[coField.items] =
|
112
|
+
(this as any)[coField.items] = schemaFieldToCoFieldDef(
|
72
113
|
zodSchemaToCoSchemaOrKeepPrimitive(def.element),
|
73
114
|
);
|
74
115
|
}
|
75
116
|
};
|
76
117
|
|
77
|
-
|
78
|
-
|
118
|
+
const coValueSchema = enrichCoListSchema(schema, coValueClass as any);
|
119
|
+
|
120
|
+
coSchemasForZodSchemas.set(schema, coValueSchema);
|
121
|
+
return coValueSchema as unknown as CoValueSchemaFromZodSchema<S>;
|
79
122
|
} else if (isZodCustom(schema)) {
|
80
123
|
if ("builtin" in schema) {
|
81
124
|
if (schema.builtin === "CoFeed" && "element" in schema) {
|
82
|
-
|
83
|
-
|
125
|
+
const coValueClass = CoFeed.Of(
|
126
|
+
schemaFieldToCoFieldDef(
|
84
127
|
zodSchemaToCoSchemaOrKeepPrimitive(
|
85
128
|
schema.element as z.core.$ZodType,
|
86
129
|
),
|
87
130
|
),
|
88
|
-
)
|
131
|
+
);
|
132
|
+
const coValueSchema = enrichCoFeedSchema(schema, coValueClass as any);
|
133
|
+
return coValueSchema as unknown as CoValueSchemaFromZodSchema<S>;
|
89
134
|
} else if (schema.builtin === "FileStream") {
|
90
|
-
|
135
|
+
const coValueClass = FileStream;
|
136
|
+
const coValueSchema = enrichFileStreamSchema(schema, coValueClass);
|
137
|
+
return coValueSchema as unknown as CoValueSchemaFromZodSchema<S>;
|
91
138
|
} else if (schema.builtin === "CoPlainText") {
|
92
|
-
|
139
|
+
const coValueClass = CoPlainText;
|
140
|
+
const coValueSchema = enrichPlainTextSchema(schema, coValueClass);
|
141
|
+
return coValueSchema as unknown as CoValueSchemaFromZodSchema<S>;
|
93
142
|
} else if (schema.builtin === "CoRichText") {
|
94
|
-
|
143
|
+
const coValueClass = CoRichText;
|
144
|
+
const coValueSchema = enrichRichTextSchema(schema, coValueClass);
|
145
|
+
return coValueSchema as unknown as CoValueSchemaFromZodSchema<S>;
|
95
146
|
} else {
|
96
147
|
throw new Error(`Unsupported builtin type: ${schema.builtin}`);
|
97
148
|
}
|
@@ -105,9 +156,12 @@ export function tryZodSchemaToCoSchema<S extends z.core.$ZodType>(
|
|
105
156
|
}
|
106
157
|
} else if (schema instanceof z.core.$ZodDiscriminatedUnion) {
|
107
158
|
if (isUnionOfCoMapsDeeply(schema)) {
|
108
|
-
|
109
|
-
|
110
|
-
|
159
|
+
const coValueClass = SchemaUnion.Of(schemaUnionDiscriminatorFor(schema));
|
160
|
+
const coValueSchema = enrichCoDiscriminatedUnionSchema(
|
161
|
+
schema as any,
|
162
|
+
coValueClass as any,
|
163
|
+
);
|
164
|
+
return coValueSchema as unknown as CoValueSchemaFromZodSchema<S>;
|
111
165
|
} else {
|
112
166
|
throw new Error(
|
113
167
|
"z.discriminatedUnion() of non-collaborative types is not supported as a top-level schema",
|
@@ -118,19 +172,9 @@ export function tryZodSchemaToCoSchema<S extends z.core.$ZodType>(
|
|
118
172
|
}
|
119
173
|
}
|
120
174
|
|
121
|
-
export function zodSchemaToCoSchema<
|
122
|
-
S
|
123
|
-
|
124
|
-
| (z.core.$ZodObject<any, any> & {
|
125
|
-
builtin: "Account";
|
126
|
-
migration?: (account: any, creationProps?: { name: string }) => void;
|
127
|
-
})
|
128
|
-
| (z.core.$ZodCustom<any, any> & { builtin: "FileStream" })
|
129
|
-
| (z.core.$ZodCustom<any, any> & {
|
130
|
-
builtin: "CoFeed";
|
131
|
-
element: z.core.$ZodType;
|
132
|
-
}),
|
133
|
-
>(schema: S): CoValueClassFromZodSchema<S> {
|
175
|
+
export function zodSchemaToCoSchema<S extends z.core.$ZodType | AnyCoSchema>(
|
176
|
+
schema: S,
|
177
|
+
): CoValueSchemaFromZodSchema<S> {
|
134
178
|
const coSchema = tryZodSchemaToCoSchema(schema);
|
135
179
|
if (!coSchema) {
|
136
180
|
throw new Error(
|
@@ -140,38 +184,24 @@ export function zodSchemaToCoSchema<
|
|
140
184
|
return coSchema;
|
141
185
|
}
|
142
186
|
|
143
|
-
|
144
|
-
|
145
|
-
| CoValueClass
|
146
|
-
| z.core.$ZodType
|
147
|
-
| (z.core.$ZodObject<any, any> & {
|
148
|
-
builtin: "Account";
|
149
|
-
migration?: (account: any, creationProps?: { name: string }) => void;
|
150
|
-
})
|
151
|
-
| (z.core.$ZodCustom<any, any> & { builtin: "FileStream" })
|
152
|
-
| (z.core.$ZodCustom<any, any> & {
|
153
|
-
builtin: "CoFeed";
|
154
|
-
element: z.core.$ZodType;
|
155
|
-
}),
|
156
|
-
>(
|
187
|
+
// TODO this should be coValueClassOrAnySchemaToCoValueClass
|
188
|
+
export function anySchemaToCoSchema<S extends CoValueOrZodSchema>(
|
157
189
|
schema: S,
|
158
|
-
): S
|
159
|
-
? S
|
160
|
-
: S extends z.core.$ZodType
|
161
|
-
? CoValueClassFromZodSchema<S>
|
162
|
-
: never {
|
190
|
+
): CoValueClassFromAnySchema<S> {
|
163
191
|
if (isCoValueClass(schema)) {
|
164
192
|
return schema as any;
|
165
|
-
} else if (
|
166
|
-
return
|
193
|
+
} else if (isCoValueSchema(schema)) {
|
194
|
+
return schema.getCoValueClass() as any;
|
167
195
|
} else if ("def" in schema) {
|
168
|
-
const coSchema = tryZodSchemaToCoSchema(
|
196
|
+
const coSchema = tryZodSchemaToCoSchema(
|
197
|
+
schema as z.core.$ZodType | AnyCoSchema,
|
198
|
+
);
|
169
199
|
if (!coSchema) {
|
170
200
|
throw new Error(
|
171
201
|
`Unsupported zod type: ${(schema.def as any)?.type || JSON.stringify(schema)}`,
|
172
202
|
);
|
173
203
|
}
|
174
|
-
return coSchema as any;
|
204
|
+
return coSchema.getCoValueClass() as any;
|
175
205
|
}
|
176
206
|
|
177
207
|
throw new Error(`Unsupported schema: ${JSON.stringify(schema)}`);
|
@@ -179,7 +209,7 @@ export function anySchemaToCoSchema<
|
|
179
209
|
|
180
210
|
export function zodSchemaToCoSchemaOrKeepPrimitive<S extends z.core.$ZodType>(
|
181
211
|
schema: S,
|
182
|
-
):
|
212
|
+
): CoValueSchemaFromZodSchema<S> | ZodPrimitiveSchema {
|
183
213
|
const coSchema = tryZodSchemaToCoSchema(schema);
|
184
214
|
if (!coSchema) {
|
185
215
|
return schema as any;
|
@@ -1,5 +1,10 @@
|
|
1
1
|
import { CryptoProvider } from "cojson";
|
2
|
-
import {
|
2
|
+
import {
|
3
|
+
Account,
|
4
|
+
AccountCreationProps,
|
5
|
+
Group,
|
6
|
+
RefsToResolveStrict,
|
7
|
+
} from "../../../internal.js";
|
3
8
|
import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
|
4
9
|
import { InstanceOrPrimitiveOfSchema } from "../typeConverters/InstanceOrPrimitiveOfSchema.js";
|
5
10
|
import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "../typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
|
@@ -57,9 +62,63 @@ export type AccountSchema<
|
|
57
62
|
) => void,
|
58
63
|
): AccountSchema<Shape>;
|
59
64
|
|
60
|
-
|
65
|
+
getCoValueClass: () => typeof Account;
|
61
66
|
};
|
62
67
|
|
68
|
+
export function enrichAccountSchema<Shape extends BaseAccountShape>(
|
69
|
+
schema: AnyAccountSchema<Shape>,
|
70
|
+
coValueClass: typeof Account,
|
71
|
+
): AccountSchema<Shape> {
|
72
|
+
const enrichedSchema = Object.assign(schema, {
|
73
|
+
create: (...args: any[]) => {
|
74
|
+
// @ts-expect-error
|
75
|
+
return coValueClass.create(...args);
|
76
|
+
},
|
77
|
+
createAs: (...args: any[]) => {
|
78
|
+
// @ts-expect-error
|
79
|
+
return coValueClass.createAs(...args);
|
80
|
+
},
|
81
|
+
getMe: (...args: any[]) => {
|
82
|
+
// @ts-expect-error
|
83
|
+
return coValueClass.getMe(...args);
|
84
|
+
},
|
85
|
+
load: (...args: any[]) => {
|
86
|
+
// @ts-expect-error
|
87
|
+
return coValueClass.load(...args);
|
88
|
+
},
|
89
|
+
subscribe: (...args: any[]) => {
|
90
|
+
// @ts-expect-error
|
91
|
+
return coValueClass.subscribe(...args);
|
92
|
+
},
|
93
|
+
withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
|
94
|
+
return Object.assign(schema, helpers(schema));
|
95
|
+
},
|
96
|
+
fromRaw: (...args: any[]) => {
|
97
|
+
// @ts-expect-error
|
98
|
+
return coValueClass.fromRaw(...args);
|
99
|
+
},
|
100
|
+
withMigration: (
|
101
|
+
migration: (
|
102
|
+
value: any,
|
103
|
+
creationProps?: AccountCreationProps,
|
104
|
+
) => void | Promise<void>,
|
105
|
+
) => {
|
106
|
+
(coValueClass.prototype as Account).migrate = async function (
|
107
|
+
this,
|
108
|
+
creationProps,
|
109
|
+
) {
|
110
|
+
await migration(this, creationProps);
|
111
|
+
};
|
112
|
+
|
113
|
+
return enrichedSchema;
|
114
|
+
},
|
115
|
+
getCoValueClass: () => {
|
116
|
+
return coValueClass;
|
117
|
+
},
|
118
|
+
}) as unknown as AccountSchema<Shape>;
|
119
|
+
return enrichedSchema;
|
120
|
+
}
|
121
|
+
|
63
122
|
export type DefaultProfileShape = {
|
64
123
|
name: z.core.$ZodString<string>;
|
65
124
|
inbox: z.core.$ZodOptional<z.core.$ZodString>;
|
@@ -71,7 +130,7 @@ export type CoProfileSchema<
|
|
71
130
|
Config extends z.core.$ZodObjectConfig = z.core.$ZodObjectConfig,
|
72
131
|
> = CoMapSchema<Shape & DefaultProfileShape, Config, Group>;
|
73
132
|
|
74
|
-
// less precise
|
133
|
+
// less precise version to avoid circularity issues and allow matching against
|
75
134
|
export type AnyAccountSchema<
|
76
135
|
Shape extends z.core.$ZodLooseShape = z.core.$ZodLooseShape,
|
77
136
|
> = z.core.$ZodObject<Shape> & {
|
@@ -0,0 +1,90 @@
|
|
1
|
+
import {
|
2
|
+
Account,
|
3
|
+
AnonymousJazzAgent,
|
4
|
+
AnyCoSchema,
|
5
|
+
InstanceOrPrimitiveOfSchemaCoValuesNullable,
|
6
|
+
RefsToResolve,
|
7
|
+
RefsToResolveStrict,
|
8
|
+
Resolved,
|
9
|
+
SchemaUnion,
|
10
|
+
SubscribeListenerOptions,
|
11
|
+
} from "../../../internal.js";
|
12
|
+
import { z } from "../zodReExport.js";
|
13
|
+
|
14
|
+
export type AnyDiscriminableCoSchema = AnyCoSchema &
|
15
|
+
z.core.$ZodTypeDiscriminable;
|
16
|
+
|
17
|
+
export type AnyCoDiscriminatedUnionSchema<
|
18
|
+
Types extends readonly [
|
19
|
+
AnyDiscriminableCoSchema,
|
20
|
+
...AnyDiscriminableCoSchema[],
|
21
|
+
],
|
22
|
+
> = z.ZodDiscriminatedUnion<Types> & {
|
23
|
+
collaborative: true;
|
24
|
+
};
|
25
|
+
|
26
|
+
export type CoDiscriminatedUnionSchema<
|
27
|
+
Types extends readonly [
|
28
|
+
AnyDiscriminableCoSchema,
|
29
|
+
...AnyDiscriminableCoSchema[],
|
30
|
+
],
|
31
|
+
> = AnyCoDiscriminatedUnionSchema<Types> & {
|
32
|
+
load(
|
33
|
+
id: string,
|
34
|
+
options?: {
|
35
|
+
loadAs?: Account | AnonymousJazzAgent;
|
36
|
+
skipRetry?: boolean;
|
37
|
+
},
|
38
|
+
): Promise<Resolved<
|
39
|
+
CoDiscriminatedUnionInstanceCoValuesNullable<Types> & SchemaUnion,
|
40
|
+
true
|
41
|
+
> | null>;
|
42
|
+
|
43
|
+
subscribe(
|
44
|
+
id: string,
|
45
|
+
options: SubscribeListenerOptions<
|
46
|
+
CoDiscriminatedUnionInstanceCoValuesNullable<Types> & SchemaUnion,
|
47
|
+
true
|
48
|
+
>,
|
49
|
+
listener: (
|
50
|
+
value: Resolved<
|
51
|
+
CoDiscriminatedUnionInstanceCoValuesNullable<Types> & SchemaUnion,
|
52
|
+
true
|
53
|
+
>,
|
54
|
+
unsubscribe: () => void,
|
55
|
+
) => void,
|
56
|
+
): () => void;
|
57
|
+
|
58
|
+
getCoValueClass: () => typeof SchemaUnion;
|
59
|
+
};
|
60
|
+
|
61
|
+
export function enrichCoDiscriminatedUnionSchema<
|
62
|
+
Types extends readonly [
|
63
|
+
AnyDiscriminableCoSchema,
|
64
|
+
...AnyDiscriminableCoSchema[],
|
65
|
+
],
|
66
|
+
>(
|
67
|
+
schema: z.ZodDiscriminatedUnion<Types>,
|
68
|
+
coValueClass: typeof SchemaUnion,
|
69
|
+
): CoDiscriminatedUnionSchema<Types> {
|
70
|
+
return Object.assign(schema, {
|
71
|
+
load: (...args: [any, ...any]) => {
|
72
|
+
// @ts-expect-error
|
73
|
+
return coValueClass.load(...args);
|
74
|
+
},
|
75
|
+
subscribe: (...args: [any, ...any[]]) => {
|
76
|
+
// @ts-expect-error
|
77
|
+
return coValueClass.subscribe(...args);
|
78
|
+
},
|
79
|
+
getCoValueClass: () => {
|
80
|
+
return coValueClass;
|
81
|
+
},
|
82
|
+
}) as unknown as CoDiscriminatedUnionSchema<Types>;
|
83
|
+
}
|
84
|
+
|
85
|
+
type CoDiscriminatedUnionInstanceCoValuesNullable<
|
86
|
+
Types extends readonly [
|
87
|
+
AnyDiscriminableCoSchema,
|
88
|
+
...AnyDiscriminableCoSchema[],
|
89
|
+
],
|
90
|
+
> = NonNullable<InstanceOrPrimitiveOfSchemaCoValuesNullable<Types[number]>>;
|
@@ -58,10 +58,35 @@ export type CoFeedSchema<T extends z.core.$ZodType> = z.core.$ZodCustom<
|
|
58
58
|
) => void,
|
59
59
|
): () => void;
|
60
60
|
|
61
|
-
|
61
|
+
getCoValueClass: () => typeof CoFeed;
|
62
62
|
};
|
63
63
|
|
64
|
-
|
64
|
+
export function enrichCoFeedSchema<T extends z.core.$ZodType>(
|
65
|
+
schema: AnyCoFeedSchema<T>,
|
66
|
+
coValueClass: typeof CoFeed,
|
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>;
|
87
|
+
}
|
88
|
+
|
89
|
+
// less precise version to avoid circularity issues and allow matching against
|
65
90
|
export type AnyCoFeedSchema<T extends z.core.$ZodType = z.core.$ZodType> =
|
66
91
|
z.core.$ZodCustom<any, unknown> & {
|
67
92
|
collaborative: true;
|