jazz-tools 0.13.31 → 0.14.1

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 +22 -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-WLOZKDOH.js} +3536 -3291
  7. package/dist/chunk-WLOZKDOH.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 +88 -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
package/src/exports.ts CHANGED
@@ -8,53 +8,56 @@ export type {
8
8
  SyncMessage,
9
9
  } from "cojson";
10
10
 
11
+ export { z } from "zod/v4";
12
+
11
13
  export type { CoValue, ID } from "./internal.js";
12
14
 
13
- export { Encoders, co } from "./internal.js";
15
+ export { Encoders, coField } from "./internal.js";
14
16
 
15
- export { Inbox, InboxSender } from "./coValues/inbox.js";
17
+ export { Inbox, InboxSender } from "./internal.js";
16
18
 
17
- export {
18
- Account,
19
- isControlledAccount,
20
- type AccountClass,
21
- type AccountCreationProps,
22
- } from "./coValues/account.js";
23
- export {
24
- BinaryCoStream,
25
- CoFeed,
26
- CoStream,
27
- type CoFeedEntry,
28
- FileStream,
29
- } from "./coValues/coFeed.js";
30
- export { CoList } from "./coValues/coList.js";
31
- export { CoMap, type CoMapInit } from "./coValues/coMap.js";
32
- export { CoPlainText, type TextPos } from "./coValues/coPlainText.js";
33
- export { CoRichText } from "./coValues/coRichText.js";
34
- export { ImageDefinition } from "./coValues/extensions/imageDef.js";
35
- export { Group } from "./coValues/group.js";
36
- export { CoValueBase } from "./coValues/interfaces.js";
37
- export { Profile } from "./coValues/profile.js";
38
- export { SchemaUnion } from "./coValues/schemaUnion.js";
19
+ export { Group } from "./internal.js";
20
+ export { CoValueBase } from "./internal.js";
21
+ export { Profile } from "./internal.js";
22
+ export { SchemaUnion } from "./internal.js";
23
+
24
+ export { co } from "./internal.js";
39
25
 
40
26
  export type {
41
27
  CoValueClass,
28
+ CoValueFromRaw,
42
29
  DeeplyLoaded,
43
30
  Resolved,
44
31
  RefsToResolve,
45
32
  RefsToResolveStrict,
33
+ CoMapInit,
34
+ CoFeedEntry,
35
+ TextPos,
36
+ AccountClass,
37
+ AccountCreationProps,
46
38
  } from "./internal.js";
47
39
 
48
40
  export {
41
+ CoMap,
42
+ CoList,
43
+ BinaryCoStream,
44
+ CoFeed,
45
+ CoStream,
46
+ FileStream,
47
+ CoPlainText,
48
+ CoRichText,
49
+ Account,
50
+ isControlledAccount,
49
51
  createCoValueObservable,
50
52
  loadCoValue,
51
53
  subscribeToCoValue,
54
+ ImageDefinition,
52
55
  } from "./internal.js";
53
56
 
54
57
  export {
55
58
  JazzContextManager,
56
59
  type JazzContextManagerAuthProps,
57
- } from "./implementation/ContextManager.js";
60
+ } from "./internal.js";
58
61
 
59
62
  export { AuthSecretStorage } from "./auth/AuthSecretStorage.js";
60
63
  export { KvStoreContext, type KvStore } from "./auth/KvStoreContext.js";
@@ -81,3 +84,22 @@ export {
81
84
  } from "./internal.js";
82
85
 
83
86
  export type * from "./types.js";
87
+
88
+ export {
89
+ zodSchemaToCoSchema,
90
+ anySchemaToCoSchema,
91
+ type InstanceOfSchema,
92
+ type InstanceOfSchemaCoValuesNullable,
93
+ type CoValueOrZodSchema,
94
+ type Loaded,
95
+ type AccountSchema,
96
+ type AnyAccountSchema,
97
+ type CoListSchema,
98
+ type CoMapSchema,
99
+ type CoFeedSchema,
100
+ type PlainTextSchema,
101
+ type FileStreamSchema,
102
+ type ResolveQuery,
103
+ type ResolveQueryStrict,
104
+ type InitFor,
105
+ } from "./internal.js";
@@ -1,4 +1,4 @@
1
- import type { Account } from "../coValues/account.js";
1
+ import type { Account } from "../internal.js";
2
2
 
