jazz-tools 0.13.31 → 0.14.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.
Files changed (166) hide show
  1. package/.turbo/turbo-build.log +8 -8
  2. package/CHANGELOG.md +14 -3
  3. package/dist/auth/DemoAuth.d.ts.map +1 -1
  4. package/dist/auth/PassphraseAuth.d.ts +1 -3
  5. package/dist/auth/PassphraseAuth.d.ts.map +1 -1
  6. package/dist/{chunk-IJU4XPFS.js → chunk-2ASOGEYA.js} +3535 -3291
  7. package/dist/chunk-2ASOGEYA.js.map +1 -0
  8. package/dist/coValues/CoValueBase.d.ts +22 -0
  9. package/dist/coValues/CoValueBase.d.ts.map +1 -0
  10. package/dist/coValues/account.d.ts +12 -12
  11. package/dist/coValues/account.d.ts.map +1 -1
  12. package/dist/coValues/coFeed.d.ts +24 -25
  13. package/dist/coValues/coFeed.d.ts.map +1 -1
  14. package/dist/coValues/coList.d.ts +10 -13
  15. package/dist/coValues/coList.d.ts.map +1 -1
  16. package/dist/coValues/coMap.d.ts +32 -35
  17. package/dist/coValues/coMap.d.ts.map +1 -1
  18. package/dist/coValues/coPlainText.d.ts.map +1 -1
  19. package/dist/coValues/deepLoading.d.ts +17 -21
  20. package/dist/coValues/deepLoading.d.ts.map +1 -1
  21. package/dist/coValues/extensions/imageDef.d.ts +12 -11
  22. package/dist/coValues/extensions/imageDef.d.ts.map +1 -1
  23. package/dist/coValues/group.d.ts +5 -9
  24. package/dist/coValues/group.d.ts.map +1 -1
  25. package/dist/coValues/inbox.d.ts +2 -3
  26. package/dist/coValues/inbox.d.ts.map +1 -1
  27. package/dist/coValues/interfaces.d.ts +8 -34
  28. package/dist/coValues/interfaces.d.ts.map +1 -1
  29. package/dist/coValues/profile.d.ts +4 -14
  30. package/dist/coValues/profile.d.ts.map +1 -1
  31. package/dist/coValues/registeredSchemas.d.ts +1 -3
  32. package/dist/coValues/registeredSchemas.d.ts.map +1 -1
  33. package/dist/coValues/schemaUnion.d.ts +6 -6
  34. package/dist/exports.d.ts +12 -16
  35. package/dist/exports.d.ts.map +1 -1
  36. package/dist/implementation/ContextManager.d.ts +1 -1
  37. package/dist/implementation/ContextManager.d.ts.map +1 -1
  38. package/dist/implementation/activeAccountContext.d.ts +1 -1
  39. package/dist/implementation/activeAccountContext.d.ts.map +1 -1
  40. package/dist/implementation/createContext.d.ts +10 -10
  41. package/dist/implementation/createContext.d.ts.map +1 -1
  42. package/dist/implementation/invites.d.ts +6 -6
  43. package/dist/implementation/invites.d.ts.map +1 -1
  44. package/dist/implementation/refs.d.ts +2 -2
  45. package/dist/implementation/refs.d.ts.map +1 -1
  46. package/dist/implementation/schema.d.ts +21 -28
  47. package/dist/implementation/schema.d.ts.map +1 -1
  48. package/dist/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +9 -0
  49. package/dist/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +1 -0
  50. package/dist/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +28 -0
  51. package/dist/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +1 -0
  52. package/dist/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +65 -0
  53. package/dist/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -0
  54. package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +28 -0
  55. package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -0
  56. package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +24 -0
  57. package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -0
  58. package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +41 -0
  59. package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -0
  60. package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +35 -0
  61. package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -0
  62. package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +9 -0
  63. package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -0
  64. package/dist/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +20 -0
  65. package/dist/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -0
  66. package/dist/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +18 -0
  67. package/dist/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -0
  68. package/dist/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +24 -0
  69. package/dist/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -0
  70. package/dist/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +21 -0
  71. package/dist/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -0
  72. package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +29 -0
  73. package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -0
  74. package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +29 -0
  75. package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -0
  76. package/dist/implementation/zodSchema/unionUtils.d.ts +6 -0
  77. package/dist/implementation/zodSchema/unionUtils.d.ts.map +1 -0
  78. package/dist/implementation/zodSchema/zodCo.d.ts +35 -0
  79. package/dist/implementation/zodSchema/zodCo.d.ts.map +1 -0
  80. package/dist/implementation/zodSchema/zodSchema.d.ts +38 -0
  81. package/dist/implementation/zodSchema/zodSchema.d.ts.map +1 -0
  82. package/dist/index.js +295 -10
  83. package/dist/index.js.map +1 -1
  84. package/dist/internal.d.ts +34 -0
  85. package/dist/internal.d.ts.map +1 -1
  86. package/dist/subscribe/SubscriptionScope.d.ts +1 -2
  87. package/dist/subscribe/SubscriptionScope.d.ts.map +1 -1
  88. package/dist/subscribe/utils.d.ts +2 -2
  89. package/dist/subscribe/utils.d.ts.map +1 -1
  90. package/dist/testing.d.ts +10 -8
  91. package/dist/testing.d.ts.map +1 -1
  92. package/dist/testing.js +1 -1
  93. package/dist/testing.js.map +1 -1
  94. package/dist/tests/utils.d.ts +6 -2
  95. package/dist/tests/utils.d.ts.map +1 -1
  96. package/dist/types.d.ts +1 -7
  97. package/dist/types.d.ts.map +1 -1
  98. package/package.json +3 -2
  99. package/src/auth/DemoAuth.ts +1 -2
  100. package/src/auth/PassphraseAuth.ts +1 -1
  101. package/src/coValues/CoValueBase.ts +83 -0
  102. package/src/coValues/account.ts +53 -43
  103. package/src/coValues/coFeed.ts +65 -83
  104. package/src/coValues/coList.ts +28 -21
  105. package/src/coValues/coMap.ts +54 -38
  106. package/src/coValues/coPlainText.ts +4 -1
  107. package/src/coValues/deepLoading.ts +35 -43
  108. package/src/coValues/extensions/imageDef.ts +21 -19
  109. package/src/coValues/group.ts +37 -38
  110. package/src/coValues/inbox.ts +24 -11
  111. package/src/coValues/interfaces.ts +29 -93
  112. package/src/coValues/profile.ts +12 -13
  113. package/src/coValues/registeredSchemas.ts +1 -3
  114. package/src/coValues/schemaUnion.ts +6 -6
  115. package/src/exports.ts +47 -25
  116. package/src/implementation/activeAccountContext.ts +1 -1
  117. package/src/implementation/createContext.ts +39 -24
  118. package/src/implementation/invites.ts +15 -12
  119. package/src/implementation/refs.ts +6 -4
  120. package/src/implementation/schema.ts +22 -34
  121. package/src/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +101 -0
  122. package/src/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +191 -0
  123. package/src/implementation/zodSchema/schemaTypes/AccountSchema.ts +102 -0
  124. package/src/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +70 -0
  125. package/src/implementation/zodSchema/schemaTypes/CoListSchema.ts +59 -0
  126. package/src/implementation/zodSchema/schemaTypes/CoMapSchema.ts +126 -0
  127. package/src/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +98 -0
  128. package/src/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +9 -0
  129. package/src/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +27 -0
  130. package/src/implementation/zodSchema/schemaTypes/RichTextSchema.ts +25 -0
  131. package/src/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +61 -0
  132. package/src/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +77 -0
  133. package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +90 -0
  134. package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +103 -0
  135. package/src/implementation/zodSchema/unionUtils.ts +139 -0
  136. package/src/implementation/zodSchema/zodCo.ts +409 -0
  137. package/src/implementation/zodSchema/zodSchema.ts +116 -0
  138. package/src/internal.ts +38 -0
  139. package/src/subscribe/SubscriptionScope.ts +3 -1
  140. package/src/subscribe/utils.ts +7 -2
  141. package/src/testing.ts +14 -16
  142. package/src/tests/ContextManager.test.ts +73 -47
  143. package/src/tests/DemoAuth.test.ts +1 -1
  144. package/src/tests/account.test.ts +6 -9
  145. package/src/tests/coFeed.test.ts +102 -63
  146. package/src/tests/coList.test.ts +82 -95
  147. package/src/tests/coMap.record.test.ts +53 -87
  148. package/src/tests/coMap.test.ts +297 -312
  149. package/src/tests/coPlainText.test.ts +19 -39
  150. package/src/tests/createContext.test.ts +33 -15
  151. package/src/tests/deepLoading.test.ts +196 -179
  152. package/src/tests/groupsAndAccounts.test.ts +81 -72
  153. package/src/tests/imageDef.test.ts +22 -13
  154. package/src/tests/inbox.test.ts +36 -29
  155. package/src/tests/load.test.ts +10 -10
  156. package/src/tests/patterns/requestToJoin.test.ts +31 -31
  157. package/src/tests/schema.test.ts +37 -38
  158. package/src/tests/schemaUnion.test.ts +54 -64
  159. package/src/tests/subscribe.test.ts +118 -116
  160. package/src/tests/testing.test.ts +33 -33
  161. package/src/tests/utils.ts +3 -2
  162. package/src/types.ts +1 -8
  163. package/dist/chunk-IJU4XPFS.js.map +0 -1
  164. package/dist/tests/deepLoading.test-d.d.ts +0 -2
  165. package/dist/tests/deepLoading.test-d.d.ts.map +0 -1
  166. package/src/tests/deepLoading.test-d.ts +0 -393
