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.
- package/.svelte-kit/__package__/jazz.class.svelte.d.ts +2 -2
- package/.svelte-kit/__package__/jazz.class.svelte.d.ts.map +1 -1
- package/.svelte-kit/__package__/jazz.class.svelte.js +5 -5
- package/.svelte-kit/__package__/jazz.svelte.d.ts +2 -2
- package/.svelte-kit/__package__/jazz.svelte.d.ts.map +1 -1
- package/.turbo/turbo-build.log +46 -50
- package/CHANGELOG.md +35 -0
- package/dist/browser/index.d.ts +2 -2
- package/dist/browser/index.d.ts.map +1 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/browser-media-images/index.d.ts +3 -1
- package/dist/browser-media-images/index.d.ts.map +1 -1
- package/dist/browser-media-images/index.js.map +1 -1
- package/dist/{chunk-4CFNXQE7.js → chunk-MLCNE3TL.js} +791 -698
- package/dist/chunk-MLCNE3TL.js.map +1 -0
- package/dist/index.js +363 -11
- package/dist/index.js.map +1 -1
- package/dist/react/hooks.d.ts +2 -2
- package/dist/react/hooks.d.ts.map +1 -1
- package/dist/react/index.js +2 -0
- package/dist/react/index.js.map +1 -1
- package/dist/react/testing.js +3 -1
- package/dist/react/testing.js.map +1 -1
- package/dist/react-core/hooks.d.ts +2 -2
- package/dist/react-core/hooks.d.ts.map +1 -1
- package/dist/react-core/index.js +3 -3
- package/dist/react-core/index.js.map +1 -1
- package/dist/react-native-core/hooks.d.ts +2 -2
- package/dist/react-native-core/hooks.d.ts.map +1 -1
- package/dist/react-native-core/index.js.map +1 -1
- package/dist/svelte/jazz.class.svelte.d.ts +2 -2
- package/dist/svelte/jazz.class.svelte.d.ts.map +1 -1
- package/dist/svelte/jazz.class.svelte.js +5 -5
- package/dist/svelte/jazz.svelte.d.ts +2 -2
- package/dist/svelte/jazz.svelte.d.ts.map +1 -1
- package/dist/testing.js +3 -3
- package/dist/testing.js.map +1 -1
- package/dist/tools/coValues/CoValueBase.d.ts +3 -13
- package/dist/tools/coValues/CoValueBase.d.ts.map +1 -1
- package/dist/tools/coValues/account.d.ts +2 -2
- package/dist/tools/coValues/account.d.ts.map +1 -1
- package/dist/tools/coValues/coFeed.d.ts.map +1 -1
- package/dist/tools/coValues/coList.d.ts.map +1 -1
- package/dist/tools/coValues/coMap.d.ts +5 -18
- package/dist/tools/coValues/coMap.d.ts.map +1 -1
- package/dist/tools/coValues/deepLoading.d.ts +4 -1
- package/dist/tools/coValues/deepLoading.d.ts.map +1 -1
- package/dist/tools/coValues/extensions/imageDef.d.ts +4 -7
- package/dist/tools/coValues/extensions/imageDef.d.ts.map +1 -1
- package/dist/tools/coValues/group.d.ts +1 -0
- package/dist/tools/coValues/group.d.ts.map +1 -1
- package/dist/tools/coValues/inbox.d.ts +2 -2
- package/dist/tools/coValues/inbox.d.ts.map +1 -1
- package/dist/tools/coValues/interfaces.d.ts +58 -17
- package/dist/tools/coValues/interfaces.d.ts.map +1 -1
- package/dist/tools/coValues/request.d.ts +82 -0
- package/dist/tools/coValues/request.d.ts.map +1 -0
- package/dist/tools/coValues/schemaUnion.d.ts +5 -1
- package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
- package/dist/tools/exports.d.ts +4 -3
- package/dist/tools/exports.d.ts.map +1 -1
- package/dist/tools/implementation/createContext.d.ts +4 -4
- package/dist/tools/implementation/createContext.d.ts.map +1 -1
- package/dist/tools/implementation/invites.d.ts +2 -2
- package/dist/tools/implementation/invites.d.ts.map +1 -1
- package/dist/tools/implementation/schemaUtils.d.ts +8 -0
- package/dist/tools/implementation/schemaUtils.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/coExport.d.ts +11 -1
- package/dist/tools/implementation/zodSchema/coExport.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts +22 -0
- package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts +10 -0
- package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +11 -11
- package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +34 -24
- package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +16 -14
- package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +23 -17
- package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +24 -16
- package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts +20 -9
- package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +18 -12
- package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoValueSchema.d.ts +18 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/CoValueSchema.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +14 -9
- package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +14 -9
- package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +14 -9
- package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +15 -13
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +12 -15
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +17 -20
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +17 -20
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/unionUtils.d.ts +3 -5
- package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodCo.d.ts +10 -8
- package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodReExport.d.ts +4 -8
- package/dist/tools/implementation/zodSchema/zodReExport.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodSchema.d.ts +21 -30
- package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
- package/dist/tools/internal.d.ts +3 -2
- package/dist/tools/internal.d.ts.map +1 -1
- package/dist/tools/lib/id.d.ts +2 -0
- package/dist/tools/lib/id.d.ts.map +1 -0
- package/dist/tools/lib/utilityTypes.d.ts +10 -0
- package/dist/tools/lib/utilityTypes.d.ts.map +1 -0
- package/dist/tools/subscribe/SubscriptionScope.d.ts +3 -2
- package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
- package/dist/tools/subscribe/utils.d.ts.map +1 -1
- package/dist/tools/testing.d.ts +2 -2
- package/dist/tools/testing.d.ts.map +1 -1
- package/dist/tools/tests/exportImport.test.d.ts +2 -0
- package/dist/tools/tests/exportImport.test.d.ts.map +1 -0
- package/dist/tools/tests/request.test.d.ts +2 -0
- package/dist/tools/tests/request.test.d.ts.map +1 -0
- package/dist/worker/index.d.ts.map +1 -1
- package/dist/worker/index.js +2 -2
- package/dist/worker/index.js.map +1 -1
- package/package.json +7 -6
- package/src/browser/index.ts +2 -4
- package/src/browser-media-images/index.ts +1 -1
- package/src/react/hooks.tsx +2 -2
- package/src/react-core/hooks.ts +6 -6
- package/src/react-core/tests/useAccount.test.ts +2 -2
- package/src/react-core/tests/useCoState.test.ts +3 -2
- package/src/react-native-core/hooks.tsx +2 -2
- package/src/svelte/jazz.class.svelte.ts +10 -7
- package/src/svelte/jazz.svelte.ts +2 -2
- package/src/tools/coValues/CoValueBase.ts +8 -20
- package/src/tools/coValues/account.ts +18 -14
- package/src/tools/coValues/coFeed.ts +0 -4
- package/src/tools/coValues/coList.ts +7 -9
- package/src/tools/coValues/coMap.ts +1 -6
- package/src/tools/coValues/coPlainText.ts +4 -4
- package/src/tools/coValues/deepLoading.ts +4 -1
- package/src/tools/coValues/extensions/imageDef.ts +3 -3
- package/src/tools/coValues/group.ts +1 -0
- package/src/tools/coValues/inbox.ts +8 -7
- package/src/tools/coValues/interfaces.ts +177 -69
- package/src/tools/coValues/request.ts +633 -0
- package/src/tools/coValues/schemaUnion.ts +8 -4
- package/src/tools/exports.ts +11 -14
- package/src/tools/implementation/createContext.ts +9 -9
- package/src/tools/implementation/invites.ts +2 -2
- package/src/tools/implementation/schemaUtils.ts +18 -0
- package/src/tools/implementation/zodSchema/coExport.ts +14 -0
- package/src/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.ts +156 -0
- package/src/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.ts +133 -0
- package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +36 -17
- package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +101 -52
- package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +54 -50
- package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +54 -46
- package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +187 -137
- package/src/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.ts +29 -27
- package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +28 -18
- package/src/tools/implementation/zodSchema/schemaTypes/CoValueSchema.ts +18 -0
- package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +45 -36
- package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +47 -35
- package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +43 -30
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +28 -23
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +28 -25
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +86 -78
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +91 -82
- package/src/tools/implementation/zodSchema/unionUtils.ts +47 -51
- package/src/tools/implementation/zodSchema/zodCo.ts +42 -75
- package/src/tools/implementation/zodSchema/zodReExport.ts +44 -24
- package/src/tools/implementation/zodSchema/zodSchema.ts +64 -102
- package/src/tools/internal.ts +3 -2
- package/src/tools/lib/id.ts +3 -0
- package/src/tools/lib/utilityTypes.ts +7 -0
- package/src/tools/subscribe/SubscriptionScope.ts +10 -1
- package/src/tools/subscribe/utils.ts +4 -2
- package/src/tools/testing.ts +4 -4
- package/src/tools/tests/ContextManager.test.ts +8 -9
- package/src/tools/tests/account.test.ts +65 -3
- package/src/tools/tests/coDiscriminatedUnion.test-d.ts +38 -0
- package/src/tools/tests/coDiscriminatedUnion.test.ts +219 -1
- package/src/tools/tests/coFeed.test-d.ts +4 -3
- package/src/tools/tests/coList.test-d.ts +32 -3
- package/src/tools/tests/coList.test.ts +20 -2
- package/src/tools/tests/coMap.record.test-d.ts +31 -3
- package/src/tools/tests/coMap.record.test.ts +9 -9
- package/src/tools/tests/coMap.test-d.ts +8 -8
- package/src/tools/tests/coMap.test.ts +19 -5
- package/src/tools/tests/coOptional.test.ts +63 -1
- package/src/tools/tests/createContext.test.ts +7 -9
- package/src/tools/tests/deepLoading.test.ts +4 -10
- package/src/tools/tests/exportImport.test.ts +526 -0
- package/src/tools/tests/groupsAndAccounts.test.ts +5 -4
- package/src/tools/tests/inbox.test.ts +3 -2
- package/src/tools/tests/load.test.ts +3 -29
- package/src/tools/tests/request.test.ts +951 -0
- package/src/tools/tests/schemaUnion.test.ts +2 -2
- package/src/tools/tests/subscribe.test.ts +22 -114
- package/src/tools/tests/testing.test.ts +6 -6
- package/src/tools/tests/zod.test-d.ts +27 -0
- package/src/tools/tests/zod.test.ts +50 -45
- package/src/worker/index.ts +0 -1
- package/tsup.config.ts +0 -2
- package/dist/chunk-4CFNXQE7.js.map +0 -1
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +0 -12
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +0 -1
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +0 -9
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +0 -1
- package/src/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +0 -172
- 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
|
-
|
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
|
-
|
|
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 =
|
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
|
-
|
|
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 =
|
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
|
-
|
|
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
|
-
|
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
|
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 {
|
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:
|
23
|
-
root:
|
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
|
36
|
+
export interface AccountSchema<
|
32
37
|
Shape extends BaseAccountShape = DefaultAccountShape,
|
33
|
-
>
|
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:
|
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
|
-
|
131
|
-
> = CoMapSchema<Shape & DefaultProfileShape,
|
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
|
154
|
+
export interface CoreAccountSchema<
|
135
155
|
Shape extends z.core.$ZodLooseShape = z.core.$ZodLooseShape,
|
136
|
-
>
|
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]>;
|