jazz-tools 0.15.15 → 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 (217) 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 -50
  7. package/CHANGELOG.md +35 -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-4CFNXQE7.js → chunk-MLCNE3TL.js} +791 -698
  15. package/dist/chunk-MLCNE3TL.js.map +1 -0
  16. package/dist/index.js +363 -11
  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 +2 -0
  21. package/dist/react/index.js.map +1 -1
  22. package/dist/react/testing.js +3 -1
  23. package/dist/react/testing.js.map +1 -1
  24. package/dist/react-core/hooks.d.ts +2 -2
  25. package/dist/react-core/hooks.d.ts.map +1 -1
  26. package/dist/react-core/index.js +3 -3
  27. package/dist/react-core/index.js.map +1 -1
  28. package/dist/react-native-core/hooks.d.ts +2 -2
  29. package/dist/react-native-core/hooks.d.ts.map +1 -1
  30. package/dist/react-native-core/index.js.map +1 -1
  31. package/dist/svelte/jazz.class.svelte.d.ts +2 -2
  32. package/dist/svelte/jazz.class.svelte.d.ts.map +1 -1
  33. package/dist/svelte/jazz.class.svelte.js +5 -5
  34. package/dist/svelte/jazz.svelte.d.ts +2 -2
  35. package/dist/svelte/jazz.svelte.d.ts.map +1 -1
  36. package/dist/testing.js +3 -3
  37. package/dist/testing.js.map +1 -1
  38. package/dist/tools/coValues/CoValueBase.d.ts +3 -13
  39. package/dist/tools/coValues/CoValueBase.d.ts.map +1 -1
  40. package/dist/tools/coValues/account.d.ts +2 -2
  41. package/dist/tools/coValues/account.d.ts.map +1 -1
  42. package/dist/tools/coValues/coFeed.d.ts.map +1 -1
  43. package/dist/tools/coValues/coList.d.ts.map +1 -1
  44. package/dist/tools/coValues/coMap.d.ts +5 -18
  45. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  46. package/dist/tools/coValues/deepLoading.d.ts +4 -1
  47. package/dist/tools/coValues/deepLoading.d.ts.map +1 -1
  48. package/dist/tools/coValues/extensions/imageDef.d.ts +4 -7
  49. package/dist/tools/coValues/extensions/imageDef.d.ts.map +1 -1
  50. package/dist/tools/coValues/group.d.ts +1 -0
  51. package/dist/tools/coValues/group.d.ts.map +1 -1
  52. package/dist/tools/coValues/inbox.d.ts +2 -2
  53. package/dist/tools/coValues/inbox.d.ts.map +1 -1
  54. package/dist/tools/coValues/interfaces.d.ts +58 -17
  55. package/dist/tools/coValues/interfaces.d.ts.map +1 -1
  56. package/dist/tools/coValues/request.d.ts +82 -0
  57. package/dist/tools/coValues/request.d.ts.map +1 -0
  58. package/dist/tools/coValues/schemaUnion.d.ts +5 -1
  59. package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
  60. package/dist/tools/exports.d.ts +4 -3
  61. package/dist/tools/exports.d.ts.map +1 -1
  62. package/dist/tools/implementation/createContext.d.ts +4 -4
  63. package/dist/tools/implementation/createContext.d.ts.map +1 -1
  64. package/dist/tools/implementation/invites.d.ts +2 -2
  65. package/dist/tools/implementation/invites.d.ts.map +1 -1
  66. package/dist/tools/implementation/schemaUtils.d.ts +8 -0
  67. package/dist/tools/implementation/schemaUtils.d.ts.map +1 -0
  68. package/dist/tools/implementation/zodSchema/coExport.d.ts +11 -1
  69. package/dist/tools/implementation/zodSchema/coExport.d.ts.map +1 -1
  70. package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts +22 -0
  71. package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts.map +1 -0
  72. package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts +10 -0
  73. package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts.map +1 -0
  74. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +11 -11
  75. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
  76. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +34 -24
  77. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -1
  78. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +16 -14
  79. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  80. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +23 -17
  81. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  82. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +24 -16
  83. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  84. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts +20 -9
  85. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts.map +1 -1
  86. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +18 -12
  87. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  88. package/dist/tools/implementation/zodSchema/schemaTypes/CoValueSchema.d.ts +18 -0
  89. package/dist/tools/implementation/zodSchema/schemaTypes/CoValueSchema.d.ts.map +1 -0
  90. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +14 -9
  91. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  92. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +14 -9
  93. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
  94. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +14 -9
  95. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
  96. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +15 -13
  97. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
  98. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +12 -15
  99. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -1
  100. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +17 -20
  101. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -1
  102. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +17 -20
  103. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -1
  104. package/dist/tools/implementation/zodSchema/unionUtils.d.ts +3 -5
  105. package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
  106. package/dist/tools/implementation/zodSchema/zodCo.d.ts +10 -8
  107. package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
  108. package/dist/tools/implementation/zodSchema/zodReExport.d.ts +4 -8
  109. package/dist/tools/implementation/zodSchema/zodReExport.d.ts.map +1 -1
  110. package/dist/tools/implementation/zodSchema/zodSchema.d.ts +21 -30
  111. package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
  112. package/dist/tools/internal.d.ts +3 -2
  113. package/dist/tools/internal.d.ts.map +1 -1
  114. package/dist/tools/lib/id.d.ts +2 -0
  115. package/dist/tools/lib/id.d.ts.map +1 -0
  116. package/dist/tools/lib/utilityTypes.d.ts +10 -0
  117. package/dist/tools/lib/utilityTypes.d.ts.map +1 -0
  118. package/dist/tools/subscribe/SubscriptionScope.d.ts +3 -2
  119. package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
  120. package/dist/tools/subscribe/utils.d.ts.map +1 -1
  121. package/dist/tools/testing.d.ts +2 -2
  122. package/dist/tools/testing.d.ts.map +1 -1
  123. package/dist/tools/tests/exportImport.test.d.ts +2 -0
  124. package/dist/tools/tests/exportImport.test.d.ts.map +1 -0
  125. package/dist/tools/tests/request.test.d.ts +2 -0
  126. package/dist/tools/tests/request.test.d.ts.map +1 -0
  127. package/dist/worker/index.d.ts.map +1 -1
  128. package/dist/worker/index.js +2 -2
  129. package/dist/worker/index.js.map +1 -1
  130. package/package.json +7 -6
  131. package/src/browser/index.ts +2 -4
  132. package/src/browser-media-images/index.ts +1 -1
  133. package/src/react/hooks.tsx +2 -2
  134. package/src/react-core/hooks.ts +6 -6
  135. package/src/react-core/tests/useAccount.test.ts +2 -2
  136. package/src/react-core/tests/useCoState.test.ts +3 -2
  137. package/src/react-native-core/hooks.tsx +2 -2
  138. package/src/svelte/jazz.class.svelte.ts +10 -7
  139. package/src/svelte/jazz.svelte.ts +2 -2
  140. package/src/tools/coValues/CoValueBase.ts +8 -20
  141. package/src/tools/coValues/account.ts +18 -14
  142. package/src/tools/coValues/coFeed.ts +0 -4
  143. package/src/tools/coValues/coList.ts +7 -9
  144. package/src/tools/coValues/coMap.ts +1 -6
  145. package/src/tools/coValues/coPlainText.ts +4 -4
  146. package/src/tools/coValues/deepLoading.ts +4 -1
  147. package/src/tools/coValues/extensions/imageDef.ts +3 -3
  148. package/src/tools/coValues/group.ts +1 -0
  149. package/src/tools/coValues/inbox.ts +8 -7
  150. package/src/tools/coValues/interfaces.ts +177 -69
  151. package/src/tools/coValues/request.ts +633 -0
  152. package/src/tools/coValues/schemaUnion.ts +8 -4
  153. package/src/tools/exports.ts +11 -14
  154. package/src/tools/implementation/createContext.ts +9 -9
  155. package/src/tools/implementation/invites.ts +2 -2
  156. package/src/tools/implementation/schemaUtils.ts +18 -0
  157. package/src/tools/implementation/zodSchema/coExport.ts +14 -0
  158. package/src/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.ts +156 -0
  159. package/src/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.ts +133 -0
  160. package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +36 -17
  161. package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +101 -52
  162. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +54 -50
  163. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +54 -46
  164. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +187 -137
  165. package/src/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.ts +29 -27
  166. package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +28 -18
  167. package/src/tools/implementation/zodSchema/schemaTypes/CoValueSchema.ts +18 -0
  168. package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +45 -36
  169. package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +47 -35
  170. package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +43 -30
  171. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +28 -23
  172. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +28 -25
  173. package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +86 -78
  174. package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +91 -82
  175. package/src/tools/implementation/zodSchema/unionUtils.ts +47 -51
  176. package/src/tools/implementation/zodSchema/zodCo.ts +42 -75
  177. package/src/tools/implementation/zodSchema/zodReExport.ts +44 -24
  178. package/src/tools/implementation/zodSchema/zodSchema.ts +64 -102
  179. package/src/tools/internal.ts +3 -2
  180. package/src/tools/lib/id.ts +3 -0
  181. package/src/tools/lib/utilityTypes.ts +7 -0
  182. package/src/tools/subscribe/SubscriptionScope.ts +10 -1
  183. package/src/tools/subscribe/utils.ts +4 -2
  184. package/src/tools/testing.ts +4 -4
  185. package/src/tools/tests/ContextManager.test.ts +8 -9
  186. package/src/tools/tests/account.test.ts +65 -3
  187. package/src/tools/tests/coDiscriminatedUnion.test-d.ts +38 -0
  188. package/src/tools/tests/coDiscriminatedUnion.test.ts +219 -1
  189. package/src/tools/tests/coFeed.test-d.ts +4 -3
  190. package/src/tools/tests/coList.test-d.ts +32 -3
  191. package/src/tools/tests/coList.test.ts +20 -2
  192. package/src/tools/tests/coMap.record.test-d.ts +31 -3
  193. package/src/tools/tests/coMap.record.test.ts +9 -9
  194. package/src/tools/tests/coMap.test-d.ts +8 -8
  195. package/src/tools/tests/coMap.test.ts +19 -5
  196. package/src/tools/tests/coOptional.test.ts +63 -1
  197. package/src/tools/tests/createContext.test.ts +7 -9
  198. package/src/tools/tests/deepLoading.test.ts +4 -10
  199. package/src/tools/tests/exportImport.test.ts +526 -0
  200. package/src/tools/tests/groupsAndAccounts.test.ts +5 -4
  201. package/src/tools/tests/inbox.test.ts +3 -2
  202. package/src/tools/tests/load.test.ts +3 -29
  203. package/src/tools/tests/request.test.ts +951 -0
  204. package/src/tools/tests/schemaUnion.test.ts +2 -2
  205. package/src/tools/tests/subscribe.test.ts +22 -114
  206. package/src/tools/tests/testing.test.ts +6 -6
  207. package/src/tools/tests/zod.test-d.ts +27 -0
  208. package/src/tools/tests/zod.test.ts +50 -45
  209. package/src/worker/index.ts +0 -1
  210. package/tsup.config.ts +0 -2
  211. package/dist/chunk-4CFNXQE7.js.map +0 -1
  212. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +0 -12
  213. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +0 -1
  214. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +0 -9
  215. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +0 -1
  216. package/src/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +0 -172
  217. package/src/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +0 -218