@@ -0,0 +1,409 @@
1
+ import { CoValueUniqueness } from "cojson";
2
+ import z from "zod/v4";
3
+ import {
4
+ Account,
5
+ AccountCreationProps,
6
+ AccountInstance,
7
+ AccountSchema,
8
+ AnonymousJazzAgent,
9
+ AnyCoMapSchema,
10
+ CoFeed,
11
+ CoFeedSchema,
12
+ CoListSchema,
13
+ CoMapSchema,
14
+ CoPlainText,
15
+ CoProfileSchema,
16
+ CoRecordSchema,
17
+ CoRichText,
18
+ DefaultProfileShape,
19
+ FileStream,
20
+ FileStreamSchema,
21
+ Group,
22
+ ImageDefinition,
23
+ PlainTextSchema,
24
+ Simplify,
25
+ zodSchemaToCoSchema,
26
+ } from "../../internal.js";
27
+ import { RichTextSchema } from "./schemaTypes/RichTextSchema.js";
28
+
29
+ export const coMapDefiner = <Shape extends z.core.$ZodLooseShape>(
30
+ shape: Shape,
31
+ ): CoMapSchema<Shape> => {
32
+ const objectSchema = z.object(shape).meta({
33
+ collaborative: true,
34
+ });
35
+
36
+ type CleanedType = Pick<
37
+ typeof objectSchema,
38
+ "_zod" | "def" | "~standard" | "catchall"
39
+ >;
40
+
41
+ const coMapSchema = objectSchema as unknown as CleanedType & {
42
+ collaborative: true;
43
+ create: CoMapSchema<Shape>["create"];
44
+ load: CoMapSchema<Shape>["load"];
45
+ subscribe: CoMapSchema<Shape>["subscribe"];
46
+ findUnique: CoMapSchema<Shape>["findUnique"];
47
+ catchall: CoMapSchema<Shape>["catchall"];
48
+ withHelpers: CoMapSchema<Shape>["withHelpers"];
49
+ };
50
+
51
+ coMapSchema.collaborative = true;
52
+
53
+ coMapSchema.create = function (this: CoMapSchema<Shape>, ...args: any[]) {
54
+ return (zodSchemaToCoSchema(this) as any).create(...args);
55
+ } as CoMapSchema<Shape>["create"];
56
+
57
+ coMapSchema.load = function (this: CoMapSchema<Shape>, ...args: any[]) {
58
+ return (zodSchemaToCoSchema(this) as any).load(...args);
59
+ } as CoMapSchema<Shape>["load"];
60
+
61
+ coMapSchema.subscribe = function (this: CoMapSchema<Shape>, ...args: any[]) {
62
+ return (zodSchemaToCoSchema(this) as any).subscribe(...args);
63
+ } as CoMapSchema<Shape>["subscribe"];
64
+
65
+ coMapSchema.findUnique = function (
66
+ this: CoMapSchema<Shape>,
67
+ unique: CoValueUniqueness["uniqueness"],
68
+ ownerID: string,
69
+ as?: Account | Group | AnonymousJazzAgent,
70
+ ) {
71
+ return (zodSchemaToCoSchema(this) as any).findUnique(unique, ownerID, as);
72
+ } as CoMapSchema<Shape>["findUnique"];
73
+
74
+ const oldCatchall = coMapSchema.catchall;
75
+ coMapSchema.catchall = function (
76
+ this: CoMapSchema<Shape>,
77
+ schema: z.core.$ZodType,
78
+ ) {
79
+ return { ...this, ...oldCatchall(schema) } as any;
80
+ } as CoMapSchema<Shape>["catchall"] as any;
81
+
82
+ coMapSchema.withHelpers = function (
83
+ this: CoMapSchema<Shape>,
84
+ helpers: (Self: CoMapSchema<Shape>) => object,
85
+ ) {
86
+ return { ...this, ...helpers(this) };
87
+ } as CoMapSchema<Shape>["withHelpers"];
88
+
89
+ return coMapSchema as unknown as CoMapSchema<Shape>;
90
+ };
91
+
92
+ const coAccountDefiner = <
93
+ Shape extends {
94
+ profile: AnyCoMapSchema<{
95
+ name: z.core.$ZodString<string>;
96
+ inbox?: z.core.$ZodOptional<z.core.$ZodString>;
97
+ inboxInvite?: z.core.$ZodOptional<z.core.$ZodString>;
98
+ }>;
99
+ root: AnyCoMapSchema;
100
+ },
101
+ >(
102
+ shape: Shape = {
103
+ profile: co.map({
104
+ name: z.string(),
105
+ inbox: z.optional(z.string()),
106
+ inboxInvite: z.optional(z.string()),
107
+ }),
108
+ root: co.map({}),
109
+ } as unknown as Shape,
110
+ ): AccountSchema<Shape> => {
111
+ const objectSchema = z.object(shape).meta({
112
+ collaborative: true,
113
+ });
114
+
115
+ type CleanedType = Pick<
116
+ typeof objectSchema,
117
+ "_zod" | "def" | "~standard" | "catchall"
118
+ >;
119
+
120
+ const accountSchema = objectSchema as unknown as CleanedType & {
121
+ collaborative: true;
122
+ builtin: "Account";
123
+ migration?: (
124
+ account: AccountInstance<Shape>,
125
+ creationProps?: AccountCreationProps,
126
+ ) => void;
127
+
128
+ create: AccountSchema<Shape>["create"];
129
+ createAs: AccountSchema<Shape>["createAs"];
130
+ getMe: AccountSchema<Shape>["getMe"];
131
+ load: AccountSchema<Shape>["load"];
132
+ subscribe: AccountSchema<Shape>["subscribe"];
133
+ withHelpers: AccountSchema<Shape>["withHelpers"];
134
+ withMigration: AccountSchema<Shape>["withMigration"];
135
+ };
136
+
137
+ accountSchema.collaborative = true;
138
+ accountSchema.builtin = "Account";
139
+
140
+ accountSchema.create = function (this: AccountSchema<Shape>, ...args: any[]) {
141
+ return (zodSchemaToCoSchema(this) as any).create(...args);
142
+ } as AccountSchema<Shape>["create"];
143
+
144
+ accountSchema.createAs = function (
145
+ this: AccountSchema<Shape>,
146
+ ...args: any[]
147
+ ) {
148
+ return (zodSchemaToCoSchema(this) as any).createAs(...args);
149
+ } as AccountSchema<Shape>["createAs"];
150
+
151
+ accountSchema.getMe = function (this: AccountSchema<Shape>) {
152
+ return (zodSchemaToCoSchema(this) as any).getMe();
153
+ } as AccountSchema<Shape>["getMe"];
154
+
155
+ accountSchema.load = function (this: AccountSchema<Shape>, ...args: any[]) {
156
+ return (zodSchemaToCoSchema(this) as any).load(...args);
157
+ } as AccountSchema<Shape>["load"];
158
+
159
+ accountSchema.subscribe = function (
160
+ this: AccountSchema<Shape>,
161
+ ...args: any[]
162
+ ) {
163
+ return (zodSchemaToCoSchema(this) as any).subscribe(...args);
164
+ } as AccountSchema<Shape>["subscribe"];
165
+
166
+ accountSchema.withHelpers = function (
167
+ this: CoMapSchema<Shape>,
168
+ helpers: object,
169
+ ) {
170
+ return { ...this, ...helpers };
171
+ } as CoMapSchema<Shape>["withHelpers"];
172
+
173
+ accountSchema.withMigration = function (
174
+ this: AccountSchema<Shape>,
175
+ migration: (
176
+ account: AccountInstance<Shape>,
177
+ creationProps?: AccountCreationProps,
178
+ ) => void,
179
+ ) {
180
+ return { ...this, migration };
181
+ } as AccountSchema<Shape>["withMigration"];
182
+
183
+ return accountSchema as unknown as AccountSchema<Shape>;
184
+ };
185
+
186
+ const coRecordDefiner = <
187
+ K extends z.core.$ZodString<string>,
188
+ V extends z.core.$ZodType,
189
+ >(
190
+ _keyType: K,
191
+ valueType: V,
192
+ ): CoRecordSchema<K, V> => {
193
+ return coMapDefiner({}).catchall(valueType) as unknown as CoRecordSchema<
194
+ K,
195
+ V
196
+ >;
197
+ };
198
+
199
+ const coListDefiner = <T extends z.core.$ZodType>(
200
+ element: T,
201
+ ): CoListSchema<T> => {
202
+ const arraySchema = z.array(element).meta({
203
+ collaborative: true,
204
+ });
205
+
206
+ type CleanedType = Pick<typeof arraySchema, "_zod" | "def" | "~standard">;
207
+
208
+ const coListSchema = arraySchema as unknown as CleanedType & {
209
+ collaborative: true;
210
+ create: CoListSchema<T>["create"];
211
+ load: CoListSchema<T>["load"];
212
+ subscribe: CoListSchema<T>["subscribe"];
213
+ withHelpers: CoListSchema<T>["withHelpers"];
214
+ };
215
+
216
+ coListSchema.collaborative = true;
217
+
218
+ coListSchema.create = function (this: CoListSchema<T>, ...args: any[]) {
219
+ return (zodSchemaToCoSchema(this) as any).create(...args);
220
+ } as CoListSchema<T>["create"];
221
+
222
+ coListSchema.load = function (this: CoListSchema<T>, ...args: any[]) {
223
+ return (zodSchemaToCoSchema(this) as any).load(...args);
224
+ } as CoListSchema<T>["load"];
225
+
226
+ coListSchema.subscribe = function (this: CoListSchema<T>, ...args: any[]) {
227
+ return (zodSchemaToCoSchema(this) as any).subscribe(...args);
228
+ } as CoListSchema<T>["subscribe"];
229
+
230
+ coListSchema.withHelpers = function (
231
+ this: CoListSchema<T>,
232
+ helpers: (Self: CoListSchema<T>) => object,
233
+ ) {
234
+ return { ...this, ...helpers(this) };
235
+ } as CoListSchema<T>["withHelpers"];
236
+
237
+ return coListSchema;
238
+ };
239
+
240
+ const coProfileDefiner = <
241
+ Shape extends z.core.$ZodLooseShape = Simplify<DefaultProfileShape>,
242
+ >(
243
+ shape: Shape & {
244
+ name?: z.core.$ZodString<string>;
245
+ inbox?: z.core.$ZodOptional<z.core.$ZodString>;
246
+ inboxInvite?: z.core.$ZodOptional<z.core.$ZodString>;
247
+ } = {} as any,
248
+ ): CoProfileSchema<Shape> => {
249
+ const base = coMapDefiner({
250
+ ...(shape ?? {}),
251
+ name: z.string(),
252
+ inbox: z.optional(z.string()),
253
+ inboxInvite: z.optional(z.string()),
254
+ });
255
+ return {
256
+ ...base,
257
+ // enforce that the owner is a group
258
+ create: ((init: any, options: { owner: Group } | Group) => {
259
+ return base.create(init, options);
260
+ }) as CoProfileSchema<Shape>["create"],
261
+ } as CoProfileSchema<Shape>;
262
+ };
263
+
264
+ const coFeedDefiner = <T extends z.core.$ZodType>(
265
+ element: T,
266
+ ): CoFeedSchema<T> => {
267
+ const placeholderSchema = z.instanceof(CoFeed);
268
+
269
+ const coFeedSchema = placeholderSchema as unknown as Pick<
270
+ typeof placeholderSchema,
271
+ "_zod" | "def" | "~standard"
272
+ > & {
273
+ collaborative: true;
274
+ builtin: "CoFeed";
275
+ element: T;
276
+ create: CoFeedSchema<T>["create"];
277
+ load: CoFeedSchema<T>["load"];
278
+ subscribe: CoFeedSchema<T>["subscribe"];
279
+ };
280
+
281
+ coFeedSchema.collaborative = true;
282
+ coFeedSchema.builtin = "CoFeed";
283
+ coFeedSchema.element = element;
284
+
285
+ coFeedSchema.create = function (this: CoFeedSchema<T>, ...args: any[]) {
286
+ return (zodSchemaToCoSchema(this) as any).create(...args);
287
+ } as CoFeedSchema<T>["create"];
288
+
289
+ coFeedSchema.load = function (this: CoFeedSchema<T>, ...args: any[]) {
290
+ return (zodSchemaToCoSchema(this) as any).load(...args);
291
+ } as CoFeedSchema<T>["load"];
292
+
293
+ coFeedSchema.subscribe = function (this: CoFeedSchema<T>, ...args: any[]) {
294
+ return (zodSchemaToCoSchema(this) as any).subscribe(...args);
295
+ } as CoFeedSchema<T>["subscribe"];
296
+
297
+ return coFeedSchema as unknown as CoFeedSchema<T>;
298
+ };
299
+
300
+ export const coFileStreamDefiner = (): FileStreamSchema => {
301
+ const placeholderSchema = z.instanceof(FileStream);
302
+
303
+ const fileStreamSchema = placeholderSchema as unknown as Pick<
304
+ typeof placeholderSchema,
305
+ "_zod" | "def" | "~standard"
306
+ > & {
307
+ collaborative: true;
308
+ builtin: "FileStream";
309
+ create: (typeof FileStream)["create"];
310
+ createFromBlob: (typeof FileStream)["createFromBlob"];
311
+ };
312
+
313
+ fileStreamSchema.collaborative = true;
314
+ fileStreamSchema.builtin = "FileStream";
315
+
316
+ fileStreamSchema.create = function (options: any) {
317
+ return FileStream.create(options);
318
+ } as (typeof FileStream)["create"];
319
+
320
+ fileStreamSchema.createFromBlob = function (blob: Blob, options: any) {
321
+ return FileStream.createFromBlob(blob, options);
322
+ } as (typeof FileStream)["createFromBlob"];
323
+
324
+ return fileStreamSchema;
325
+ };
326
+
327
+ export const coPlainTextDefiner = (): PlainTextSchema => {
328
+ const placeholderSchema = z.instanceof(CoPlainText);
329
+
330
+ const plainTextSchema = placeholderSchema as unknown as Pick<
331
+ typeof placeholderSchema,
332
+ "_zod" | "def" | "~standard"
333
+ > & {
334
+ collaborative: true;
335
+ builtin: "CoPlainText";
336
+ create: PlainTextSchema["create"];
337
+ load: PlainTextSchema["load"];
338
+ subscribe: PlainTextSchema["subscribe"];
339
+ fromRaw: PlainTextSchema["fromRaw"];
340
+ };
341
+
342
+ plainTextSchema.collaborative = true;
343
+ plainTextSchema.builtin = "CoPlainText";
344
+
345
+ plainTextSchema.create = function (...args: any[]) {
346
+ return (CoPlainText as any).create(...args);
347
+ } as PlainTextSchema["create"];
348
+
349
+ plainTextSchema.load = function (...args: any[]) {
350
+ return (CoPlainText as any).load(...args);
351
+ } as PlainTextSchema["load"];
352
+
353
+ plainTextSchema.subscribe = function (...args: any[]) {
354
+ return (CoPlainText as any).subscribe(...args);
355
+ } as PlainTextSchema["subscribe"];
356
+
357
+ plainTextSchema.fromRaw = function (...args: any[]) {
358
+ return (CoPlainText as any).fromRaw(...args);
359
+ } as PlainTextSchema["fromRaw"];
360
+
361
+ return plainTextSchema;
362
+ };
363
+
364
+ export const coRichTextDefiner = (): RichTextSchema => {
365
+ const placeholderSchema = z.instanceof(CoRichText);
366
+
367
+ const richTextSchema = placeholderSchema as unknown as Pick<
368
+ typeof placeholderSchema,
369
+ "_zod" | "def" | "~standard"
370
+ > & {
371
+ collaborative: true;
372
+ builtin: "CoRichText";
373
+ create: RichTextSchema["create"];
374
+ load: RichTextSchema["load"];
375
+ subscribe: RichTextSchema["subscribe"];
376
+ };
377
+
378
+ richTextSchema.collaborative = true;
379
+ richTextSchema.builtin = "CoRichText";
380
+
381
+ richTextSchema.create = function (...args: any[]) {
382
+ return (CoRichText as any).create(...args);
383
+ } as RichTextSchema["create"];
384
+
385
+ richTextSchema.load = function (...args: any[]) {
386
+ return (CoRichText as any).load(...args);
387
+ } as RichTextSchema["load"];
388
+
389
+ richTextSchema.subscribe = function (...args: any[]) {
390
+ return (CoRichText as any).subscribe(...args);
391
+ } as RichTextSchema["subscribe"];
392
+
393
+ return richTextSchema;
394
+ };
395
+
396
+ export const co = {
397
+ map: coMapDefiner,
398
+ record: coRecordDefiner,
399
+ list: coListDefiner,
400
+ feed: coFeedDefiner,
401
+ plainText: coPlainTextDefiner,
402
+ richText: coRichTextDefiner,
403
+ fileStream: coFileStreamDefiner,
404
+ image: (): typeof ImageDefinition => {
405
+ return ImageDefinition;
406
+ },
407
+ account: coAccountDefiner,
408
+ profile: coProfileDefiner,
409
+ };
@@ -0,0 +1,116 @@
1
+ import { LocalNode, RawAccount } from "cojson";
2
+ import z from "zod/v4";
3
+ import {
4
+ Account,
5
+ AccountClass,
6
+ CoValue,
7
+ CoValueClass,
8
+ CoValueFromRaw,
9
+ RefsToResolve,
10
+ RefsToResolveStrict,
11
+ Resolved,
12
+ Simplify,
13
+ } from "../../internal.js";
14
+ import { AnyAccountSchema } from "./schemaTypes/AccountSchema.js";
15
+ import { AnyCoFeedSchema } from "./schemaTypes/CoFeedSchema.js";
16
+ import { AnyCoListSchema } from "./schemaTypes/CoListSchema.js";
17
+ import { AnyCoMapSchema, CoMapInitZod } from "./schemaTypes/CoMapSchema.js";
18
+ import { AnyCoRecordSchema } from "./schemaTypes/CoRecordSchema.js";
19
+ import { FileStreamSchema } from "./schemaTypes/FileStreamSchema.js";
20
+ import { PlainTextSchema } from "./schemaTypes/PlainTextSchema.js";
21
+ import { RichTextSchema } from "./schemaTypes/RichTextSchema.js";
22
+ import { InstanceOfSchema } from "./typeConverters/InstanceOfSchema.js";
23
+ import { InstanceOfSchemaCoValuesNullable } from "./typeConverters/InstanceOfSchemaCoValuesNullable.js";
24
+ import { InstanceOrPrimitiveOfSchema } from "./typeConverters/InstanceOrPrimitiveOfSchema.js";
25
+ import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "./typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
26
+
27
+ // defining an extra type for this, otherwise BaseSchema & {...} often
28
+ // gets expanded into a n inferred type that's too long for typescript to print
29
+ export type WithHelpers<
30
+ Base extends z.core.$ZodType,
31
+ Helpers extends object,
32
+ > = Base & Helpers;
33
+
34
+ export type FullyOrPartiallyLoaded<S extends z.core.$ZodType | CoValueClass> =
35
+ InstanceOrPrimitiveOfSchema<S> extends CoValue
36
+ ? NonNullable<InstanceOrPrimitiveOfSchemaCoValuesNullable<S>>
37
+ : InstanceOrPrimitiveOfSchema<S>;
38
+
39
+ export type ZodPrimitiveSchema =
40
+ | z.core.$ZodString
41
+ | z.core.$ZodNumber
42
+ | z.core.$ZodBoolean
43
+ | z.core.$ZodNull
44
+ | z.core.$ZodDate
45
+ | z.core.$ZodLiteral;
46
+
47
+ export type AnyCoUnionSchema = z.core.$ZodDiscriminatedUnion<
48
+ (
49
+ | (z.core.$ZodType & { collaborative: true })
50
+ | z.core.$ZodDiscriminatedUnion
51
+ )[]
52
+ >;
53
+
54
+ // this is a series of hacks to work around z4 removing _zod at runtime from z.core.$ZodType
55
+ export function isZodObject(
56
+ schema: z.core.$ZodType,
57
+ ): schema is z.core.$ZodObject<any, any> {
58
+ return (schema as any).def?.type === "object";
59
+ }
60
+
61
+ export function isZodArray(
62
+ schema: z.core.$ZodType,
63
+ ): schema is z.core.$ZodArray<any> {
64
+ return (schema as any).def?.type === "array";
65
+ }
66
+
67
+ export function isZodCustom(
68
+ schema: z.core.$ZodType,
69
+ ): schema is z.core.$ZodCustom<any, any> {
70
+ return (schema as any).def?.type === "custom";
71
+ }
72
+
73
+ export function getDef<S extends z.core.$ZodType>(schema: S): S["_zod"]["def"] {
74
+ return (schema as any).def;
75
+ }
76
+
77
+ export type CoValueOrZodSchema = CoValueClass | AnyCoSchema;
78
+
79
+ export type CoValueClassFromZodSchema<S extends z.core.$ZodType> = CoValueClass<
80
+ InstanceOfSchema<S>
81
+ > &
82
+ CoValueFromRaw<InstanceOfSchema<S>> &
83
+ (S extends AnyAccountSchema ? AccountClassEssentials : {});
84
+
85
+ type AccountClassEssentials = {
86
+ fromRaw: <A extends Account>(this: AccountClass<A>, raw: RawAccount) => A;
87
+ fromNode: <A extends Account>(this: AccountClass<A>, node: LocalNode) => A;
88
+ };
89
+
90
+ type AnyCoSchema =
91
+ | AnyCoMapSchema
92
+ | AnyAccountSchema
93
+ | AnyCoRecordSchema
94
+ | AnyCoListSchema
95
+ | AnyCoFeedSchema
96
+ | AnyCoUnionSchema
97
+ | PlainTextSchema
98
+ | RichTextSchema
99
+ | FileStreamSchema;
100
+
101
+ export type Loaded<
102
+ T extends CoValueClass | AnyCoSchema,
103
+ R extends ResolveQuery<T> = true,
104
+ > = Resolved<NonNullable<InstanceOfSchemaCoValuesNullable<T>>, R>;
105
+
106
+ export type ResolveQuery<T extends CoValueClass | AnyCoSchema> = RefsToResolve<
107
+ NonNullable<InstanceOfSchemaCoValuesNullable<T>>
108
+ >;
109
+
110
+ export type ResolveQueryStrict<
111
+ T extends CoValueClass | AnyCoSchema,
112
+ R extends ResolveQuery<T>,
113
+ > = RefsToResolveStrict<NonNullable<InstanceOfSchemaCoValuesNullable<T>>, R>;
114
+
115
+ export type InitFor<T extends CoValueClass | AnyCoSchema> =
116
+ T extends AnyCoMapSchema<infer Shape> ? Simplify<CoMapInitZod<Shape>> : never;
package/src/internal.ts CHANGED
@@ -1,13 +1,51 @@
1
1
  export * from "./coValues/interfaces.js";
