jazz-tools 0.15.10 → 0.15.12

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.
Files changed (110) hide show
  1. package/.turbo/turbo-build.log +37 -37
  2. package/CHANGELOG.md +20 -0
  3. package/dist/{chunk-V54IPL5M.js → chunk-DZFT4EZM.js} +426 -303
  4. package/dist/chunk-DZFT4EZM.js.map +1 -0
  5. package/dist/index.js +1 -1
  6. package/dist/react/index.js +1 -3
  7. package/dist/react/testing.js +1 -3
  8. package/dist/react/testing.js.map +1 -1
  9. package/dist/testing.js +1 -1
  10. package/dist/tools/coValues/CoValueBase.d.ts.map +1 -1
  11. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  12. package/dist/tools/coValues/inbox.d.ts.map +1 -1
  13. package/dist/tools/coValues/schemaUnion.d.ts +27 -1
  14. package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
  15. package/dist/tools/implementation/ContextManager.d.ts.map +1 -1
  16. package/dist/tools/implementation/zodSchema/coExport.d.ts +1 -1
  17. package/dist/tools/implementation/zodSchema/coExport.d.ts.map +1 -1
  18. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +6 -3
  19. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +1 -1
  20. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +6 -25
  21. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +1 -1
  22. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +2 -1
  23. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
  24. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +30 -0
  25. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -0
  26. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +2 -1
  27. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  28. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +3 -3
  29. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  30. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +3 -3
  31. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  32. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts +11 -0
  33. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts.map +1 -0
  34. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +2 -3
  35. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  36. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +5 -2
  37. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  38. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +5 -2
  39. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
  40. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +5 -2
  41. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
  42. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +7 -12
  43. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
  44. package/dist/tools/implementation/zodSchema/unionUtils.d.ts +2 -3
  45. package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
  46. package/dist/tools/implementation/zodSchema/zodCo.d.ts +6 -6
  47. package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
  48. package/dist/tools/implementation/zodSchema/zodReExport.d.ts +8 -1
  49. package/dist/tools/implementation/zodSchema/zodReExport.d.ts.map +1 -1
  50. package/dist/tools/implementation/zodSchema/zodSchema.d.ts +18 -13
  51. package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
  52. package/dist/tools/internal.d.ts +1 -0
  53. package/dist/tools/internal.d.ts.map +1 -1
  54. package/dist/tools/subscribe/CoValueCoreSubscription.d.ts.map +1 -1
  55. package/dist/tools/tests/coDiscriminatedUnion.test-d.d.ts +2 -0
  56. package/dist/tools/tests/coDiscriminatedUnion.test-d.d.ts.map +1 -0
  57. package/dist/tools/tests/coDiscriminatedUnion.test.d.ts +2 -0
  58. package/dist/tools/tests/coDiscriminatedUnion.test.d.ts.map +1 -0
  59. package/dist/tools/tests/coOptional.test.d.ts +2 -0
  60. package/dist/tools/tests/coOptional.test.d.ts.map +1 -0
  61. package/dist/tools/tests/testStorage.d.ts +5 -0
  62. package/dist/tools/tests/testStorage.d.ts.map +1 -0
  63. package/dist/tools/tests/zod.test-d.d.ts +2 -0
  64. package/dist/tools/tests/zod.test-d.d.ts.map +1 -0
  65. package/package.json +5 -4
  66. package/src/react-core/tests/useAccount.test.ts +1 -11
  67. package/src/react-core/tests/useCoState.test.ts +20 -0
  68. package/src/tools/coValues/CoValueBase.ts +2 -2
  69. package/src/tools/coValues/inbox.ts +0 -2
  70. package/src/tools/coValues/schemaUnion.ts +62 -0
  71. package/src/tools/implementation/ContextManager.ts +5 -1
  72. package/src/tools/implementation/zodSchema/coExport.ts +2 -0
  73. package/src/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +29 -12
  74. package/src/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +93 -63
  75. package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +62 -3
  76. package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +90 -0
  77. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +27 -2
  78. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +27 -5
  79. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +60 -5
  80. package/src/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.ts +33 -0
  81. package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +3 -5
  82. package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +32 -2
  83. package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +30 -2
  84. package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +26 -2
  85. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +10 -13
  86. package/src/tools/implementation/zodSchema/unionUtils.ts +22 -14
  87. package/src/tools/implementation/zodSchema/zodCo.ts +68 -256
  88. package/src/tools/implementation/zodSchema/zodReExport.ts +34 -5
  89. package/src/tools/implementation/zodSchema/zodSchema.ts +78 -19
  90. package/src/tools/internal.ts +1 -0
  91. package/src/tools/subscribe/CoValueCoreSubscription.ts +4 -0
  92. package/src/tools/tests/ContextManager.test.ts +84 -10
  93. package/src/tools/tests/coDiscriminatedUnion.test-d.ts +57 -0
  94. package/src/tools/tests/coDiscriminatedUnion.test.ts +93 -0
  95. package/src/tools/tests/coList.test.ts +4 -12
  96. package/src/tools/tests/coMap.record.test.ts +4 -4
  97. package/src/tools/tests/coMap.test-d.ts +3 -3
  98. package/src/tools/tests/coMap.test.ts +10 -10
  99. package/src/tools/tests/coOptional.test.ts +39 -0
  100. package/src/tools/tests/createContext.test.ts +5 -4
  101. package/src/tools/tests/deepLoading.test.ts +3 -3
  102. package/src/tools/tests/inbox.test.ts +3 -11
  103. package/src/tools/tests/load.test.ts +9 -0
  104. package/src/tools/tests/patterns/requestToJoin.test.ts +1 -1
  105. package/src/tools/tests/schemaUnion.test.ts +11 -22
  106. package/src/tools/tests/subscribe.test.ts +25 -39
  107. package/src/tools/tests/testStorage.ts +70 -0
  108. package/src/tools/tests/zod.test-d.ts +70 -0
  109. package/src/tools/tests/zod.test.ts +64 -5
  110. package/dist/chunk-V54IPL5M.js.map +0 -1
