jazz-tools 0.15.16 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (199) hide show
  1. package/.svelte-kit/__package__/jazz.class.svelte.d.ts +2 -2
  2. package/.svelte-kit/__package__/jazz.class.svelte.d.ts.map +1 -1
  3. package/.svelte-kit/__package__/jazz.class.svelte.js +5 -5
  4. package/.svelte-kit/__package__/jazz.svelte.d.ts +2 -2
  5. package/.svelte-kit/__package__/jazz.svelte.d.ts.map +1 -1
  6. package/.turbo/turbo-build.log +46 -46
  7. package/CHANGELOG.md +25 -0
  8. package/dist/browser/index.d.ts +2 -2
  9. package/dist/browser/index.d.ts.map +1 -1
  10. package/dist/browser/index.js.map +1 -1
  11. package/dist/browser-media-images/index.d.ts +3 -1
  12. package/dist/browser-media-images/index.d.ts.map +1 -1
  13. package/dist/browser-media-images/index.js.map +1 -1
  14. package/dist/{chunk-OSVAAVWQ.js → chunk-MLCNE3TL.js} +594 -598
  15. package/dist/chunk-MLCNE3TL.js.map +1 -0
  16. package/dist/index.js +8 -10
  17. package/dist/index.js.map +1 -1
  18. package/dist/react/hooks.d.ts +2 -2
  19. package/dist/react/hooks.d.ts.map +1 -1
  20. package/dist/react/index.js.map +1 -1
  21. package/dist/react-core/hooks.d.ts +2 -2
  22. package/dist/react-core/hooks.d.ts.map +1 -1
  23. package/dist/react-core/index.js +3 -3
  24. package/dist/react-core/index.js.map +1 -1
  25. package/dist/react-native-core/hooks.d.ts +2 -2
  26. package/dist/react-native-core/hooks.d.ts.map +1 -1
  27. package/dist/react-native-core/index.js.map +1 -1
  28. package/dist/svelte/jazz.class.svelte.d.ts +2 -2
  29. package/dist/svelte/jazz.class.svelte.d.ts.map +1 -1
  30. package/dist/svelte/jazz.class.svelte.js +5 -5
  31. package/dist/svelte/jazz.svelte.d.ts +2 -2
  32. package/dist/svelte/jazz.svelte.d.ts.map +1 -1
  33. package/dist/testing.js +3 -3
  34. package/dist/testing.js.map +1 -1
  35. package/dist/tools/coValues/CoValueBase.d.ts +3 -13
  36. package/dist/tools/coValues/CoValueBase.d.ts.map +1 -1
  37. package/dist/tools/coValues/account.d.ts +2 -2
  38. package/dist/tools/coValues/account.d.ts.map +1 -1
  39. package/dist/tools/coValues/coFeed.d.ts.map +1 -1
  40. package/dist/tools/coValues/coList.d.ts.map +1 -1
  41. package/dist/tools/coValues/coMap.d.ts +5 -18
  42. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  43. package/dist/tools/coValues/deepLoading.d.ts +4 -1
  44. package/dist/tools/coValues/deepLoading.d.ts.map +1 -1
  45. package/dist/tools/coValues/extensions/imageDef.d.ts +4 -7
  46. package/dist/tools/coValues/extensions/imageDef.d.ts.map +1 -1
  47. package/dist/tools/coValues/inbox.d.ts +2 -2
  48. package/dist/tools/coValues/inbox.d.ts.map +1 -1
  49. package/dist/tools/coValues/interfaces.d.ts +2 -17
  50. package/dist/tools/coValues/interfaces.d.ts.map +1 -1
  51. package/dist/tools/coValues/request.d.ts +2 -2
  52. package/dist/tools/coValues/request.d.ts.map +1 -1
  53. package/dist/tools/coValues/schemaUnion.d.ts +5 -1
  54. package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
  55. package/dist/tools/exports.d.ts +3 -3
  56. package/dist/tools/exports.d.ts.map +1 -1
  57. package/dist/tools/implementation/createContext.d.ts +4 -4
  58. package/dist/tools/implementation/createContext.d.ts.map +1 -1
  59. package/dist/tools/implementation/invites.d.ts +2 -2
  60. package/dist/tools/implementation/invites.d.ts.map +1 -1
  61. package/dist/tools/implementation/schemaUtils.d.ts +8 -0
  62. package/dist/tools/implementation/schemaUtils.d.ts.map +1 -0
  63. package/dist/tools/implementation/zodSchema/coExport.d.ts +11 -1
  64. package/dist/tools/implementation/zodSchema/coExport.d.ts.map +1 -1
  65. package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts +22 -0
  66. package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts.map +1 -0
  67. package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts +10 -0
  68. package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts.map +1 -0
  69. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +11 -11
  70. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
  71. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +34 -24
  72. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -1
  73. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +16 -14
  74. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  75. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +23 -17
  76. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  77. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +24 -16
  78. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  79. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts +20 -9
  80. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts.map +1 -1
  81. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +18 -12
  82. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  83. package/dist/tools/implementation/zodSchema/schemaTypes/CoValueSchema.d.ts +18 -0
  84. package/dist/tools/implementation/zodSchema/schemaTypes/CoValueSchema.d.ts.map +1 -0
  85. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +14 -9
  86. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  87. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +14 -9
  88. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
  89. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +14 -9
  90. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
  91. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +15 -13
  92. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
  93. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +12 -15
  94. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -1
  95. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +17 -20
  96. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -1
  97. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +17 -20
  98. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -1
  99. package/dist/tools/implementation/zodSchema/unionUtils.d.ts +3 -5
  100. package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
  101. package/dist/tools/implementation/zodSchema/zodCo.d.ts +10 -8
  102. package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
  103. package/dist/tools/implementation/zodSchema/zodReExport.d.ts +4 -8
  104. package/dist/tools/implementation/zodSchema/zodReExport.d.ts.map +1 -1
  105. package/dist/tools/implementation/zodSchema/zodSchema.d.ts +21 -30
  106. package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
  107. package/dist/tools/internal.d.ts +3 -2
  108. package/dist/tools/internal.d.ts.map +1 -1
  109. package/dist/tools/lib/utilityTypes.d.ts +10 -0
  110. package/dist/tools/lib/utilityTypes.d.ts.map +1 -0
  111. package/dist/tools/subscribe/utils.d.ts.map +1 -1
  112. package/dist/tools/testing.d.ts +2 -2
  113. package/dist/tools/testing.d.ts.map +1 -1
  114. package/dist/worker/index.d.ts.map +1 -1
  115. package/dist/worker/index.js +2 -2
  116. package/dist/worker/index.js.map +1 -1
  117. package/package.json +5 -5
  118. package/src/browser/index.ts +2 -4
  119. package/src/browser-media-images/index.ts +1 -1
  120. package/src/react/hooks.tsx +2 -2
  121. package/src/react-core/hooks.ts +6 -6
  122. package/src/react-core/tests/useAccount.test.ts +2 -2
  123. package/src/react-core/tests/useCoState.test.ts +3 -2
  124. package/src/react-native-core/hooks.tsx +2 -2
  125. package/src/svelte/jazz.class.svelte.ts +10 -7
  126. package/src/svelte/jazz.svelte.ts +2 -2
  127. package/src/tools/coValues/CoValueBase.ts +8 -20
  128. package/src/tools/coValues/account.ts +18 -14
  129. package/src/tools/coValues/coFeed.ts +0 -4
  130. package/src/tools/coValues/coList.ts +7 -9
  131. package/src/tools/coValues/coMap.ts +1 -6
  132. package/src/tools/coValues/coPlainText.ts +4 -4
  133. package/src/tools/coValues/deepLoading.ts +4 -1
  134. package/src/tools/coValues/extensions/imageDef.ts +3 -3
  135. package/src/tools/coValues/inbox.ts +4 -4
  136. package/src/tools/coValues/interfaces.ts +4 -71
  137. package/src/tools/coValues/request.ts +15 -13
  138. package/src/tools/coValues/schemaUnion.ts +8 -4
  139. package/src/tools/exports.ts +3 -14
  140. package/src/tools/implementation/createContext.ts +9 -9
  141. package/src/tools/implementation/invites.ts +2 -2
  142. package/src/tools/implementation/schemaUtils.ts +18 -0
  143. package/src/tools/implementation/zodSchema/coExport.ts +14 -0
  144. package/src/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.ts +156 -0
  145. package/src/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.ts +133 -0
  146. package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +36 -17
  147. package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +101 -52
  148. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +54 -50
  149. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +54 -46
  150. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +187 -137
  151. package/src/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.ts +29 -27
  152. package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +28 -18
  153. package/src/tools/implementation/zodSchema/schemaTypes/CoValueSchema.ts +18 -0
  154. package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +45 -36
  155. package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +47 -35
  156. package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +43 -30
  157. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +28 -23
  158. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +28 -25
  159. package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +86 -78
  160. package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +91 -82
  161. package/src/tools/implementation/zodSchema/unionUtils.ts +47 -51
  162. package/src/tools/implementation/zodSchema/zodCo.ts +42 -75
  163. package/src/tools/implementation/zodSchema/zodReExport.ts +44 -24
  164. package/src/tools/implementation/zodSchema/zodSchema.ts +64 -102
  165. package/src/tools/internal.ts +3 -2
  166. package/src/tools/lib/utilityTypes.ts +7 -0
  167. package/src/tools/subscribe/utils.ts +4 -2
  168. package/src/tools/testing.ts +4 -4
  169. package/src/tools/tests/ContextManager.test.ts +8 -9
  170. package/src/tools/tests/account.test.ts +65 -3
  171. package/src/tools/tests/coDiscriminatedUnion.test-d.ts +38 -0
  172. package/src/tools/tests/coDiscriminatedUnion.test.ts +219 -1
  173. package/src/tools/tests/coFeed.test-d.ts +4 -3
  174. package/src/tools/tests/coList.test-d.ts +32 -3
  175. package/src/tools/tests/coList.test.ts +20 -2
  176. package/src/tools/tests/coMap.record.test-d.ts +31 -3
  177. package/src/tools/tests/coMap.record.test.ts +9 -9
  178. package/src/tools/tests/coMap.test-d.ts +8 -8
  179. package/src/tools/tests/coMap.test.ts +19 -5
  180. package/src/tools/tests/coOptional.test.ts +63 -1
  181. package/src/tools/tests/createContext.test.ts +7 -9
  182. package/src/tools/tests/deepLoading.test.ts +4 -10
  183. package/src/tools/tests/exportImport.test.ts +2 -2
  184. package/src/tools/tests/groupsAndAccounts.test.ts +5 -4
  185. package/src/tools/tests/inbox.test.ts +3 -2
  186. package/src/tools/tests/load.test.ts +3 -29
  187. package/src/tools/tests/schemaUnion.test.ts +2 -2
  188. package/src/tools/tests/subscribe.test.ts +22 -114
  189. package/src/tools/tests/testing.test.ts +6 -6
  190. package/src/tools/tests/zod.test-d.ts +27 -0
  191. package/src/tools/tests/zod.test.ts +50 -45
  192. package/src/worker/index.ts +0 -1
  193. package/dist/chunk-OSVAAVWQ.js.map +0 -1
  194. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +0 -12
  195. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +0 -1
  196. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +0 -9
  197. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +0 -1
  198. package/src/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +0 -172
  199. package/src/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +0 -218