2
+ export * from "./coValues/CoValueBase.js";
2
3
  export * from "./implementation/inspect.js";
3
4
  export * from "./implementation/symbols.js";
4
5
 
6
+ export * from "./coValues/registeredSchemas.js";
7
+
8
+ export * from "./coValues/coMap.js";
9
+ export * from "./coValues/coList.js";
10
+ export * from "./coValues/coFeed.js";
11
+ export * from "./coValues/account.js";
12
+ export * from "./coValues/group.js";
13
+ export * from "./coValues/profile.js";
14
+ export * from "./coValues/inbox.js";
15
+ export * from "./coValues/coPlainText.js";
16
+ export * from "./coValues/coRichText.js";
17
+ export * from "./coValues/schemaUnion.js";
18
+
5
19
  export * from "./coValues/deepLoading.js";
6
20
  export * from "./implementation/anonymousJazzAgent.js";
21
+ export * from "./implementation/activeAccountContext.js";
7
22
  export * from "./implementation/errors.js";
8
23
  export * from "./implementation/refs.js";
9
24
  export * from "./implementation/schema.js";
25
+ export * from "./subscribe/SubscriptionScope.js";
26
+ export * from "./subscribe/types.js";
10
27
  export * from "./subscribe/index.js";
28
+ export * from "./lib/cache.js";
11
29
  export * from "./implementation/createContext.js";
