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.
- package/.turbo/turbo-build.log +8 -8
- package/CHANGELOG.md +22 -3
- package/dist/auth/DemoAuth.d.ts.map +1 -1
- package/dist/auth/PassphraseAuth.d.ts +1 -3
- package/dist/auth/PassphraseAuth.d.ts.map +1 -1
- package/dist/{chunk-IJU4XPFS.js → chunk-WLOZKDOH.js} +3536 -3291
- package/dist/chunk-WLOZKDOH.js.map +1 -0
- package/dist/coValues/CoValueBase.d.ts +22 -0
- package/dist/coValues/CoValueBase.d.ts.map +1 -0
- package/dist/coValues/account.d.ts +12 -12
- package/dist/coValues/account.d.ts.map +1 -1
- package/dist/coValues/coFeed.d.ts +24 -25
- package/dist/coValues/coFeed.d.ts.map +1 -1
- package/dist/coValues/coList.d.ts +10 -13
- package/dist/coValues/coList.d.ts.map +1 -1
- package/dist/coValues/coMap.d.ts +32 -35
- package/dist/coValues/coMap.d.ts.map +1 -1
- package/dist/coValues/coPlainText.d.ts.map +1 -1
- package/dist/coValues/deepLoading.d.ts +17 -21
- package/dist/coValues/deepLoading.d.ts.map +1 -1
- package/dist/coValues/extensions/imageDef.d.ts +12 -11
- package/dist/coValues/extensions/imageDef.d.ts.map +1 -1
- package/dist/coValues/group.d.ts +5 -9
- package/dist/coValues/group.d.ts.map +1 -1
- package/dist/coValues/inbox.d.ts +2 -3
- package/dist/coValues/inbox.d.ts.map +1 -1
- package/dist/coValues/interfaces.d.ts +8 -34
- package/dist/coValues/interfaces.d.ts.map +1 -1
- package/dist/coValues/profile.d.ts +4 -14
- package/dist/coValues/profile.d.ts.map +1 -1
- package/dist/coValues/registeredSchemas.d.ts +1 -3
- package/dist/coValues/registeredSchemas.d.ts.map +1 -1
- package/dist/coValues/schemaUnion.d.ts +6 -6
- package/dist/exports.d.ts +12 -16
- package/dist/exports.d.ts.map +1 -1
- package/dist/implementation/ContextManager.d.ts +1 -1
- package/dist/implementation/ContextManager.d.ts.map +1 -1
- package/dist/implementation/activeAccountContext.d.ts +1 -1
- package/dist/implementation/activeAccountContext.d.ts.map +1 -1
- package/dist/implementation/createContext.d.ts +10 -10
- package/dist/implementation/createContext.d.ts.map +1 -1
- package/dist/implementation/invites.d.ts +6 -6
- package/dist/implementation/invites.d.ts.map +1 -1
- package/dist/implementation/refs.d.ts +2 -2
- package/dist/implementation/refs.d.ts.map +1 -1
- package/dist/implementation/schema.d.ts +21 -28
- package/dist/implementation/schema.d.ts.map +1 -1
- package/dist/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +9 -0
- package/dist/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +1 -0
- package/dist/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +28 -0
- package/dist/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +65 -0
- package/dist/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +28 -0
- package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +24 -0
- package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +41 -0
- package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +35 -0
- package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +9 -0
- package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +20 -0
- package/dist/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +18 -0
- package/dist/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +24 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +21 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +29 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +29 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -0
- package/dist/implementation/zodSchema/unionUtils.d.ts +6 -0
- package/dist/implementation/zodSchema/unionUtils.d.ts.map +1 -0
- package/dist/implementation/zodSchema/zodCo.d.ts +35 -0
- package/dist/implementation/zodSchema/zodCo.d.ts.map +1 -0
- package/dist/implementation/zodSchema/zodSchema.d.ts +38 -0
- package/dist/implementation/zodSchema/zodSchema.d.ts.map +1 -0
- package/dist/index.js +295 -10
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +34 -0
- package/dist/internal.d.ts.map +1 -1
- package/dist/subscribe/SubscriptionScope.d.ts +1 -2
- package/dist/subscribe/SubscriptionScope.d.ts.map +1 -1
- package/dist/subscribe/utils.d.ts +2 -2
- package/dist/subscribe/utils.d.ts.map +1 -1
- package/dist/testing.d.ts +10 -8
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +1 -1
- package/dist/testing.js.map +1 -1
- package/dist/tests/utils.d.ts +6 -2
- package/dist/tests/utils.d.ts.map +1 -1
- package/dist/types.d.ts +1 -7
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/auth/DemoAuth.ts +1 -2
- package/src/auth/PassphraseAuth.ts +1 -1
- package/src/coValues/CoValueBase.ts +88 -0
- package/src/coValues/account.ts +53 -43
- package/src/coValues/coFeed.ts +65 -83
- package/src/coValues/coList.ts +28 -21
- package/src/coValues/coMap.ts +54 -38
- package/src/coValues/coPlainText.ts +4 -1
- package/src/coValues/deepLoading.ts +35 -43
- package/src/coValues/extensions/imageDef.ts +21 -19
- package/src/coValues/group.ts +37 -38
- package/src/coValues/inbox.ts +24 -11
- package/src/coValues/interfaces.ts +29 -93
- package/src/coValues/profile.ts +12 -13
- package/src/coValues/registeredSchemas.ts +1 -3
- package/src/coValues/schemaUnion.ts +6 -6
- package/src/exports.ts +47 -25
- package/src/implementation/activeAccountContext.ts +1 -1
- package/src/implementation/createContext.ts +39 -24
- package/src/implementation/invites.ts +15 -12
- package/src/implementation/refs.ts +6 -4
- package/src/implementation/schema.ts +22 -34
- package/src/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +101 -0
- package/src/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +191 -0
- package/src/implementation/zodSchema/schemaTypes/AccountSchema.ts +102 -0
- package/src/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +70 -0
- package/src/implementation/zodSchema/schemaTypes/CoListSchema.ts +59 -0
- package/src/implementation/zodSchema/schemaTypes/CoMapSchema.ts +126 -0
- package/src/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +98 -0
- package/src/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +9 -0
- package/src/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +27 -0
- package/src/implementation/zodSchema/schemaTypes/RichTextSchema.ts +25 -0
- package/src/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +61 -0
- package/src/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +77 -0
- package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +90 -0
- package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +103 -0
- package/src/implementation/zodSchema/unionUtils.ts +139 -0
- package/src/implementation/zodSchema/zodCo.ts +409 -0
- package/src/implementation/zodSchema/zodSchema.ts +116 -0
- package/src/internal.ts +38 -0
- package/src/subscribe/SubscriptionScope.ts +3 -1
- package/src/subscribe/utils.ts +7 -2
- package/src/testing.ts +14 -16
- package/src/tests/ContextManager.test.ts +73 -47
- package/src/tests/DemoAuth.test.ts +1 -1
- package/src/tests/account.test.ts +6 -9
- package/src/tests/coFeed.test.ts +102 -63
- package/src/tests/coList.test.ts +82 -95
- package/src/tests/coMap.record.test.ts +53 -87
- package/src/tests/coMap.test.ts +297 -312
- package/src/tests/coPlainText.test.ts +19 -39
- package/src/tests/createContext.test.ts +33 -15
- package/src/tests/deepLoading.test.ts +196 -179
- package/src/tests/groupsAndAccounts.test.ts +81 -72
- package/src/tests/imageDef.test.ts +22 -13
- package/src/tests/inbox.test.ts +36 -29
- package/src/tests/load.test.ts +10 -10
- package/src/tests/patterns/requestToJoin.test.ts +31 -31
- package/src/tests/schema.test.ts +37 -38
- package/src/tests/schemaUnion.test.ts +54 -64
- package/src/tests/subscribe.test.ts +118 -116
- package/src/tests/testing.test.ts +33 -33
- package/src/tests/utils.ts +3 -2
- package/src/types.ts +1 -8
- package/dist/chunk-IJU4XPFS.js.map +0 -1
- package/dist/tests/deepLoading.test-d.d.ts +0 -2
- package/dist/tests/deepLoading.test-d.d.ts.map +0 -1
- 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,
|
15
|
+
export { Encoders, coField } from "./internal.js";
|
14
16
|
|
15
|
-
export { Inbox, InboxSender } from "./
|
17
|
+
export { Inbox, InboxSender } from "./internal.js";
|
16
18
|
|
17
|
-
export {
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
} from "./
|
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 "./
|
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";
|
@@ -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
|
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
|
-
|
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?:
|
103
|
+
AccountSchema?: S;
|
95
104
|
sessionProvider: SessionProvider;
|
96
105
|
onLogOut?: () => void;
|
97
|
-
}): Promise<JazzContextWithAccount<
|
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
|
-
|
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 =
|
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 =
|
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<
|
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?:
|
165
|
+
AccountSchema?: S;
|
154
166
|
onLogOut?: () => Promise<void>;
|
155
|
-
}): Promise<JazzContextWithAccount<
|
167
|
+
}): Promise<JazzContextWithAccount<InstanceOfSchema<S>>> {
|
156
168
|
const CurrentAccountSchema =
|
157
|
-
PropsAccountSchema ??
|
158
|
-
|
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 =
|
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 =
|
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<
|
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?:
|
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<
|
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 {
|
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
|
35
|
-
inviteURL: string,
|
36
|
-
):
|
39
|
+
export function parseInviteLink(inviteURL: string):
|
37
40
|
| {
|
38
|
-
valueID:
|
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:
|
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]
|
56
|
+
valueID = parts[3];
|
54
57
|
inviteSecret = parts[4] as InviteSecret;
|
55
58
|
} else if (parts.length === 4) {
|
56
|
-
valueID = parts[2]
|
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<
|
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:
|
80
|
+
invitedObjectSchema: S;
|
78
81
|
}): Promise<
|
79
82
|
| {
|
80
|
-
valueID:
|
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
|
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 {
|
10
|
-
|
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<
|
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>>():
|
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>):
|
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
|
38
|
+
} as unknown as string | undefined,
|
51
39
|
number: {
|
52
40
|
[SchemaInit]: "json" satisfies Schema,
|
53
|
-
} as unknown as
|
41
|
+
} as unknown as number | undefined,
|
54
42
|
boolean: {
|
55
43
|
[SchemaInit]: "json" satisfies Schema,
|
56
|
-
} as unknown as
|
44
|
+
} as unknown as boolean | undefined,
|
57
45
|
null: {
|
58
46
|
[SchemaInit]: "json" satisfies Schema,
|
59
|
-
} as unknown as
|
47
|
+
} as unknown as null | undefined,
|
60
48
|
Date: {
|
61
49
|
[SchemaInit]: { encoded: Encoders.OptionalDate } satisfies Schema,
|
62
|
-
} as unknown as
|
50
|
+
} as unknown as Date | undefined,
|
63
51
|
literal<T extends (string | number | boolean)[]>(
|
64
52
|
..._lit: T
|
65
|
-
):
|
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
|
60
|
+
export const coField = {
|
73
61
|
string: {
|
74
62
|
[SchemaInit]: "json" satisfies Schema,
|
75
|
-
} as unknown as
|
63
|
+
} as unknown as string,
|
76
64
|
number: {
|
77
65
|
[SchemaInit]: "json" satisfies Schema,
|
78
|
-
} as unknown as
|
66
|
+
} as unknown as number,
|
79
67
|
boolean: {
|
80
68
|
[SchemaInit]: "json" satisfies Schema,
|
81
|
-
} as unknown as
|
69
|
+
} as unknown as boolean,
|
82
70
|
null: {
|
83
71
|
[SchemaInit]: "json" satisfies Schema,
|
84
|
-
} as unknown as
|
72
|
+
} as unknown as null,
|
85
73
|
Date: {
|
86
74
|
[SchemaInit]: { encoded: Encoders.Date } satisfies Schema,
|
87
|
-
} as unknown as
|
88
|
-
literal<T extends (string | number | boolean)[]>(..._lit: T):
|
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>>():
|
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>):
|
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
|
-
):
|
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
|
-
):
|
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
|
-
):
|
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
|
-
?
|
127
|
-
:
|
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
|
+
}
|