@@ -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
+ }
@@ -0,0 +1,133 @@
1
+ import { CoValueClass, isCoValueClass } from "../../../internal.js";
2
+ import { coField } from "../../schema.js";
3
+ import { CoreCoValueSchema } from "../schemaTypes/CoValueSchema.js";
4
+ import { isUnionOfPrimitivesDeeply } from "../unionUtils.js";
5
+ import {
6
+ ZodCatch,
7
+ ZodDefault,
8
+ ZodLazy,
9
+ ZodReadonly,
10
+ z,
11
+ } from "../zodReExport.js";
12
+ import { ZodPrimitiveSchema } from "../zodSchema.js";
13
+ import { isCoValueSchema } from "./coValueSchemaTransformation.js";
14
+
15
+ /**
16
+ * Types of objects that can be nested inside CoValue schema containers
17
+ */
18
+ export type SchemaField =
19
+ // Schemas created with co.map(), co.record(), co.list(), etc.
20
+ | CoreCoValueSchema
21
+ // CoValue classes created with class syntax, or framework-provided classes like Group
22
+ | CoValueClass
23
+ | ZodPrimitiveSchema
24
+ | z.core.$ZodOptional<z.core.$ZodType>
25
+ | z.core.$ZodUnion<z.core.$ZodType[]>
26
+ | z.core.$ZodDiscriminatedUnion<z.core.$ZodType[]>
27
+ | z.core.$ZodObject<z.core.$ZodLooseShape>
28
+ | z.core.$ZodArray<z.core.$ZodType>
29
+ | z.core.$ZodTuple<z.core.$ZodType[]>
30
+ | z.core.$ZodReadonly<z.core.$ZodType>
31
+ | z.core.$ZodLazy<z.core.$ZodType>
32
+ | z.core.$ZodTemplateLiteral<any>
33
+ | z.core.$ZodLiteral<any>
34
+ | z.core.$ZodCatch<z.core.$ZodType>
35
+ | z.core.$ZodEnum<any>
36
+ | z.core.$ZodDefault<z.core.$ZodType>
37
+ | z.core.$ZodCatch<z.core.$ZodType>;
38
+
39
+ export function schemaFieldToCoFieldDef(schema: SchemaField) {
40
+ if (isCoValueClass(schema)) {
41
+ return coField.ref(schema);
42
+ } else if (isCoValueSchema(schema)) {
43
+ if (schema.builtin === "CoOptional") {
44
+ return coField.ref(schema.getCoValueClass(), {
45
+ optional: true,
46
+ });
47
+ }
48
+ return coField.ref(schema.getCoValueClass());
49
+ } else {
50
+ if ("_zod" in schema) {
51
+ const zodSchemaDef = schema._zod.def;
52
+ if (zodSchemaDef.type === "optional") {
53
+ const inner = zodSchemaDef.innerType as ZodPrimitiveSchema;
54
+ return schemaFieldToCoFieldDef(inner);
55
+ } else if (zodSchemaDef.type === "string") {
56
+ return coField.string;
57
+ } else if (zodSchemaDef.type === "number") {
58
+ return coField.number;
59
+ } else if (zodSchemaDef.type === "boolean") {
60
+ return coField.boolean;
61
+ } else if (zodSchemaDef.type === "null") {
62
+ return coField.null;
63
+ } else if (zodSchemaDef.type === "enum") {
64
+ return coField.string;
65
+ } else if (zodSchemaDef.type === "readonly") {
66
+ return schemaFieldToCoFieldDef(
67
+ (schema as unknown as ZodReadonly).def.innerType as SchemaField,
68
+ );
69
+ } else if (zodSchemaDef.type === "date") {
70
+ return coField.optional.Date;
71
+ } else if (zodSchemaDef.type === "template_literal") {
72
+ return coField.string;
73
+ } else if (zodSchemaDef.type === "lazy") {
74
+ // Mostly to support z.json()
75
+ return schemaFieldToCoFieldDef(
76
+ (schema as unknown as ZodLazy).unwrap() as SchemaField,
77
+ );
78
+ } else if (
79
+ zodSchemaDef.type === "default" ||
80
+ zodSchemaDef.type === "catch"
81
+ ) {
82
+ console.warn(
83
+ "z.default()/z.catch() are not supported in collaborative schemas. They will be ignored.",
84
+ );
85
+
86
+ return schemaFieldToCoFieldDef(
87
+ (schema as unknown as ZodDefault | ZodCatch).def
88
+ .innerType as SchemaField,
89
+ );
90
+ } else if (zodSchemaDef.type === "literal") {
91
+ if (
92
+ zodSchemaDef.values.some((literal) => typeof literal === "undefined")
93
+ ) {
94
+ throw new Error("z.literal() with undefined is not supported");
95
+ }
96
+ if (zodSchemaDef.values.some((literal) => literal === null)) {
97
+ throw new Error("z.literal() with null is not supported");
98
+ }
99
+ if (
100
+ zodSchemaDef.values.some((literal) => typeof literal === "bigint")
101
+ ) {
102
+ throw new Error("z.literal() with bigint is not supported");
103
+ }
104
+ return coField.literal(
105
+ ...(zodSchemaDef.values as Exclude<
106
+ (typeof zodSchemaDef.values)[number],
107
+ undefined | null | bigint
108
+ >[]),
109
+ );
110
+ } else if (
111
+ zodSchemaDef.type === "object" ||
112
+ zodSchemaDef.type === "array" ||
113
+ zodSchemaDef.type === "tuple"
114
+ ) {
115
+ return coField.json();
116
+ } else if (zodSchemaDef.type === "union") {
117
+ if (isUnionOfPrimitivesDeeply(schema)) {
118
+ return coField.json();
119
+ } else {
120
+ throw new Error(
121
+ "z.union()/z.discriminatedUnion() of collaborative types is not supported. Use co.discriminatedUnion() instead.",
122
+ );
123
+ }
124
+ } else {
125
+ throw new Error(
126
+ `Unsupported zod type: ${(schema._zod?.def as any)?.type || JSON.stringify(schema)}`,
127
+ );
128
+ }
129
+ } else {
130
+ throw new Error(`Unsupported zod type: ${schema}`);
131
+ }
132
+ }
133
+ }
@@ -9,8 +9,13 @@ import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
9
9
  import { InstanceOrPrimitiveOfSchema } from "../typeConverters/InstanceOrPrimitiveOfSchema.js";