12
30
 
31
+ export * from "./types.js";
32
+ export * from "./implementation/zodSchema/zodSchema.js";
33
+ export * from "./implementation/zodSchema/zodCo.js";
34
+ export * from "./implementation/zodSchema/schemaTypes/CoMapSchema.js";
35
+ export * from "./implementation/zodSchema/schemaTypes/CoRecordSchema.js";
36
+ export * from "./implementation/zodSchema/schemaTypes/CoListSchema.js";
37
+ export * from "./implementation/zodSchema/schemaTypes/CoFeedSchema.js";
38
+ export * from "./implementation/zodSchema/schemaTypes/AccountSchema.js";
39
+ export * from "./implementation/zodSchema/schemaTypes/FileStreamSchema.js";
40
+ export * from "./implementation/zodSchema/schemaTypes/PlainTextSchema.js";
41
+ export * from "./implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.js";
42
+ export * from "./implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
43
+ export * from "./implementation/zodSchema/typeConverters/InstanceOfSchema.js";
44
+ export * from "./implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.js";
45
+ export * from "./implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.js";
46
+ export * from "./implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.js";
47
+ export * from "./coValues/extensions/imageDef.js";
48
+
49
+ export * from "./implementation/ContextManager.js";
50
+
13
51
  import "./implementation/devtoolsFormatters.js";