@@ -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
- CoValueClassFromZodSchema,
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 { zodFieldToCoFieldDef } from "./zodFieldToCoFieldDef.js";
40
+ import { schemaFieldToCoFieldDef } from "./zodFieldToCoFieldDef.js";
29
41
 
30
- let coSchemasForZodSchemas = new Map<z.core.$ZodType, CoValueClass>();
42
+ let coSchemasForZodSchemas = new Map<z.core.$ZodType, AnyCoSchema>();
31
43
 
32
- export function tryZodSchemaToCoSchema<S extends z.core.$ZodType>(
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
- ): CoValueClassFromZodSchema<S> | null {
35
- if ("collaborative" in schema && schema.collaborative) {
65
+ ): CoValueSchemaFromZodSchema<S> | null {
66
+ if (isAnyCoValueSchema(schema)) {
36
67
  if (coSchemasForZodSchemas.has(schema)) {
37
- return coSchemasForZodSchemas.get(schema) as CoValueClassFromZodSchema<S>;
68
+ return coSchemasForZodSchemas.get(
69
+ schema,
70
+ ) as CoValueSchemaFromZodSchema<S>;
38
71
  }
39
72
 
40
- if (isZodObject(schema)) {
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 coSchema = class ZCoMap extends ClassToExtend {
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] = zodFieldToCoFieldDef(
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] = zodFieldToCoFieldDef(
93
+ (this as any)[coField.items] = schemaFieldToCoFieldDef(
58
94
  zodSchemaToCoSchemaOrKeepPrimitive(def.catchall),
59
95
  );
60
96
  }
61
97
  }
62
98
  };
63
99
 
64
- coSchemasForZodSchemas.set(schema, coSchema as unknown as CoValueClass);
65
- return coSchema as unknown as CoValueClassFromZodSchema<S>;
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 coSchema = class ZCoList extends CoList {
109
+ const coValueClass = class ZCoList extends CoList {
69
110
  constructor(options: { fromRaw: RawCoList } | undefined) {
70
111
  super(options);
71
- (this as any)[coField.items] = zodFieldToCoFieldDef(
112
+ (this as any)[coField.items] = schemaFieldToCoFieldDef(
72
113
  zodSchemaToCoSchemaOrKeepPrimitive(def.element),
73
114
  );
74
115
  }
75
116
  };
76
117
 
77
- coSchemasForZodSchemas.set(schema, coSchema);
78
- return coSchema as unknown as CoValueClassFromZodSchema<S>;
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
- return CoFeed.Of(
83
- zodFieldToCoFieldDef(
125
+ const coValueClass = CoFeed.Of(
126
+ schemaFieldToCoFieldDef(
84
127
  zodSchemaToCoSchemaOrKeepPrimitive(
85
128
  schema.element as z.core.$ZodType,
86
129
  ),
87
130
  ),
88
- ) as unknown as CoValueClassFromZodSchema<S>;
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
- return FileStream as unknown as CoValueClassFromZodSchema<S>;
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
- return CoPlainText as unknown as CoValueClassFromZodSchema<S>;
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
- return CoRichText as unknown as CoValueClassFromZodSchema<S>;
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
- return SchemaUnion.Of(
109
- schemaUnionDiscriminatorFor(schema),
110
- ) as unknown as CoValueClassFromZodSchema<S>;
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 extends
123
- | z.core.$ZodType
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
- export function anySchemaToCoSchema<
144
- S extends
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 extends CoValueClass
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 ("getCoSchema" in schema) {
166
- return (schema as any).getCoSchema() as any;
193
+ } else if (isCoValueSchema(schema)) {
194
+ return schema.getCoValueClass() as any;
167
195
  } else if ("def" in schema) {
168
- const coSchema = tryZodSchemaToCoSchema(schema as z.core.$ZodType);
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
- ): CoValueClassFromZodSchema<S> | ZodPrimitiveSchema {
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 { Account, Group, RefsToResolveStrict } from "../../../internal.js";
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
- getCoSchema: () => typeof Account;
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 verion to avoid circularity issues and allow matching against
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
- getCoSchema: () => typeof CoFeed;
61
+ getCoValueClass: () => typeof CoFeed;
62
62
  };
63
63
 
64
- // less precise verion to avoid circularity issues and allow matching against
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;
@@ -19,9 +19,7 @@ type CoListInit<T extends z.core.$ZodType> = Array<
19
19
  : NonNullable<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>>
20
20
  >;
21
21
 
22
- export type CoListSchema<T extends z.core.$ZodType> = z.core.$ZodArray<T> & {
23
- collaborative: true;
24
-
22
+ export type CoListSchema<T extends z.core.$ZodType> = AnyCoListSchema<T> & {
25
23
  create: (
26
24
  items: CoListInit<T>,
27
25
  options?: { owner: Account | Group } | Account | Group,
@@ -52,10 +50,34 @@ export type CoListSchema<T extends z.core.$ZodType> = z.core.$ZodArray<T> & {
52
50
  helpers: (Self: S) => T,
53
51
  ): WithHelpers<S, T>;
54
52
 
55
- getCoSchema: () => typeof CoList;
53
+ getCoValueClass: () => typeof CoList;
56
54
  };
57
55
 
58
- // less precise verion to avoid circularity issues and allow matching against
56
+ export function enrichCoListSchema<T extends z.core.$ZodType>(
57
+ schema: AnyCoListSchema<T>,
58
+ coValueClass: typeof CoList,
59
+ ): CoListSchema<T> {
60
+ return Object.assign(schema, {
61
+ create: (...args: [any, ...any[]]) => {
62
+ return coValueClass.create(...args);
63
+ },
64
+ load: (...args: [any, ...any[]]) => {
65
+ return coValueClass.load(...args);
66
+ },
67
+ subscribe: (...args: [any, ...any[]]) => {
68
+ // @ts-expect-error
69
+ return coValueClass.subscribe(...args);
70
+ },
71
+ withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
72
+ return Object.assign(schema, helpers(schema));
73
+ },
74
+ getCoValueClass: () => {
75
+ return coValueClass;
76
+ },
77
+ }) as unknown as CoListSchema<T>;
78
+ }
79
+
80
+ // less precise version to avoid circularity issues and allow matching against
59
81
  export type AnyCoListSchema<T extends z.core.$ZodType = z.core.$ZodType> =
60
82
  z.core.$ZodArray<T> & { collaborative: true };
61
83
 
@@ -8,6 +8,7 @@ import {
8
8
  Resolved,
9
9
  Simplify,
10
10
  SubscribeListenerOptions,
11
+ zodSchemaToCoSchema,
11
12
  } from "../../../internal.js";
12
13
  import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
13
14
  import { InstanceOrPrimitiveOfSchema } from "../typeConverters/InstanceOrPrimitiveOfSchema.js";
@@ -19,10 +20,8 @@ export type CoMapSchema<
19
20
  Shape extends z.core.$ZodLooseShape,
20
21
  Config extends z.core.$ZodObjectConfig = z.core.$ZodObjectConfig,
21
22
  Owner extends Account | Group = Account | Group,
22
- > = z.core.$ZodObject<Shape, Config> &
23
+ > = AnyCoMapSchema<Shape, Config> &
23
24
  z.$ZodTypeDiscriminable & {
24
- collaborative: true;
25
-
26
25
  create: (
27
26
  init: Simplify<CoMapInitZod<Shape>>,
28
27
  options?:
@@ -145,9 +144,65 @@ export type CoMapSchema<
145
144
  ) => undefined,
146
145
  ): CoMapSchema<Shape, Config, Owner>;
147
146
 
148
- getCoSchema: () => typeof CoMap;
147
+ getCoValueClass: () => typeof CoMap;
149
148
  };
150
149
 
150
+ export function enrichCoMapSchema<
151
+ Shape extends z.core.$ZodLooseShape,
152
+ Config extends z.core.$ZodObjectConfig,
153
+ >(
154
+ schema: AnyCoMapSchema<Shape, Config>,
155
+ coValueClass: typeof CoMap,
156
+ ): CoMapSchema<Shape, Config> {
157
+ // @ts-expect-error schema is actually a z.ZodObject, but we need to use z.core.$ZodObject to avoid circularity issues
158
+ const baseCatchall = schema.catchall;
159
+ const coValueSchema = Object.assign(schema, {
160
+ create: (...args: [any, ...any[]]) => {
161
+ return coValueClass.create(...args);
162
+ },
163
+ load: (...args: [any, ...any[]]) => {
164
+ return coValueClass.load(...args);
165
+ },
166
+ subscribe: (...args: [any, ...any[]]) => {
167
+ // @ts-expect-error
168
+ return coValueClass.subscribe(...args);
169
+ },
170
+ findUnique: (...args: [any, ...any[]]) => {
171
+ // @ts-expect-error
172
+ return coValueClass.findUnique(...args);
173
+ },
174
+ upsertUnique: (...args: [any, ...any[]]) => {
175
+ // @ts-expect-error
176
+ return coValueClass.upsertUnique(...args);
177
+ },
178
+ loadUnique: (...args: [any, ...any[]]) => {
179
+ // @ts-expect-error
180
+ return coValueClass.loadUnique(...args);
181
+ },
182
+ catchall: (index: z.core.$ZodType) => {
183
+ const newSchema = baseCatchall(index);
184
+ // TODO avoid repeating this with coMapDefiner
185
+ const enrichedSchema = Object.assign(newSchema, {
186
+ collaborative: true,
187
+ }) as AnyCoMapSchema<Shape, Config>;
188
+ return zodSchemaToCoSchema(enrichedSchema);
189
+ },
190
+ withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
191
+ return Object.assign(schema, helpers(schema));
192
+ },
193
+ withMigration: (migration: (value: any) => undefined) => {
194
+ // @ts-expect-error TODO check
195
+ coValueClass.prototype.migrate = migration;
196
+
197
+ return coValueSchema;
198
+ },
199
+ getCoValueClass: () => {
200
+ return coValueClass;
201
+ },
202
+ }) as unknown as CoMapSchema<Shape, Config>;
203
+ return coValueSchema;
204
+ }
205
+
151
206
  export type optionalKeys<Shape extends z.core.$ZodLooseShape> = {
152
207
  [key in keyof Shape]: Shape[key] extends z.core.$ZodOptional<any>
153
208
  ? key
@@ -170,7 +225,7 @@ export type CoMapInitZod<Shape extends z.core.$ZodLooseShape> = {
170
225
  >;
171
226
  } & { [key in keyof Shape]?: unknown };
172
227
 
173
- // less precise verion to avoid circularity issues and allow matching against
228
+ // less precise version to avoid circularity issues and allow matching against
174
229
  export type AnyCoMapSchema<
175
230
  Shape extends z.core.$ZodLooseShape = z.core.$ZodLooseShape,
176
231
  Config extends z.core.$ZodObjectConfig = z.core.$ZodObjectConfig,
@@ -0,0 +1,33 @@
1
+ import { isAnyCoValueSchema } from "../runtimeConverters/zodSchemaToCoSchema.js";
2
+ import { z } from "../zodReExport.js";
3
+ import { AnyCoSchema, CoValueSchemaFromZodSchema } from "../zodSchema.js";
4
+
5
+ export type AnyCoOptionalSchema<
6
+ Shape extends z.core.$ZodType = z.core.$ZodType,
7
+ > = z.ZodOptional<Shape> & {
8
+ collaborative: true;
9
+ };
10
+
11
+ export type CoOptionalSchema<Shape extends z.core.$ZodType = z.core.$ZodType> =
12
+ AnyCoOptionalSchema<Shape> & {
13
+ getCoValueClass: () => CoValueSchemaFromZodSchema<AnyCoSchema>["getCoValueClass"];
14
+ };
15
+
16
+ export function createCoOptionalSchema<T extends AnyCoSchema>(
17
+ schema: T,
18
+ ): CoOptionalSchema<T> {
19
+ return Object.assign(z.optional(schema), {
20
+ collaborative: true,
21
+ getCoValueClass: () => {
22
+ return (
23
+ schema as CoValueSchemaFromZodSchema<AnyCoSchema>
24
+ ).getCoValueClass();
25
+ },
26
+ }) as unknown as CoOptionalSchema<T>;
27
+ }
28
+
29
+ export function isAnyCoOptionalSchema(
30
+ schema: z.core.$ZodType,
31
+ ): schema is CoOptionalSchema<z.core.$ZodType> {
32
+ return isAnyCoValueSchema(schema) && schema._zod.def.type === "optional";
33
+ }
@@ -28,9 +28,7 @@ type CoRecordInit<
28
28
  export type CoRecordSchema<
29
29
  K extends z.core.$ZodString<string>,
30
30
  V extends z.core.$ZodType,
31
- > = z.core.$ZodRecord<K, V> & {
32
- collaborative: true;
33
-
31
+ > = AnyCoRecordSchema<K, V> & {
34
32
  create: (
35
33
  init: Simplify<CoRecordInit<K, V>>,
36
34
  options?:
@@ -83,10 +81,10 @@ export type CoRecordSchema<
83
81
  this: S,
84
82
  helpers: (Self: S) => T,
85
83
  ): WithHelpers<S, T>;
86
- getCoSchema: () => typeof CoMap;
84
+ getCoValueClass: () => typeof CoMap;
87
85
  };
88
86
 
89
- // less precise verion to avoid circularity issues and allow matching against
87
+ // less precise version to avoid circularity issues and allow matching against
90
88
  export type AnyCoRecordSchema<
91
89
  K extends z.core.$ZodString<string> = z.core.$ZodString<string>,
92
90
  V extends z.core.$ZodType = z.core.$ZodType,