@@ -14,7 +14,7 @@ import {
14
14
  Resolved,
15
15
  SubscribeListenerOptions,
16
16
  SubscribeRestArgs,
17
- anySchemaToCoSchema,
17
+ coValueClassFromCoValueClassOrSchema,
18
18
  parseCoValueCreateOptions,
19
19
  } from "../internal.js";
20
20
  import {
@@ -47,9 +47,9 @@ export class CoPlainText extends String implements CoValue {
47
47
 
48
48
  if (agent instanceof ControlledAccount) {
49
49
  return coValuesCache.get(agent.account, () =>
50
- anySchemaToCoSchema(RegisteredSchemas["Account"]).fromRaw(
51
- agent.account,
52
- ),
50
+ coValueClassFromCoValueClassOrSchema(
51
+ RegisteredSchemas["Account"],
52
+ ).fromRaw(agent.account),
53
53
  );
54
54
  }
55
55
 
@@ -5,7 +5,10 @@ import { CoFeedEntry } from "./coFeed.js";
5
5
  import { type CoKeys } from "./coMap.js";
6
6
  import { type CoValue, type ID } from "./interfaces.js";
7
7
 
8
- type NotNull<T> = Exclude<T, null>;
8
+ /**
9
+ * Similar to {@link NonNullable}, but removes only `null` and preserves `undefined`.
10
+ */
11
+ export type NotNull<T> = Exclude<T, null>;
9
12
 
10
13
  /**
11
14
  * Used to check if T is a union type.
@@ -8,9 +8,9 @@ const ImageDefinitionBase = coMapDefiner({
8
8
  }).catchall(coFileStreamDefiner());
9
9
 
10
10
  /** @category Media */
11
- export const ImageDefinition = ImageDefinitionBase.withHelpers((Self) => ({
11
+ export const ImageDefinition = Object.assign({}, ImageDefinitionBase, {
12
12
  highestResAvailable(
13
- imageDef: Loaded<typeof Self>,
13
+ imageDef: ImageDefinition,
14
14
  options?: {
15
15
  maxWidth?: number;
16
16
  targetWidth?: number;
@@ -56,5 +56,5 @@ export const ImageDefinition = ImageDefinitionBase.withHelpers((Self) => ({
56
56
  }
57
57
  );
58
58
  },
59
- }));
59
+ });
60
60
  export type ImageDefinition = Loaded<typeof ImageDefinition>;
@@ -3,11 +3,11 @@ import { CoStreamItem, RawCoStream } from "cojson";
3
3
  import {
4
4
  type Account,
5
5
  CoValue,
6
- CoValueOrZodSchema,
6
+ CoValueClassOrSchema,
7
7
  ID,
8
8
  InstanceOfSchema,
9
9
  activeAccountContext,
10
- anySchemaToCoSchema,
10
+ coValueClassFromCoValueClassOrSchema,
11
11
  loadCoValue,
12
12
  } from "../internal.js";
13
13
  import { isCoValueId } from "../lib/id.js";
@@ -109,7 +109,7 @@ export class Inbox {
109
109
  this.failed = failed;
110
110
  }
111
111
 
112
- subscribe<M extends CoValueOrZodSchema, O extends CoValue | undefined>(
112
+ subscribe<M extends CoValueClassOrSchema, O extends CoValue | undefined>(
113
113
  Schema: M,
114
114
  callback: (
115
115
  message: InstanceOfSchema<M>,
@@ -172,7 +172,7 @@ export class Inbox {
172
172
  }
173
173
 
174
174
  return loadCoValue(
175
- anySchemaToCoSchema(Schema),
175
+ coValueClassFromCoValueClassOrSchema(Schema),
176
176
  message.get("payload")!,
177
177
  {
178
178
  loadAs: account,
@@ -1,5 +1,4 @@
1
1
  import {
2
- CoValueCore,
3
2
  type CoValueUniqueness,
4
3
  type CojsonInternalTypes,
5
4
  type RawCoValue,
@@ -9,7 +8,7 @@ import { AvailableCoValueCore } from "cojson/dist/coValueCore/coValueCore.js";
9
8
  import {
10
9
  Account,
11
10
  AnonymousJazzAgent,
12
- CoValueOrZodSchema,
11
+ CoValueClassOrSchema,
13
12
  type Group,
14
13
  Loaded,
15
14
  RefsToResolve,
@@ -21,8 +20,7 @@ import {
21
20
  SubscriptionScope,
22
21
  type SubscriptionValue,
23
22
  activeAccountContext,
24
- anySchemaToCoSchema,
25
- getSubscriptionScope,
23
+ coValueClassFromCoValueClassOrSchema,
26
24
  inspect,
27
25
  } from "../internal.js";
28
26
 
@@ -312,71 +310,6 @@ export function subscribeToCoValue<
312
310
  return unsubscribe;
313
311
  }
314
312
 
315
- /**
316
- * @deprecated Used for the React integration in the past, but we moved to use SubscriptionScope directly.
317
- *
318
- * Going to be removed in the next minor version.
319
- */
320
- export function createCoValueObservable<
321
- S extends CoValueOrZodSchema,
322
- const R extends ResolveQuery<S>,
323
- >(initialValue: undefined | null = undefined) {
324
- let currentValue: Loaded<S, R> | undefined | null = initialValue;
325
- let subscriberCount = 0;
326
-
327
- function subscribe(
328
- cls: S,
329
- id: string,
330
- options: {
331
- loadAs: Account | AnonymousJazzAgent;
332
- resolve?: ResolveQueryStrict<S, R>;
333
- onUnavailable?: () => void;
334
- onUnauthorized?: () => void;
335
- syncResolution?: boolean;
336
- },
337
- listener: () => void,
338
- ) {
339
- subscriberCount++;
340
-
341
- const unsubscribe = subscribeToCoValue(
342
- anySchemaToCoSchema(cls),
343
- id,
344
- {
345
- loadAs: options.loadAs,
346
- resolve: options.resolve as any,
347
- onUnavailable: () => {
348
- currentValue = null;
349
- options.onUnavailable?.();
350
- },
351
- onUnauthorized: () => {
352
- currentValue = null;
353
- options.onUnauthorized?.();
354
- },
355
- syncResolution: options.syncResolution,
356
- },
357
- (value) => {
358
- currentValue = value as Loaded<S, R>;
359
- listener();
360
- },
361
- );
362
-
363
- return () => {
364
- unsubscribe();
365
- subscriberCount--;
366
- if (subscriberCount === 0) {
367
- currentValue = undefined;
368
- }
369
- };
370
- }
371
-
372
- const observable = {
373
- getCurrentValue: () => currentValue,
374
- subscribe,
375
- };
376
-
377
- return observable;
378
- }
379
-
380
313
  export function subscribeToExistingCoValue<
381
314
  V extends CoValue,
382
315
  const R extends RefsToResolve<V>,
@@ -515,7 +448,7 @@ export function parseGroupCreateOptions(
515
448
  * ```
516
449
  */
517
450
  export async function exportCoValue<
518
- S extends CoValueOrZodSchema | CoValueClass<CoValue>,
451
+ S extends CoValueClassOrSchema,
519
452
  const R extends ResolveQuery<S>,
520
453
  >(
521
454
  cls: S,
@@ -537,7 +470,7 @@ export async function exportCoValue<
537
470
  resolve as any,
538
471
  id,
539
472
  {
540
- ref: anySchemaToCoSchema(cls),
473
+ ref: coValueClassFromCoValueClassOrSchema(cls),
541
474
  optional: false,
542
475
  },
543
476
  options.skipRetry,
@@ -8,19 +8,19 @@ import {
8
8
  } from "cojson";
9
9
  import z from "zod/v4";
10
10
  import {
11
- AnyCoMapSchema,
12
- AnyCoSchema,
11
+ AnyZodOrCoValueSchema,
13
12
  CoMap,
14
13
  CoMapInitZod,
15
14
  CoMapSchema,
16
15
  CoValueClass,
16
+ CoreCoMapSchema,
17
17
  Group,
18
18
  Loaded,
19
19
  ResolveQuery,
20
20
  ResolveQueryStrict,
21
21
  Simplify,
22
- anySchemaToCoSchema,
23
22
  coMapDefiner,
23
+ coValueClassFromCoValueClassOrSchema,
24
24
  exportCoValue,
25
25
  importContentPieces,
26
26
  loadCoValue,
@@ -28,7 +28,7 @@ import {
28
28
  import { isCoValueId } from "../lib/id.js";
29
29
  import { Account } from "./account.js";
30
30
 
31
- type MessageShape = Record<string, z.core.$ZodType | AnyCoSchema>;
31
+ type MessageShape = Record<string, AnyZodOrCoValueSchema>;
32
32
 
33
33
  type RequestSchemaDefinition<
34
34
  S extends MessageShape,
@@ -97,7 +97,7 @@ type MessageValuePayload<T extends MessageShape> =
97
97
  | AsNullablePayload<T>;
98
98
 
99
99
  function createMessageEnvelope<S extends MessageShape>(
100
- schema: AnyCoMapSchema,
100
+ schema: CoreCoMapSchema,
101
101
  value: MessageValuePayload<S>,
102
102
  owner: Account,
103
103
  sharedWith: Account | Group,
@@ -111,7 +111,7 @@ function createMessageEnvelope<S extends MessageShape>(
111
111
  group.addMember(sharedWith, "reader");
112
112
  }
113
113
 
114
- // @ts-expect-error - AnyCoMapSchema doesn't have static methods
114
+ // @ts-expect-error - CoreCoMapSchema doesn't have static methods
115
115
  return schema.create(value ?? {}, group);
116
116
  }
117
117
 
@@ -129,7 +129,7 @@ async function serializeMessagePayload({
129
129
  }: {
130
130
  // Skipping type validation here to avoid excessive type complexity that affects the typecheck performance
131
131
  type: "request" | "response";
132
- schema: AnyCoMapSchema;
132
+ schema: CoreCoMapSchema;
133
133
  resolve: any;
134
134
  value: any;
135
135
  owner: Account;
@@ -198,7 +198,7 @@ async function handleMessagePayload({
198
198
  }: {
199
199
  type: "request" | "response";
200
200
  // Skipping type validation here to avoid excessive type complexity that affects the typecheck performance
201
- schema: AnyCoMapSchema;
201
+ schema: CoreCoMapSchema;
202
202
  resolve: any;
203
203
  request: unknown;
204
204
  loadAs: Account;
@@ -290,7 +290,9 @@ async function handleMessagePayload({
290
290
  throw new JazzRequestError("Creator account not found", 400);
291
291
  }
292
292
 
293
- const coSchema = anySchemaToCoSchema(schema) as CoValueClass<CoMap>;
293
+ const coSchema = coValueClassFromCoValueClassOrSchema(
294
+ schema,
295
+ ) as CoValueClass<CoMap>;
294
296
  const value = await loadCoValue<CoMap, true>(coSchema, requestData.id, {
295
297
  resolve,
296
298
  loadAs,
@@ -317,13 +319,13 @@ function parseSchemaAndResolve<
317
319
  if ("schema" in options) {
318
320
  return {
319
321
  // Using a type cast to reduce the type complexity
320
- schema: coMapDefiner(options.schema) as AnyCoMapSchema,
322
+ schema: coMapDefiner(options.schema) as CoreCoMapSchema,
321
323
  resolve: options.resolve as any,
322
324
  };
323
325
  }
324
326
 
325
327
  return {
326
- schema: coMapDefiner(options) as AnyCoMapSchema,
328
+ schema: coMapDefiner(options) as CoreCoMapSchema,
327
329
  resolve: true as any,
328
330
  };
329
331
  }
@@ -335,11 +337,11 @@ class HttpRoute<
335
337
  ResponseResolve extends ResolveQuery<CoMapSchema<ResponseShape>> = any,
336
338
  > {
337
339
  private requestDefinition: {
338
- schema: AnyCoMapSchema;
340
+ schema: CoreCoMapSchema;
339
341
  resolve: any;
340
342
  };
341
343
  private responseDefinition: {
342
- schema: AnyCoMapSchema;
344
+ schema: CoreCoMapSchema;
343
345
  resolve: any;
344
346
  };
345
347
  private url: string;
@@ -6,8 +6,6 @@ import {
6
6
  CoValueClass,
7
7
  CoValueFromRaw,
8
8
  ID,
9
- RefsToResolve,
10
- RefsToResolveStrict,
11
9
  Resolved,
12
10
  SubscribeListenerOptions,
13
11
  SubscribeRestArgs,
@@ -16,6 +14,12 @@ import {
16
14
  subscribeToCoValueWithoutMe,
17
15
  } from "../internal.js";
18
16
 
17
+ /**
18
+ * Extends `SchemaUnion` with a non-abstract constructor.
19
+ */
20
+ export type SchemaUnionConcreteSubclass<V extends CoValue> =
21
+ typeof SchemaUnion & CoValueClass<V>;
22
+
19
23
  /**
20
24
  * SchemaUnion allows you to create union types of CoValues that can be discriminated at runtime.
21
25
  *
@@ -86,7 +90,7 @@ export abstract class SchemaUnion extends CoValueBase implements CoValue {
86
90
  **/
87
91
  static Of<V extends CoValue>(
88
92
  discriminator: (raw: V["_raw"]) => CoValueClass<V> & CoValueFromRaw<V>,
89
- ): CoValueClass<V> & typeof SchemaUnion {
93
+ ): SchemaUnionConcreteSubclass<V> {
90
94
  return class SchemaUnionClass extends SchemaUnion {
91
95
  static override fromRaw<T extends CoValue>(
92
96
  this: CoValueClass<T> & CoValueFromRaw<T>,
@@ -97,7 +101,7 @@ export abstract class SchemaUnion extends CoValueBase implements CoValue {
97
101
  ) as unknown as CoValueClass<T> & CoValueFromRaw<T>;
98
102
  return ResolvedClass.fromRaw(raw);
99
103
  }
100
- } as unknown as CoValueClass<V> & typeof SchemaUnion;
104
+ } as unknown as SchemaUnionConcreteSubclass<V>;
101
105
  }
102
106
 
103
107
  /**
@@ -35,7 +35,6 @@ export type {
35
35
  TextPos,
36
36
  AccountClass,
37
37
  AccountCreationProps,
38
- WithHelpers,
39
38
  } from "./internal.js";
40
39
 
41
40
  export {
@@ -49,7 +48,6 @@ export {
49
48
  CoRichText,
50
49
  Account,
51
50
  isControlledAccount,
52
- createCoValueObservable,
53
51
  loadCoValue,
54
52
  subscribeToCoValue,
55
53
  ImageDefinition,
@@ -99,26 +97,17 @@ export {
99
97
  export type * from "./types.js";
100
98
 
101
99
  export {
102
- zodSchemaToCoSchema,
103
- anySchemaToCoSchema,
100
+ coValueClassFromCoValueClassOrSchema,
104
101
  type InstanceOfSchema,
105
102
  type InstanceOfSchemaCoValuesNullable,
106
- type CoValueOrZodSchema,
103
+ type CoValueClassOrSchema,
107
104
  type Loaded,
108
105
  type BaseAccountShape,
109
106
  type DefaultAccountShape,
110
- type AccountSchema,
111
- type AnyAccountSchema,
112
- type CoListSchema,
113
- type CoMapSchema,
114
- type CoFeedSchema,
115
- type PlainTextSchema,
116
- type FileStreamSchema,
107
+ type CoreAccountSchema as AnyAccountSchema,
117
108
  type ResolveQuery,
118
109
  type ResolveQueryStrict,
119
110
  type InitFor,
120
- type CoRecordSchema,
121
- type CoProfileSchema,
122
111
  } from "./internal.js";
123
112
 
124
113
  export {
@@ -1,7 +1,6 @@
1
1
  import {
2
2
  AgentSecret,
3
3
  CoID,
4
- ControlledAgent,
5
4
  CryptoProvider,
6
5
  LocalNode,
7
6
  Peer,
@@ -14,12 +13,11 @@ import { AuthSecretStorage } from "../auth/AuthSecretStorage.js";
14
13
  import { type Account, type AccountClass } from "../coValues/account.js";
15
14
  import { RegisteredSchemas } from "../coValues/registeredSchemas.js";
16
15
  import {
17
- type AccountSchema,
18
- type AnyAccountSchema,
19
16
  CoValueFromRaw,
17
+ type CoreAccountSchema,
20
18
  type ID,
21
19
  type InstanceOfSchema,
22
- anySchemaToCoSchema,
20
+ coValueClassFromCoValueClassOrSchema,
23
21
  } from "../internal.js";
24
22
  import { AuthCredentials, NewAccountProps } from "../types.js";
25
23
  import { activeAccountContext } from "./activeAccountContext.js";
@@ -89,7 +87,7 @@ export type JazzContext<Acc extends Account> =
89
87
  export async function createJazzContextFromExistingCredentials<
90
88
  S extends
91
89
  | (AccountClass<Account> & CoValueFromRaw<Account>)
92
- | AnyAccountSchema,
90
+ | CoreAccountSchema,
93
91
  >({
94
92
  credentials,
95
93
  peersToLoadFrom,
@@ -115,7 +113,8 @@ export async function createJazzContextFromExistingCredentials<
115
113
  const CurrentAccountSchema =
116
114
  PropsAccountSchema ?? (RegisteredSchemas["Account"] as unknown as S);
117
115
 
118
- const AccountClass = anySchemaToCoSchema(CurrentAccountSchema);
116
+ const AccountClass =
117
+ coValueClassFromCoValueClassOrSchema(CurrentAccountSchema);
119
118
 
120
119
  const node = await LocalNode.withLoadedAccount({
121
120
  accountID: credentials.accountID as unknown as CoID<RawAccount>,
@@ -153,7 +152,7 @@ export async function createJazzContextFromExistingCredentials<
153
152
  export async function createJazzContextForNewAccount<
154
153
  S extends
155
154
  | (AccountClass<Account> & CoValueFromRaw<Account>)
156
- | AnyAccountSchema,
155
+ | CoreAccountSchema,
157
156
  >({
158
157
  creationProps,
159
158
  initialAgentSecret,
@@ -174,7 +173,8 @@ export async function createJazzContextForNewAccount<
174
173
  const CurrentAccountSchema =
175
174
  PropsAccountSchema ?? (RegisteredSchemas["Account"] as unknown as S);
176
175
 
177
- const AccountClass = anySchemaToCoSchema(CurrentAccountSchema);
176
+ const AccountClass =
177
+ coValueClassFromCoValueClassOrSchema(CurrentAccountSchema);
178
178
 
179
179
  const { node } = await LocalNode.withNewlyCreatedAccount({
180
180
  creationProps,
@@ -209,7 +209,7 @@ export async function createJazzContextForNewAccount<
209
209
  export async function createJazzContext<
210
210
  S extends
211
211
  | (AccountClass<Account> & CoValueFromRaw<Account>)
212
- | AnyAccountSchema,
212
+ | CoreAccountSchema,
213
213
  >(options: {
214
214
  credentials?: AuthCredentials;
215
215
  newAccountProps?: NewAccountProps;
@@ -3,7 +3,7 @@ import { Account } from "../coValues/account.js";
3
3
  import type {
4
4
  CoValue,
5
5
  CoValueClass,
6
- CoValueOrZodSchema,
6
+ CoValueClassOrSchema,
7
7
  ID,
8
8
  } from "../internal.js";
9
9
 
@@ -68,7 +68,7 @@ export function parseInviteLink(inviteURL: string):
68
68
  }
69
69
 
70
70
  /** @category Invite Links */
71
- export function consumeInviteLink<S extends CoValueOrZodSchema>({
71
+ export function consumeInviteLink<S extends CoValueClassOrSchema>({
72
72
  inviteURL,
73
73
  as = Account.getMe(),
74
74
  forValueHint,
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Remove getters from an object
3
+ *
4
+ * @param obj - The object to remove getters from.
5
+ * @returns A new object with the getters removed.
6
+ */
7
+ export function removeGetters<T extends object>(obj: T): Partial<T> {
8
+ const result: any = {};
9
+
10
+ for (const key of Object.keys(obj)) {
11
+ const descriptor = Object.getOwnPropertyDescriptor(obj, key);
12
+ if (!descriptor?.get) {
13
+ result[key] = (obj as any)[key];
14
+ }
15
+ }
16
+
17
+ return result;
18
+ }
@@ -1,4 +1,17 @@
1
1
  export { Loaded as loaded } from "./zodSchema.js";
2
+ export { CoMapSchema as Map } from "./schemaTypes/CoMapSchema.js";
3
+ export { CoRecordSchema as Record } from "./schemaTypes/CoRecordSchema.js";
4
+ export { CoListSchema as List } from "./schemaTypes/CoListSchema.js";
5
+ export { CoFeedSchema as Feed } from "./schemaTypes/CoFeedSchema.js";
6
+ export { PlainTextSchema as PlainText } from "./schemaTypes/PlainTextSchema.js";
7
+ export { RichTextSchema as RichText } from "./schemaTypes/RichTextSchema.js";
8
+ export { FileStreamSchema as FileStream } from "./schemaTypes/FileStreamSchema.js";
9
+ export {
10
+ AccountSchema as Account,
11
+ CoProfileSchema as Profile,
12
+ } from "./schemaTypes/AccountSchema.js";
13
+ export { CoOptionalSchema as Optional } from "./schemaTypes/CoOptionalSchema.js";
14
+ export { CoDiscriminatedUnionSchema as DiscriminatedUnion } from "./schemaTypes/CoDiscriminatedUnionSchema.js";
2
15
  export {
3
16
  coMapDefiner as map,
4
17
  coRecordDefiner as record,
@@ -12,4 +25,5 @@ export {
12
25
  coProfileDefiner as profile,
13
26
  coOptionalDefiner as optional,
14
27
  coDiscriminatedUnionDefiner as discriminatedUnion,
28
+ ImageDefinitionSchema as Image,
15
29
  } from "./zodCo.js";
@@ -0,0 +1,156 @@
1
+ import { RawCoList, RawCoMap } from "cojson";
2
+ import {
3
+ Account,
4
+ CoDiscriminatedUnionSchema,
5
+ CoFeed,
6
+ CoFeedSchema,
7
+ CoList,
8
+ CoListSchema,
9
+ CoMap,
10
+ CoPlainText,
11
+ CoRichText,
12
+ CoValueClass,
13
+ FileStream,
14
+ FileStreamSchema,
15
+ PlainTextSchema,
16
+ SchemaUnion,
17
+ enrichAccountSchema,
18
+ enrichCoMapSchema,
19
+ isCoValueClass,
20
+ } from "../../../internal.js";
21
+ import { coField } from "../../schema.js";
22
+
23
+ import { CoreCoValueSchema } from "../schemaTypes/CoValueSchema.js";
24
+ import { RichTextSchema } from "../schemaTypes/RichTextSchema.js";
25
+ import { schemaUnionDiscriminatorFor } from "../unionUtils.js";
26
+ import {
27
+ AnyCoreCoValueSchema,
28
+ AnyZodOrCoValueSchema,
29
+ CoValueClassFromAnySchema,
30
+ CoValueClassOrSchema,
31
+ CoValueSchemaFromCoreSchema,
32
+ } from "../zodSchema.js";
33
+ import {
34
+ SchemaField,
35
+ schemaFieldToCoFieldDef,
36
+ } from "./schemaFieldToCoFieldDef.js";
37
+
38
+ // Note: if you're editing this function, edit the `isAnyCoValueSchema`
39
+ // function in `zodReExport.ts` as well
40
+ export function isAnyCoValueSchema(
41
+ schema: AnyZodOrCoValueSchema | CoValueClass,
42
+ ): schema is AnyCoreCoValueSchema {
43
+ return "collaborative" in schema && schema.collaborative === true;
44
+ }
45
+
46
+ export function isCoValueSchema(
47
+ schema: AnyZodOrCoValueSchema | CoValueClass,
48
+ ): schema is CoValueSchemaFromCoreSchema<AnyCoreCoValueSchema> {
49
+ return isAnyCoValueSchema(schema) && "getCoValueClass" in schema;
50
+ }
51
+
52
+ /**
53
+ * Convert a "core" CoValue schema into a CoValue schema.
54
+ * See {@link CoreCoValueSchema} for more information.
55
+ *
56
+ * @returns The CoValue schema matching the provided CoreCoValueSchema
57
+ */
58
+ export function hydrateCoreCoValueSchema<S extends AnyCoreCoValueSchema>(
59
+ schema: S,
60
+ ): CoValueSchemaFromCoreSchema<S> {
61
+ if (isCoValueSchema(schema)) {
62
+ // If the schema is already a CoValue schema, return it as is
63
+ return schema as any;
64
+ }
65
+
66
+ if (schema.builtin === "CoOptional") {
67
+ throw new Error(
68
+ `co.optional() of collaborative types is not supported as top-level schema: ${JSON.stringify(schema)}`,
69
+ );
70
+ } else if (schema.builtin === "CoMap" || schema.builtin === "Account") {
71
+ const def = schema.getDefinition();
72
+ const ClassToExtend = schema.builtin === "Account" ? Account : CoMap;
73
+
74
+ const coValueClass = class ZCoMap extends ClassToExtend {
75
+ constructor(options: { fromRaw: RawCoMap } | undefined) {
76
+ super(options);
77
+ for (const [fieldName, fieldType] of Object.entries(def.shape)) {
78
+ (this as any)[fieldName] = schemaFieldToCoFieldDef(
79
+ fieldType as SchemaField,
80
+ );
81
+ }
82
+ if (def.catchall) {
83
+ (this as any)[coField.items] = schemaFieldToCoFieldDef(
84
+ def.catchall as SchemaField,
85
+ );
86
+ }
87
+ }
88
+ };
89
+
90
+ const coValueSchema =
91
+ ClassToExtend === Account
92
+ ? enrichAccountSchema(schema as any, coValueClass as any)
93
+ : enrichCoMapSchema(schema as any, coValueClass as any);
94
+
95
+ return coValueSchema as unknown as CoValueSchemaFromCoreSchema<S>;
96
+ } else if (schema.builtin === "CoList") {
97
+ const element = schema.element;
98
+ const coValueClass = class ZCoList extends CoList {
99
+ constructor(options: { fromRaw: RawCoList } | undefined) {
100
+ super(options);
101
+ (this as any)[coField.items] = schemaFieldToCoFieldDef(
102
+ element as SchemaField,
103
+ );
104
+ }
105
+ };
106
+
107
+ const coValueSchema = new CoListSchema(element, coValueClass as any);
108
+
109
+ return coValueSchema as unknown as CoValueSchemaFromCoreSchema<S>;
110
+ } else if (schema.builtin === "CoFeed") {
111
+ const coValueClass = CoFeed.Of(
112
+ schemaFieldToCoFieldDef(schema.element as SchemaField),
113
+ );
114
+ const coValueSchema = new CoFeedSchema(schema.element, coValueClass);
115
+ return coValueSchema as unknown as CoValueSchemaFromCoreSchema<S>;
116
+ } else if (schema.builtin === "FileStream") {
117
+ const coValueClass = FileStream;
118
+ return new FileStreamSchema(coValueClass) as CoValueSchemaFromCoreSchema<S>;
119
+ } else if (schema.builtin === "CoPlainText") {
120
+ const coValueClass = CoPlainText;
121
+ return new PlainTextSchema(coValueClass) as CoValueSchemaFromCoreSchema<S>;
122
+ } else if (schema.builtin === "CoRichText") {
123
+ const coValueClass = CoRichText;
124
+ return new RichTextSchema(coValueClass) as CoValueSchemaFromCoreSchema<S>;
125
+ } else if (schema.builtin === "CoDiscriminatedUnion") {
126
+ const coValueClass = SchemaUnion.Of(schemaUnionDiscriminatorFor(schema));
127
+ const coValueSchema = new CoDiscriminatedUnionSchema(schema, coValueClass);
128
+ return coValueSchema as CoValueSchemaFromCoreSchema<S>;
129
+ } else {
130
+ const notReachable: never = schema;
131
+ throw new Error(
132
+ `Unsupported zod CoValue type for top-level schema: ${JSON.stringify(notReachable, undefined, 2)}`,
133
+ );
134
+ }
135
+ }
136
+
137
+ /**
138
+ * Convert a CoValue class or a CoValue schema into a CoValue class.
139
+ *
140
+ * This function bridges the gap between CoValue classes created with the class syntax,
141
+ * and CoValue classes created with our `co.` definers.
142
+ *
143
+ * @param schema A CoValue class or a CoValue schema
144
+ * @returns The same CoValue class, or a CoValue class that matches the provided schema
145
+ */
146
+ export function coValueClassFromCoValueClassOrSchema<
147
+ S extends CoValueClassOrSchema,
148
+ >(schema: S): CoValueClassFromAnySchema<S> {
149
+ if (isCoValueClass(schema)) {
150
+ return schema as any;
151
+ } else if (isCoValueSchema(schema)) {
152
+ return schema.getCoValueClass() as any;
153
+ }
154
+
155
+ throw new Error(`Unsupported schema: ${JSON.stringify(schema)}`);
156
+ }