@@ -1,6 +1,8 @@
1
1
  import type { LocalNode, RawCoValue } from "cojson";
2
- import type { CoFeed, CoList, CoMap } from "../exports.js";
3
2
  import {
3
+ CoFeed,
4
+ CoList,
5
+ CoMap,
4
6
  type CoValue,
5
7
  type ID,
6
8
  type RefEncoded,
@@ -1,6 +1,11 @@
1
1
  import { RawAccount, RawCoValue } from "cojson";
2
2
  import { RegisteredSchemas } from "../coValues/registeredSchemas.js";
3
- import { CoValue, RefEncoded, instantiateRefEncoded } from "../internal.js";
3
+ import {
4
+ CoValue,
5
+ RefEncoded,
6
+ anySchemaToCoSchema,
7
+ instantiateRefEncoded,
8
+ } from "../internal.js";
4
9
  import { coValuesCache } from "../lib/cache.js";
5
10
  import { SubscriptionScope } from "./SubscriptionScope.js";
6
11
 
@@ -9,7 +14,7 @@ export function getOwnerFromRawValue(raw: RawCoValue) {
9
14
 
10
15
  return coValuesCache.get(owner, () =>
11
16
  owner instanceof RawAccount
12
- ? RegisteredSchemas["Account"].fromRaw(owner)
17
+ ? anySchemaToCoSchema(RegisteredSchemas["Account"]).fromRaw(owner)
13
18
  : RegisteredSchemas["Group"].fromRaw(owner as any),
14
19
  );
15
20
  }
package/src/testing.ts CHANGED
@@ -4,17 +4,14 @@ import { PureJSCrypto } from "cojson/dist/crypto/PureJSCrypto";
4
4
  import {
5
5
  Account,
6
6
  AccountClass,
7
+ type AnonymousJazzAgent,
7
8
  AuthCredentials,
8
- JazzContextManagerAuthProps,
9
- } from "./exports.js";
10
- import {
9
+ type CoValueClass,
10
+ CoValueFromRaw,
11
11
  JazzContextManager,
12
+ JazzContextManagerAuthProps,
12
13
  JazzContextManagerBaseProps,
13
- } from "./implementation/ContextManager.js";
14
- import { activeAccountContext } from "./implementation/activeAccountContext.js";
15
- import {
16
- type AnonymousJazzAgent,
17
- type CoValueClass,
14
+ activeAccountContext,
18
15
  createAnonymousJazzContext,
19
16
  createJazzContext,
20
17
  randomSessionProvider,
@@ -76,13 +73,13 @@ export function getPeerConnectedToTestSyncServer() {
76
73
  const SecretSeedMap = new Map<string, Uint8Array>();
77
74
  let isMigrationActive = false;
78
75
 
79
- export async function createJazzTestAccount<Acc extends Account>(options?: {
76
+ export async function createJazzTestAccount<A extends Account>(options?: {
80
77
  isCurrentActiveAccount?: boolean;
81
- AccountSchema?: CoValueClass<Acc>;
78
+ AccountSchema?: CoValueClass<A>;
82
79
  creationProps?: Record<string, unknown>;
83
- }): Promise<Acc> {
80
+ }): Promise<A> {
84
81
  const AccountSchema = (options?.AccountSchema ??
85
- Account) as unknown as TestAccountSchema<Acc>;
82
+ Account) as unknown as TestAccountSchema<Account>;
86
83
  const peers = [];
87
84
  if (syncServer.current) {
88
85
  peers.push(getPeerConnectedToTestSyncServer());
@@ -134,7 +131,7 @@ export async function createJazzTestAccount<Acc extends Account>(options?: {
134
131
  activeAccountContext.set(account);
135
132
  }
136
133
 
137
- return account;
134
+ return account as A;
138
135
  }
139
136
 
140
137
  export function setActiveAccount(account: Account) {
@@ -155,7 +152,7 @@ export async function createJazzTestGuest() {
155
152
  export type TestJazzContextManagerProps<Acc extends Account> =
156
153
  JazzContextManagerBaseProps<Acc> & {
157
154
  defaultProfileName?: string;
158
- AccountSchema?: AccountClass<Acc>;
155
+ AccountSchema?: AccountClass<Acc> & CoValueFromRaw<Acc>;
159
156
  isAuthenticated?: boolean;
160
157
  };
161
158
 
@@ -194,7 +191,8 @@ export class TestJazzContextManager<
194
191
 
195
192
  context.updateContext(
196
193
  {
197
- AccountSchema: account.constructor as AccountClass<Acc>,
194
+ AccountSchema: account.constructor as AccountClass<Acc> &
195
+ CoValueFromRaw<Acc>,
198
196
  ...props,
199
197
  },
200
198
  {
@@ -247,7 +245,7 @@ export class TestJazzContextManager<
247
245
  );
248
246
  }
249
247
 
250
- const context = await createJazzContext<Acc>({
248
+ const context = await createJazzContext({
251
249
  credentials: authProps?.credentials,
252
250
  defaultProfileName: props.defaultProfileName,
253
251
  newAccountProps: authProps?.newAccountProps,