3
3
  class ActiveAccountContext {
4
4
  private activeAccount: Account | null = null;
@@ -12,7 +12,14 @@ import {
12
12
  import { AuthSecretStorage } from "../auth/AuthSecretStorage.js";
13
13
  import { type Account, type AccountClass } from "../coValues/account.js";
14
14
  import { RegisteredSchemas } from "../coValues/registeredSchemas.js";
15
- import type { ID } from "../internal.js";
15
+ import {
16
+ type AccountSchema,
17
+ type AnyAccountSchema,
18
+ CoValueFromRaw,
19
+ type ID,
20
+ type InstanceOfSchema,
21
+ anySchemaToCoSchema,
22
+ } from "../internal.js";
16
23
  import { AuthCredentials, NewAccountProps } from "../types.js";
17
24
  import { activeAccountContext } from "./activeAccountContext.js";
18
25
  import { AnonymousJazzAgent } from "./anonymousJazzAgent.js";
@@ -79,7 +86,9 @@ export type JazzContext<Acc extends Account> =
79
86
  | JazzContextWithAgent;
80
87
 
81
88
  export async function createJazzContextFromExistingCredentials<
82
- Acc extends Account,
89
+ S extends
90
+ | (AccountClass<Account> & CoValueFromRaw<Account>)
91
+ | AnyAccountSchema,
83
92
  >({
84
93
  credentials,
85
94
  peersToLoadFrom,
@@ -91,18 +100,19 @@ export async function createJazzContextFromExistingCredentials<
91
100
  credentials: Credentials;
92
101
  peersToLoadFrom: Peer[];
93
102
  crypto: CryptoProvider;
94
- AccountSchema?: AccountClass<Acc>;
103
+ AccountSchema?: S;
95
104
  sessionProvider: SessionProvider;
96
105
  onLogOut?: () => void;
97
- }): Promise<JazzContextWithAccount<Acc>> {
106
+ }): Promise<JazzContextWithAccount<InstanceOfSchema<S>>> {
98
107
  const { sessionID, sessionDone } = await sessionProvider(
99
108
  credentials.accountID,
100
109
  crypto,
101
110
  );
102
111
 
103
112
  const CurrentAccountSchema =
104
- PropsAccountSchema ??
105
- (RegisteredSchemas["Account"] as unknown as AccountClass<Acc>);
113
+ PropsAccountSchema ?? (RegisteredSchemas["Account"] as unknown as S);
114
+
115
+ const AccountClass = anySchemaToCoSchema(CurrentAccountSchema);
106
116
 
107
117
  const node = await LocalNode.withLoadedAccount({
108
118
  accountID: credentials.accountID as unknown as CoID<RawAccount>,
@@ -111,21 +121,19 @@ export async function createJazzContextFromExistingCredentials<
111
121
  peersToLoadFrom: peersToLoadFrom,
112
122
  crypto: crypto,
113
123
  migration: async (rawAccount, _node, creationProps) => {
114
- const account = new CurrentAccountSchema({
115
- fromRaw: rawAccount,
116
- }) as Acc;
124
+ const account = AccountClass.fromRaw(rawAccount) as InstanceOfSchema<S>;
117
125
  activeAccountContext.set(account);
118
126
 
119
127
  await account.applyMigration(creationProps);
120
128
  },
121
129
  });
122
130
 
123
- const account = CurrentAccountSchema.fromNode(node);
131
+ const account = AccountClass.fromNode(node);
124
132
  activeAccountContext.set(account);
125
133
 
126
134
  return {
127
135
  node,
128
- account,
136
+ account: account as InstanceOfSchema<S>,
129
137
  done: () => {
130
138
  node.gracefulShutdown();
131
139
  sessionDone();
@@ -138,7 +146,11 @@ export async function createJazzContextFromExistingCredentials<
138
146
  };
139
147
  }
140
148
 
141
- export async function createJazzContextForNewAccount<Acc extends Account>({
149
+ export async function createJazzContextForNewAccount<
150
+ S extends
151
+ | (AccountClass<Account> & CoValueFromRaw<Account>)
152
+ | AnyAccountSchema,
153
+ >({
142
154
  creationProps,
143
155
  initialAgentSecret,
144
156
  peersToLoadFrom,
@@ -150,12 +162,13 @@ export async function createJazzContextForNewAccount<Acc extends Account>({
150
162
  initialAgentSecret?: AgentSecret;
151
163
  peersToLoadFrom: Peer[];
152
164
  crypto: CryptoProvider;
153
- AccountSchema?: AccountClass<Acc>;
165
+ AccountSchema?: S;
154
166
  onLogOut?: () => Promise<void>;
155
- }): Promise<JazzContextWithAccount<Acc>> {
167
+ }): Promise<JazzContextWithAccount<InstanceOfSchema<S>>> {
156
168
  const CurrentAccountSchema =
157
- PropsAccountSchema ??
158
- (RegisteredSchemas["Account"] as unknown as AccountClass<Acc>);
169
+ PropsAccountSchema ?? (RegisteredSchemas["Account"] as unknown as S);
170
+
171
+ const AccountClass = anySchemaToCoSchema(CurrentAccountSchema);
159
172
 
160
173
  const { node } = await LocalNode.withNewlyCreatedAccount({
161
174
  creationProps,
@@ -163,21 +176,19 @@ export async function createJazzContextForNewAccount<Acc extends Account>({
163
176
  crypto,
164
177
  initialAgentSecret,
165
178
  migration: async (rawAccount, _node, creationProps) => {
166
- const account = new CurrentAccountSchema({
167
- fromRaw: rawAccount,
168
- }) as Acc;
179
+ const account = AccountClass.fromRaw(rawAccount) as InstanceOfSchema<S>;
169
180
  activeAccountContext.set(account);
170
181
 
171
182
  await account.applyMigration(creationProps);
172
183
  },
173
184
  });
174
185
 
175
- const account = CurrentAccountSchema.fromNode(node);
186
+ const account = AccountClass.fromNode(node);
176
187
  activeAccountContext.set(account);
177
188
 
178
189
  return {
179
190
  node,
180
- account,
191
+ account: account as InstanceOfSchema<S>,
181
192
  done: () => {
182
193
  node.gracefulShutdown();
183
194
  },
@@ -188,19 +199,23 @@ export async function createJazzContextForNewAccount<Acc extends Account>({
188
199
  };
189
200
  }
190
201
 
191
- export async function createJazzContext<Acc extends Account>(options: {
202
+ export async function createJazzContext<
203
+ S extends
204
+ | (AccountClass<Account> & CoValueFromRaw<Account>)
205
+ | AnyAccountSchema,
206
+ >(options: {
192
207
  credentials?: AuthCredentials;
193
208
  newAccountProps?: NewAccountProps;
194
209
  peersToLoadFrom: Peer[];
195
210
  crypto: CryptoProvider;
196
211
  defaultProfileName?: string;
197
- AccountSchema?: AccountClass<Acc>;
212
+ AccountSchema?: S;
198
213
  sessionProvider: SessionProvider;
199
214
  authSecretStorage: AuthSecretStorage;
200
215
  }) {
201
216
  const crypto = options.crypto;
202
217
 
203
- let context: JazzContextWithAccount<Acc>;
218
+ let context: JazzContextWithAccount<InstanceOfSchema<S>>;
204
219
 
205
220
  const authSecretStorage = options.authSecretStorage;
206
221
 
@@ -1,6 +1,11 @@
1
1
  import { type InviteSecret, cojsonInternals } from "cojson";
2
2
  import { Account } from "../coValues/account.js";
3
- import type { CoValue, CoValueClass, ID } from "../internal.js";
3
+ import type {
4
+ CoValue,
5
+ CoValueClass,
6
+ CoValueOrZodSchema,
7
+ ID,
8
+ } from "../internal.js";
4
9
 
5
10
  /** @category Invite Links */
6
11
  export function createInviteLink<C extends CoValue>(
@@ -31,11 +36,9 @@ export function createInviteLink<C extends CoValue>(
31
36
  }
32
37
 
33
38
  /** @category Invite Links */
34
- export function parseInviteLink<C extends CoValue>(
35
- inviteURL: string,
36
- ):
39
+ export function parseInviteLink(inviteURL: string):
37
40
  | {
38
- valueID: ID<C>;
41
+ valueID: string;
39
42
  valueHint?: string;
40
43
  inviteSecret: InviteSecret;
41
44
  }
@@ -44,16 +47,16 @@ export function parseInviteLink<C extends CoValue>(
44
47
  const parts = url.hash.split("/");
45
48
 
46
49
  let valueHint: string | undefined;
47
- let valueID: ID<C> | undefined;
50
+ let valueID: string | undefined;
48
51
  let inviteSecret: InviteSecret | undefined;
49
52
 
50
53
  if (parts[0] === "#" && parts[1] === "invite") {
51
54
  if (parts.length === 5) {
52
55
  valueHint = parts[2];
53
- valueID = parts[3] as ID<C>;
56
+ valueID = parts[3];
54
57
  inviteSecret = parts[4] as InviteSecret;
55
58
  } else if (parts.length === 4) {
56
- valueID = parts[2] as ID<C>;
59
+ valueID = parts[2];
57
60
  inviteSecret = parts[3] as InviteSecret;
58
61
  }
59
62
 
@@ -65,7 +68,7 @@ export function parseInviteLink<C extends CoValue>(
65
68
  }
66
69
 
67
70
  /** @category Invite Links */
68
- export function consumeInviteLink<V extends CoValue>({
71
+ export function consumeInviteLink<S extends CoValueOrZodSchema>({
69
72
  inviteURL,
70
73
  as = Account.getMe(),
71
74
  forValueHint,
@@ -74,17 +77,17 @@ export function consumeInviteLink<V extends CoValue>({
74
77
  inviteURL: string;
75
78
  as?: Account;
76
79
  forValueHint?: string;
77
- invitedObjectSchema: CoValueClass<V>;
80
+ invitedObjectSchema: S;
78
81
  }): Promise<
79
82
  | {
80
- valueID: ID<V>;
83
+ valueID: string;
81
84
  valueHint?: string;
82
85
  inviteSecret: InviteSecret;
83
86
  }
84
87
  | undefined
85
88
  > {
86
89
  return new Promise((resolve, reject) => {
87
- const result = parseInviteLink<V>(inviteURL);
90
+ const result = parseInviteLink(inviteURL);
88
91
 
89
92
  if (result && result.valueHint === forValueHint) {
90
93
  as.acceptInvite(result.valueID, result.inviteSecret, invitedObjectSchema)
@@ -4,10 +4,12 @@ import type {
4
4
  CoValue,
5
5
  ID,
6
6
  RefEncoded,
7
- UnCo,
8
7
  } from "../internal.js";
9
- import { isRefEncoded } from "../internal.js";
10
- import { accessChildById, getSubscriptionScope } from "../subscribe/index.js";
8
+ import {
9
+ accessChildById,
10
+ getSubscriptionScope,
11
+ isRefEncoded,
12
+ } from "../internal.js";
11
13
 
12
14
  export class Ref<out V extends CoValue> {
13
15
  constructor(
@@ -119,5 +121,5 @@ export function makeRefs<Keys extends string | number>(
119
121
  }
120
122
 
121
123
  export type RefIfCoValue<V> = NonNullable<V> extends CoValue
122
- ? Ref<UnCo<NonNullable<V>>>
124
+ ? Ref<NonNullable<V>>
123
125
  : never;
@@ -23,77 +23,65 @@ export const Encoders = {
23
23
  },
24
24
  };
25
25
 
26
- export type CoMarker = { readonly __co: unique symbol };
27
- /** @category Schema definition */
28
- export type co<T> = T | (T & CoMarker);
29
- export type IfCo<C, R> = C extends infer _A | infer B
30
- ? B extends CoMarker
31
- ? R extends JazzToolsSymbol // Exclude symbol properties like co.items from the refs/init types
32
- ? never
33
- : R
34
- : never
35
- : never;
36
- export type UnCo<T> = T extends co<infer A> ? A : T;
37
-
38
26
  const optional = {
39
27
  ref: optionalRef,
40
- json<T extends CojsonInternalTypes.CoJsonValue<T>>(): co<T | undefined> {
28
+ json<T extends CojsonInternalTypes.CoJsonValue<T>>(): T | undefined {
41
29
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
42
30
  return { [SchemaInit]: "json" satisfies Schema } as any;
43
31
  },
44
- encoded<T>(arg: OptionalEncoder<T>): co<T | undefined> {
32
+ encoded<T>(arg: OptionalEncoder<T>): T | undefined {
45
33
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
46
34
  return { [SchemaInit]: { encoded: arg } satisfies Schema } as any;
47
35
  },
48
36
  string: {
49
37
  [SchemaInit]: "json" satisfies Schema,
50
- } as unknown as co<string | undefined>,
38
+ } as unknown as string | undefined,
51
39
  number: {
52
40
  [SchemaInit]: "json" satisfies Schema,
53
- } as unknown as co<number | undefined>,
41
+ } as unknown as number | undefined,
54
42
  boolean: {
55
43
  [SchemaInit]: "json" satisfies Schema,
56
- } as unknown as co<boolean | undefined>,
44
+ } as unknown as boolean | undefined,
57
45
  null: {
58
46
  [SchemaInit]: "json" satisfies Schema,
59
- } as unknown as co<null | undefined>,
47
+ } as unknown as null | undefined,
60
48
  Date: {
61
49
  [SchemaInit]: { encoded: Encoders.OptionalDate } satisfies Schema,
62
- } as unknown as co<Date | undefined>,
50
+ } as unknown as Date | undefined,
63
51
  literal<T extends (string | number | boolean)[]>(
64
52
  ..._lit: T
65
- ): co<T[number] | undefined> {
53
+ ): T[number] | undefined {
66
54
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
67
55
  return { [SchemaInit]: "json" satisfies Schema } as any;
68
56
  },
69
57
  };
70
58
 
71
59
  /** @category Schema definition */
72
- export const co = {
60
+ export const coField = {
73
61
  string: {
74
62
  [SchemaInit]: "json" satisfies Schema,
75
- } as unknown as co<string>,
63
+ } as unknown as string,
76
64
  number: {
77
65
  [SchemaInit]: "json" satisfies Schema,
78
- } as unknown as co<number>,
66
+ } as unknown as number,
79
67
  boolean: {
80
68
  [SchemaInit]: "json" satisfies Schema,
81
- } as unknown as co<boolean>,
69
+ } as unknown as boolean,
82
70
  null: {
83
71
  [SchemaInit]: "json" satisfies Schema,
84
- } as unknown as co<null>,
72
+ } as unknown as null,
85
73
  Date: {
86
74
  [SchemaInit]: { encoded: Encoders.Date } satisfies Schema,
87
- } as unknown as co<Date>,
88
- literal<T extends (string | number | boolean)[]>(..._lit: T): co<T[number]> {
75
+ } as unknown as Date,
76
+ literal<T extends (string | number | boolean)[]>(..._lit: T): T[number] {
89
77
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
90
78
  return { [SchemaInit]: "json" satisfies Schema } as any;
91
79
  },
92
- json<T extends CojsonInternalTypes.CoJsonValue<T>>(): co<T> {
80
+ json<T extends CojsonInternalTypes.CoJsonValue<T>>(): T {
93
81
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
94
82
  return { [SchemaInit]: "json" satisfies Schema } as any;
95
83
  },
96
- encoded<T>(arg: Encoder<T>): co<T> {
84
+ encoded<T>(arg: Encoder<T>): T {
97
85
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
98
86
  return { [SchemaInit]: { encoded: arg } satisfies Schema } as any;
99
87
  },
@@ -104,18 +92,18 @@ export const co = {
104
92
 
105
93
  function optionalRef<C extends CoValueClass>(
106
94
  arg: C | ((_raw: InstanceType<C>["_raw"]) => C),
107
- ): co<InstanceType<C> | null | undefined> {
95
+ ): InstanceType<C> | null | undefined {
108
96
  return ref(arg, { optional: true });
109
97
  }
110
98
 
111
99
  function ref<C extends CoValueClass>(
112
100
  arg: C | ((_raw: InstanceType<C>["_raw"]) => C),
113
101
  options?: never,
114
- ): co<InstanceType<C> | null>;
102
+ ): InstanceType<C> | null;
115
103
  function ref<C extends CoValueClass>(
116
104
  arg: C | ((_raw: InstanceType<C>["_raw"]) => C),
117
105
  options: { optional: true },
118
- ): co<InstanceType<C> | null | undefined>;
106
+ ): InstanceType<C> | null | undefined;
119
107
  function ref<
120
108
  C extends CoValueClass,
121
109
  Options extends { optional?: boolean } | undefined,
@@ -123,8 +111,8 @@ function ref<
123
111
  arg: C | ((_raw: InstanceType<C>["_raw"]) => C),
124
112
  options?: Options,
125
113
  ): Options extends { optional: true }
126
- ? co<InstanceType<C> | null | undefined>
127
- : co<InstanceType<C> | null> {
114
+ ? InstanceType<C> | null | undefined
115
+ : InstanceType<C> | null {
128
116
  return {
129
117
  [SchemaInit]: {
130
118
  ref: arg,
@@ -0,0 +1,101 @@
1
+ import z from "zod/v4";
2
+ import { CoMap, CoValueClass, isCoValueClass } from "../../../internal.js";
3
+ import { coField } from "../../schema.js";
4
+ import {
5
+ isUnionOfCoMapsDeeply,
6
+ isUnionOfPrimitivesDeeply,
7
+ schemaUnionDiscriminatorFor,
8
+ } from "../unionUtils.js";
9
+ import { ZodPrimitiveSchema } from "../zodSchema.js";
10
+ import { zodSchemaToCoSchemaOrKeepPrimitive } from "./zodSchemaToCoSchema.js";
11
+
12
+ type FieldSchema =
13
+ | CoValueClass
14
+ | ZodPrimitiveSchema
15
+ | z.core.$ZodOptional<z.core.$ZodType>
16
+ | z.core.$ZodUnion<z.core.$ZodType[]>
17
+ | z.core.$ZodObject<z.core.$ZodLooseShape>
18
+ | z.core.$ZodArray<z.core.$ZodType>
19
+ | z.core.$ZodTuple<z.core.$ZodType[]>
20
+ | (z.core.$ZodCustom<any, any> & { builtin: any });
21
+
22
+ export function zodFieldToCoFieldDef(schema: FieldSchema) {
23
+ if (isCoValueClass(schema)) {
24
+ return coField.ref(schema);
25
+ } else {
26
+ if ("_zod" in schema) {
27
+ if (schema._zod.def.type === "optional") {
28
+ const inner = zodSchemaToCoSchemaOrKeepPrimitive(
29
+ schema._zod.def.innerType,
30
+ );
31
+ if (isCoValueClass(inner)) {
32
+ return coField.ref(inner, { optional: true });
33
+ } else {
34
+ return zodFieldToCoFieldDef(inner);
35
+ }
36
+ } else if (schema._zod.def.type === "string") {
37
+ return coField.string;
38
+ } else if (schema._zod.def.type === "number") {
39
+ return coField.number;
40
+ } else if (schema._zod.def.type === "boolean") {
41
+ return coField.boolean;
42
+ } else if (schema._zod.def.type === "null") {
43
+ return coField.null;
44
+ } else if (schema._zod.def.type === "date") {
45
+ return coField.Date;
46
+ } else if (schema._zod.def.type === "literal") {
47
+ if (
48
+ schema._zod.def.values.some(
49
+ (literal) => typeof literal === "undefined",
50
+ )
51
+ ) {
52
+ throw new Error("z.literal() with undefined is not supported");
53
+ }
54
+ if (schema._zod.def.values.some((literal) => literal === null)) {
55
+ throw new Error("z.literal() with null is not supported");
56
+ }
57
+ if (
58
+ schema._zod.def.values.some((literal) => typeof literal === "bigint")
59
+ ) {
60
+ throw new Error("z.literal() with bigint is not supported");
61
+ }
62
+ return coField.literal(
63
+ ...(schema._zod.def.values as Exclude<
64
+ (typeof schema._zod.def.values)[number],
65
+ undefined | null | bigint
66
+ >[]),
67
+ );
68
+ } else if (
69
+ schema._zod.def.type === "object" ||
70
+ schema._zod.def.type === "array" ||
71
+ schema._zod.def.type === "tuple"
72
+ ) {
73
+ return coField.json();
74
+ } else if (schema._zod.def.type === "custom") {
75
+ if ("builtin" in schema) {
76
+ return zodFieldToCoFieldDef(schema.builtin);
77
+ } else {
78
+ throw new Error(`Unsupported custom zod type`);
79
+ }
80
+ } else if (schema._zod.def.type === "union") {
81
+ if (isUnionOfPrimitivesDeeply(schema)) {
82
+ return coField.json();
83
+ } else if (isUnionOfCoMapsDeeply(schema)) {
84
+ return coField.ref<CoValueClass<CoMap>>(
85
+ schemaUnionDiscriminatorFor(schema),
86
+ );
87
+ } else {
88
+ throw new Error(
89
+ "z.union()/z.discriminatedUnion() of mixed collaborative and non-collaborative types is not supported",
90
+ );
91
+ }
92
+ } else {
93
+ throw new Error(
94
+ `Unsupported zod type: ${(schema._zod?.def as any)?.type || JSON.stringify(schema)}`,
95
+ );
96
+ }
97
+ } else {
98
+ throw new Error(`Unsupported zod type: ${schema}`);
99
+ }
100
+ }
101
+ }