10
10
  import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "../typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
11
11
  import { z } from "../zodReExport.js";
12
- import { Loaded, ResolveQuery } from "../zodSchema.js";
13
- import { AnyCoMapSchema, CoMapSchema } from "./CoMapSchema.js";
12
+ import { AnyZodOrCoValueSchema, Loaded, ResolveQuery } from "../zodSchema.js";
13
+ import {
14
+ CoMapSchema,
15
+ CoMapSchemaDefinition,
16
+ CoreCoMapSchema,
17
+ createCoreCoMapSchema,
18
+ } from "./CoMapSchema.js";
14
19
 
15
20
  export type BaseProfileShape = {
16
21
  name: z.core.$ZodString<string>;
@@ -19,8 +24,8 @@ export type BaseProfileShape = {
19
24
  };
20
25
 
21
26
  export type BaseAccountShape = {
22
- profile: AnyCoMapSchema<BaseProfileShape>;
23
- root: AnyCoMapSchema;
27
+ profile: CoreCoMapSchema<BaseProfileShape>;
28
+ root: CoreCoMapSchema;
24
29
  };
25
30
 
26
31
  export type DefaultAccountShape = {
@@ -28,9 +33,18 @@ export type DefaultAccountShape = {
28
33
  root: CoMapSchema<{}>;
29
34
  };
30
35
 
31
- export type AccountSchema<
36
+ export interface AccountSchema<
32
37
  Shape extends BaseAccountShape = DefaultAccountShape,
33
- > = Omit<CoMapSchema<Shape>, "create" | "load" | "withMigration"> & {
38
+ > extends CoreAccountSchema<Shape>,
39
+ Omit<
40
+ CoMapSchema<Shape>,
41
+ | "builtin"
42
+ | "getDefinition"
43
+ | "create"
44
+ | "load"
45
+ | "withMigration"
46
+ | "getCoValueClass"
47
+ > {
34
48
  builtin: "Account";
35
49
 
36
50
  create: (options: {
@@ -63,10 +77,19 @@ export type AccountSchema<
63
77
  ): AccountSchema<Shape>;
64
78
 
65
79
  getCoValueClass: () => typeof Account;
66
- };
80
+ }
81
+
82
+ export function createCoreAccountSchema<Shape extends BaseAccountShape>(
83
+ shape: Shape,
84
+ ): CoreAccountSchema<Shape> {
85
+ return {
86
+ ...createCoreCoMapSchema(shape),
87
+ builtin: "Account" as const,
88
+ };
89
+ }
67
90
 
68
91
  export function enrichAccountSchema<Shape extends BaseAccountShape>(
69
- schema: AnyAccountSchema<Shape>,
92
+ schema: CoreAccountSchema<Shape>,
70
93
  coValueClass: typeof Account,
71
94
  ): AccountSchema<Shape> {
72
95
  const enrichedSchema = Object.assign(schema, {
@@ -90,9 +113,6 @@ export function enrichAccountSchema<Shape extends BaseAccountShape>(
90
113
  // @ts-expect-error
91
114
  return coValueClass.subscribe(...args);
92
115
  },
93
- withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
94
- return Object.assign(schema, helpers(schema));
95
- },
96
116
  fromRaw: (...args: any[]) => {
97
117
  // @ts-expect-error
98
118
  return coValueClass.fromRaw(...args);
@@ -127,16 +147,15 @@ export type DefaultProfileShape = {
127
147
 
128
148
  export type CoProfileSchema<
129
149
  Shape extends z.core.$ZodLooseShape = DefaultProfileShape,
130
- Config extends z.core.$ZodObjectConfig = z.core.$ZodObjectConfig,
131
- > = CoMapSchema<Shape & DefaultProfileShape, Config, Group>;
150
+ CatchAll extends AnyZodOrCoValueSchema | unknown = unknown,
151
+ > = CoMapSchema<Shape & DefaultProfileShape, CatchAll, Group>;
132
152
 
133
153
  // less precise version to avoid circularity issues and allow matching against
134
- export type AnyAccountSchema<
154
+ export interface CoreAccountSchema<
135
155
  Shape extends z.core.$ZodLooseShape = z.core.$ZodLooseShape,
136
- > = z.core.$ZodObject<Shape> & {
137
- collaborative: true;
156
+ > extends Omit<CoreCoMapSchema<Shape>, "builtin"> {
138
157
  builtin: "Account";
139
- };
158
+ }
140
159
 
141
160
  export type AccountInstance<Shape extends z.core.$ZodLooseShape> = {
142
161
  -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<Shape[key]>;