jazz-tools 0.15.10 → 0.15.11

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 (98) hide show
  1. package/.turbo/turbo-build.log +42 -42
  2. package/CHANGELOG.md +10 -0
  3. package/dist/{chunk-V54IPL5M.js → chunk-MLLG3GBR.js} +421 -302
  4. package/dist/chunk-MLLG3GBR.js.map +1 -0
  5. package/dist/index.js +1 -1
  6. package/dist/testing.js +1 -1
  7. package/dist/tools/coValues/CoValueBase.d.ts.map +1 -1
  8. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  9. package/dist/tools/coValues/inbox.d.ts.map +1 -1
  10. package/dist/tools/coValues/schemaUnion.d.ts +27 -1
  11. package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
  12. package/dist/tools/implementation/zodSchema/coExport.d.ts +1 -1
  13. package/dist/tools/implementation/zodSchema/coExport.d.ts.map +1 -1
  14. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +6 -3
  15. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +1 -1
  16. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +6 -25
  17. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +1 -1
  18. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +2 -1
  19. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
  20. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +30 -0
  21. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -0
  22. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +2 -1
  23. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  24. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +3 -3
  25. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  26. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +3 -3
  27. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  28. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts +11 -0
  29. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts.map +1 -0
  30. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +2 -3
  31. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  32. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +5 -2
  33. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  34. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +5 -2
  35. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
  36. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +5 -2
  37. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
  38. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +7 -12
  39. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
  40. package/dist/tools/implementation/zodSchema/unionUtils.d.ts +2 -3
  41. package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
  42. package/dist/tools/implementation/zodSchema/zodCo.d.ts +6 -6
  43. package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
  44. package/dist/tools/implementation/zodSchema/zodReExport.d.ts +8 -1
  45. package/dist/tools/implementation/zodSchema/zodReExport.d.ts.map +1 -1
  46. package/dist/tools/implementation/zodSchema/zodSchema.d.ts +18 -13
  47. package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
  48. package/dist/tools/internal.d.ts +1 -0
  49. package/dist/tools/internal.d.ts.map +1 -1
  50. package/dist/tools/tests/coDiscriminatedUnion.test-d.d.ts +2 -0
  51. package/dist/tools/tests/coDiscriminatedUnion.test-d.d.ts.map +1 -0
  52. package/dist/tools/tests/coDiscriminatedUnion.test.d.ts +2 -0
  53. package/dist/tools/tests/coDiscriminatedUnion.test.d.ts.map +1 -0
  54. package/dist/tools/tests/coOptional.test.d.ts +2 -0
  55. package/dist/tools/tests/coOptional.test.d.ts.map +1 -0
  56. package/dist/tools/tests/zod.test-d.d.ts +2 -0
  57. package/dist/tools/tests/zod.test-d.d.ts.map +1 -0
  58. package/package.json +5 -4
  59. package/src/react-core/tests/useAccount.test.ts +1 -11
  60. package/src/tools/coValues/CoValueBase.ts +2 -2
  61. package/src/tools/coValues/inbox.ts +0 -2
  62. package/src/tools/coValues/schemaUnion.ts +62 -0
  63. package/src/tools/implementation/zodSchema/coExport.ts +2 -0
  64. package/src/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +29 -12
  65. package/src/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +93 -63
  66. package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +62 -3
  67. package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +90 -0
  68. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +27 -2
  69. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +27 -5
  70. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +60 -5
  71. package/src/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.ts +33 -0
  72. package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +3 -5
  73. package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +32 -2
  74. package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +30 -2
  75. package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +26 -2
  76. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +10 -13
  77. package/src/tools/implementation/zodSchema/unionUtils.ts +22 -14
  78. package/src/tools/implementation/zodSchema/zodCo.ts +68 -256
  79. package/src/tools/implementation/zodSchema/zodReExport.ts +34 -5
  80. package/src/tools/implementation/zodSchema/zodSchema.ts +78 -19
  81. package/src/tools/internal.ts +1 -0
  82. package/src/tools/tests/ContextManager.test.ts +5 -6
  83. package/src/tools/tests/coDiscriminatedUnion.test-d.ts +57 -0
  84. package/src/tools/tests/coDiscriminatedUnion.test.ts +93 -0
  85. package/src/tools/tests/coList.test.ts +4 -12
  86. package/src/tools/tests/coMap.record.test.ts +4 -4
  87. package/src/tools/tests/coMap.test-d.ts +3 -3
  88. package/src/tools/tests/coMap.test.ts +10 -10
  89. package/src/tools/tests/coOptional.test.ts +39 -0
  90. package/src/tools/tests/createContext.test.ts +5 -4
  91. package/src/tools/tests/deepLoading.test.ts +3 -3
  92. package/src/tools/tests/inbox.test.ts +3 -11
  93. package/src/tools/tests/patterns/requestToJoin.test.ts +1 -1
  94. package/src/tools/tests/schemaUnion.test.ts +11 -22
  95. package/src/tools/tests/subscribe.test.ts +25 -39
  96. package/src/tools/tests/zod.test-d.ts +70 -0
  97. package/src/tools/tests/zod.test.ts +64 -5
  98. package/dist/chunk-V54IPL5M.js.map +0 -1
@@ -6,9 +6,12 @@ import {
6
6
  } from "../../../internal.js";
7
7
  import { z } from "../zodReExport.js";
8
8
 
9
- export type FileStreamSchema = z.core.$ZodCustom<FileStream, unknown> & {
9
+ export type AnyFileStreamSchema = z.core.$ZodCustom<FileStream, unknown> & {
10
10
  collaborative: true;
11
11
  builtin: "FileStream";
12
+ };
13
+
14
+ export type FileStreamSchema = AnyFileStreamSchema & {
12
15
  create(options?: { owner?: Account | Group } | Account | Group): FileStream;
13
16
  createFromBlob(
14
17
  blob: Blob | File,
@@ -40,5 +43,32 @@ export type FileStreamSchema = z.core.$ZodCustom<FileStream, unknown> & {
40
43
  id: string,
41
44
  listener: (value: FileStream, unsubscribe: () => void) => void,
42
45
  ): () => void;
43
- getCoSchema: () => typeof FileStream;
46
+ getCoValueClass: () => typeof FileStream;
44
47
  };
48
+
49
+ export function enrichFileStreamSchema(
50
+ schema: AnyFileStreamSchema,
51
+ coValueClass: typeof FileStream,
52
+ ): FileStreamSchema {
53
+ return Object.assign(schema, {
54
+ create: (...args: any[]) => {
55
+ return coValueClass.create(...args);
56
+ },
57
+ createFromBlob: (...args: [any, ...any[]]) => {
58
+ return coValueClass.createFromBlob(...args);
59
+ },
60
+ load: (...args: [any, ...any[]]) => {
61
+ return coValueClass.load(...args);
62
+ },
63
+ loadAsBlob: (...args: [any, ...any[]]) => {
64
+ return coValueClass.loadAsBlob(...args);
65
+ },
66
+ subscribe: (...args: [any, ...any[]]) => {
67
+ // @ts-expect-error
68
+ return coValueClass.subscribe(...args);
69
+ },
70
+ getCoValueClass: () => {
71
+ return coValueClass;
72
+ },
73
+ }) as unknown as FileStreamSchema;
74
+ }
@@ -3,9 +3,12 @@ import { Account, CoPlainText, Group } from "../../../internal.js";
3
3
  import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
4
4
  import { z } from "../zodReExport.js";
5
5
 
6
- export type PlainTextSchema = z.core.$ZodCustom<CoPlainText, unknown> & {
6
+ export type AnyPlainTextSchema = z.core.$ZodCustom<CoPlainText, unknown> & {
7
7
  collaborative: true;
8
8
  builtin: "CoPlainText";
9
+ };
10
+
11
+ export type PlainTextSchema = AnyPlainTextSchema & {
9
12
  create(
10
13
  text: string,
11
14
  options?: { owner: Account | Group } | Account | Group,
@@ -24,5 +27,30 @@ export type PlainTextSchema = z.core.$ZodCustom<CoPlainText, unknown> & {
24
27
  listener: (value: CoPlainText, unsubscribe: () => void) => void,
25
28
  ): () => void;
26
29
  fromRaw(raw: RawCoPlainText): CoPlainText;
27
- getCoSchema: () => typeof CoPlainText;
30
+ getCoValueClass: () => typeof CoPlainText;
28
31
  };
32
+
33
+ export function enrichPlainTextSchema(
34
+ schema: AnyPlainTextSchema,
35
+ coValueClass: typeof CoPlainText,
36
+ ): PlainTextSchema {
37
+ return Object.assign(schema, {
38
+ create: (...args: [any, ...any[]]) => {
39
+ return coValueClass.create(...args);
40
+ },
41
+ load: (...args: [any, ...any[]]) => {
42
+ return coValueClass.load(...args);
43
+ },
44
+ subscribe: (...args: [any, ...any[]]) => {
45
+ // @ts-expect-error
46
+ return coValueClass.subscribe(...args);
47
+ },
48
+ fromRaw: (...args: [any, ...any[]]) => {
49
+ // @ts-expect-error
50
+ return coValueClass.fromRaw(...args);
51
+ },
52
+ getCoValueClass: () => {
53
+ return coValueClass;
54
+ },
55
+ }) as unknown as PlainTextSchema;
56
+ }
@@ -2,9 +2,12 @@ import { Account, CoRichText, Group } from "../../../internal.js";
2
2
  import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
3
3
  import { z } from "../zodReExport.js";
4
4
 
5
- export type RichTextSchema = z.core.$ZodCustom<CoRichText, unknown> & {
5
+ export type AnyRichTextSchema = z.core.$ZodCustom<CoRichText, unknown> & {
6
6
  collaborative: true;
7
7
  builtin: "CoRichText";
8
+ };
9
+
10
+ export type RichTextSchema = AnyRichTextSchema & {
8
11
  create(
9
12
  text: string,
10
13
  options?: { owner: Account | Group } | Account | Group,
@@ -22,5 +25,26 @@ export type RichTextSchema = z.core.$ZodCustom<CoRichText, unknown> & {
22
25
  id: string,
23
26
  listener: (value: CoRichText, unsubscribe: () => void) => void,
24
27
  ): () => void;
25
- getCoSchema: () => typeof CoRichText;
28
+ getCoValueClass: () => typeof CoRichText;
26
29
  };
30
+
31
+ export function enrichRichTextSchema(
32
+ schema: AnyRichTextSchema,
33
+ coValueClass: typeof CoRichText,
34
+ ): RichTextSchema {
35
+ return Object.assign(schema, {
36
+ create: (...args: [any, ...any[]]) => {
37
+ return coValueClass.create(...args);
38
+ },
39
+ load: (...args: [any, ...any[]]) => {
40
+ return coValueClass.load(...args);
41
+ },
42
+ subscribe: (...args: [any, ...any[]]) => {
43
+ // @ts-expect-error
44
+ return coValueClass.subscribe(...args);
45
+ },
46
+ getCoValueClass: () => {
47
+ return coValueClass;
48
+ },
49
+ }) as unknown as RichTextSchema;
50
+ }
@@ -1,5 +1,7 @@
1
1
  import {
2
2
  Account,
3
+ AnyAccountSchema,
4
+ AnyCoRecordSchema,
3
5
  CoFeed,
4
6
  CoList,
5
7
  CoMap,
@@ -11,24 +13,19 @@ import {
11
13
  import { AnyCoFeedSchema } from "../schemaTypes/CoFeedSchema.js";
12
14
  import { AnyCoListSchema } from "../schemaTypes/CoListSchema.js";
13
15
  import { AnyCoMapSchema } from "../schemaTypes/CoMapSchema.js";
14
- import { FileStreamSchema } from "../schemaTypes/FileStreamSchema.js";
15
- import { PlainTextSchema } from "../schemaTypes/PlainTextSchema.js";
16
- import { RichTextSchema } from "../schemaTypes/RichTextSchema.js";
16
+ import { AnyFileStreamSchema } from "../schemaTypes/FileStreamSchema.js";
17
+ import { AnyPlainTextSchema } from "../schemaTypes/PlainTextSchema.js";
18
+ import { AnyRichTextSchema } from "../schemaTypes/RichTextSchema.js";
17
19
  import { z } from "../zodReExport.js";
18
20
  import { InstanceOrPrimitiveOfSchema } from "./InstanceOrPrimitiveOfSchema.js";
19
21
 
20
22
  export type InstanceOfSchema<S extends CoValueClass | z.core.$ZodType> =
21
23
  S extends z.core.$ZodType
22
- ? S extends z.core.$ZodObject<infer Shape> & {
23
- collaborative: true;
24
- builtin: "Account";
25
- }
24
+ ? S extends AnyAccountSchema<infer Shape>
26
25
  ? {
27
26
  [key in keyof Shape]: InstanceOrPrimitiveOfSchema<Shape[key]>;
28
27
  } & Account
29
- : S extends z.core.$ZodRecord<infer K, infer V> & {
30
- collaborative: true;
31
- }
28
+ : S extends AnyCoRecordSchema<infer K, infer V>
32
29
  ? {
33
30
  [key in z.output<K> & string]: InstanceOrPrimitiveOfSchema<V>;
34
31
  } & CoMap
@@ -45,11 +42,11 @@ export type InstanceOfSchema<S extends CoValueClass | z.core.$ZodType> =
45
42
  ? CoList<InstanceOrPrimitiveOfSchema<T>>
46
43
  : S extends AnyCoFeedSchema<infer T>
47
44
  ? CoFeed<InstanceOrPrimitiveOfSchema<T>>
48
- : S extends PlainTextSchema
45
+ : S extends AnyPlainTextSchema
49
46
  ? CoPlainText
50
- : S extends RichTextSchema
47
+ : S extends AnyRichTextSchema
51
48
  ? CoRichText
52
- : S extends FileStreamSchema
49
+ : S extends AnyFileStreamSchema
53
50
  ? FileStream
54
51
  : S extends z.core.$ZodOptional<infer Inner>
55
52
  ? InstanceOrPrimitiveOfSchema<Inner>
@@ -1,5 +1,12 @@
1
1
  import { RawAccount, RawCoList, RawCoMap } from "cojson";
2
- import { zodSchemaToCoSchema } from "./runtimeConverters/zodSchemaToCoSchema.js";
2
+ import { CoValueClass, CoValueFromRaw } from "../../internal.js";
3
+ import {
4
+ isAnyCoValueSchema,
5
+ zodSchemaToCoSchema,
6
+ } from "./runtimeConverters/zodSchemaToCoSchema.js";
7
+ import { AccountSchema } from "./schemaTypes/AccountSchema.js";
8
+ import { CoListSchema } from "./schemaTypes/CoListSchema.js";
9
+ import { CoMapSchema } from "./schemaTypes/CoMapSchema.js";
3
10
  import { z } from "./zodReExport.js";
4
11
 
5
12
  export function schemaUnionDiscriminatorFor(
@@ -8,7 +15,7 @@ export function schemaUnionDiscriminatorFor(
8
15
  if (isUnionOfCoMapsDeeply(schema)) {
9
16
  if (!schema._zod.disc || schema._zod.disc.size == 0) {
10
17
  throw new Error(
11
- "z.union() of collaborative types is not supported, use z.discriminatedUnion() instead",
18
+ "z.union() of collaborative types is not supported, use co.discriminatedUnion() instead",
12
19
  );
13
20
  }
14
21
 
@@ -17,14 +24,14 @@ export function schemaUnionDiscriminatorFor(
17
24
 
18
25
  if (!field) {
19
26
  throw new Error(
20
- "z.discriminatedUnion() of collaborative types with non-existent discriminator key is not supported",
27
+ "co.discriminatedUnion() of collaborative types with non-existent discriminator key is not supported",
21
28
  );
22
29
  }
23
30
 
24
31
  for (const value of field.values) {
25
32
  if (typeof value !== "string" && typeof value !== "number") {
26
33
  throw new Error(
27
- "z.discriminatedUnion() of collaborative types with non-string or non-number discriminator value is not supported",
34
+ "co.discriminatedUnion() of collaborative types with non-string or non-number discriminator value is not supported",
28
35
  );
29
36
  }
30
37
  }
@@ -42,7 +49,7 @@ export function schemaUnionDiscriminatorFor(
42
49
  }
43
50
  } else {
44
51
  throw new Error(
45
- "Unsupported zod type in z.discriminatedUnion() of collaborative types",
52
+ "Unsupported zod type in co.discriminatedUnion() of collaborative types",
46
53
  );
47
54
  }
48
55
  }
@@ -50,7 +57,7 @@ export function schemaUnionDiscriminatorFor(
50
57
  const determineSchema = (_raw: RawCoMap | RawAccount | RawCoList) => {
51
58
  if (_raw instanceof RawCoList) {
52
59
  throw new Error(
53
- "z.discriminatedUnion() of collaborative types is not supported for CoLists",
60
+ "co.discriminatedUnion() of collaborative types is not supported for CoLists",
54
61
  );
55
62
  }
56
63
 
@@ -90,12 +97,17 @@ export function schemaUnionDiscriminatorFor(
90
97
  }
91
98
 
92
99
  if (match) {
93
- return zodSchemaToCoSchema(option);
100
+ const coValueSchema = zodSchemaToCoSchema(option) as
101
+ | CoMapSchema<any>
102
+ | AccountSchema
103
+ | CoListSchema<any>;
104
+ return coValueSchema.getCoValueClass() as CoValueClass<any> &
105
+ CoValueFromRaw<any>;
94
106
  }
95
107
  }
96
108
 
97
109
  throw new Error(
98
- "z.discriminatedUnion() of collaborative types with no matching discriminator value found",
110
+ "co.discriminatedUnion() of collaborative types with no matching discriminator value found",
99
111
  );
100
112
  };
101
113
 
@@ -120,11 +132,7 @@ export function isUnionOfCoMapsDeeply(
120
132
  function isCoMapOrUnionOfCoMapsDeeply(
121
133
  schema: z.core.$ZodType,
122
134
  ): schema is z.core.$ZodDiscriminatedUnion {
123
- if (
124
- schema instanceof z.core.$ZodObject &&
125
- "collaborative" in schema &&
126
- schema.collaborative
127
- ) {
135
+ if (schema instanceof z.core.$ZodObject && isAnyCoValueSchema(schema)) {
128
136
  return true;
129
137
  } else if (schema instanceof z.core.$ZodUnion) {
130
138
  return schema._zod.def.options.every(isCoMapOrUnionOfCoMapsDeeply);
@@ -137,6 +145,6 @@ export function isUnionOfPrimitivesDeeply(schema: z.core.$ZodType) {
137
145
  if (schema instanceof z.core.$ZodUnion) {
138
146
  return schema._zod.def.options.every(isUnionOfPrimitivesDeeply);
139
147
  } else {
140
- return !("collaborative" in schema);
148
+ return !isAnyCoValueSchema(schema);
141
149
  }
142
150
  }
@@ -1,8 +1,11 @@
1
1
  import {
2
- type Account,
3
- type AccountCreationProps,
4
2
  type AccountSchema,
5
- type AnyCoMapSchema,
3
+ AnyAccountSchema,
4
+ AnyCoFeedSchema,
5
+ AnyCoListSchema,
6
+ AnyCoSchema,
7
+ AnyFileStreamSchema,
8
+ AnyPlainTextSchema,
6
9
  BaseAccountShape,
7
10
  CoFeed,
8
11
  type CoFeedSchema,
@@ -20,121 +23,31 @@ import {
20
23
  type Simplify,
21
24
  zodSchemaToCoSchema,
22
25
  } from "../../internal.js";
23
- import { RichTextSchema } from "./schemaTypes/RichTextSchema.js";
26
+ import {
27
+ AnyDiscriminableCoSchema,
28
+ CoDiscriminatedUnionSchema,
29
+ } from "./schemaTypes/CoDiscriminatedUnionSchema.js";
30
+ import {
31
+ CoOptionalSchema,
32
+ createCoOptionalSchema,
33
+ } from "./schemaTypes/CoOptionalSchema.js";
34
+ import {
35
+ AnyRichTextSchema,
36
+ RichTextSchema,
37
+ } from "./schemaTypes/RichTextSchema.js";
24
38
  import { z } from "./zodReExport.js";
25
39
 
26
- function enrichCoMapSchema<Shape extends z.core.$ZodLooseShape>(
27
- schema: z.ZodObject<
28
- { -readonly [P in keyof Shape]: Shape[P] },
29
- z.core.$strip
30
- >,
31
- ) {
32
- const baseCatchall = schema.catchall;
33
-
34
- const enrichedSchema = Object.assign(schema, {
35
- collaborative: true,
36
- create: (...args: any[]) => {
37
- return coSchema.create(...args);
38
- },
39
- load: (...args: any[]) => {
40
- return coSchema.load(...args);
41
- },
42
- subscribe: (...args: any[]) => {
43
- return coSchema.subscribe(...args);
44
- },
45
- findUnique: (...args: any[]) => {
46
- return coSchema.findUnique(...args);
47
- },
48
- upsertUnique: (...args: any[]) => {
49
- return coSchema.upsertUnique(...args);
50
- },
51
- loadUnique: (...args: any[]) => {
52
- return coSchema.loadUnique(...args);
53
- },
54
- catchall: (index: z.core.$ZodType) => {
55
- return enrichCoMapSchema(baseCatchall(index));
56
- },
57
- withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
58
- return Object.assign(schema, helpers(schema));
59
- },
60
- withMigration: (migration: (value: any) => undefined) => {
61
- coSchema.prototype.migrate = migration;
62
-
63
- return enrichedSchema;
64
- },
65
- getCoSchema: () => {
66
- return coSchema;
67
- },
68
- }) as unknown as CoMapSchema<Shape>;
69
-
70
- // Needs to be derived from the enriched schema
71
- const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
72
-
73
- return enrichedSchema;
74
- }
75
-
76
40
  export const coMapDefiner = <Shape extends z.core.$ZodLooseShape>(
77
41
  shape: Shape,
78
42
  ): CoMapSchema<Shape> => {
79
43
  const objectSchema = z.object(shape).meta({
80
44
  collaborative: true,
81
45
  });
82
-
83
- return enrichCoMapSchema(objectSchema);
84
- };
85
-
86
- function enrichAccountSchema<Shape extends BaseAccountShape>(
87
- schema: z.ZodObject<Shape, z.core.$strip>,
88
- ) {
89
- const enrichedSchema = Object.assign(schema, {
46
+ const enrichedSchema = Object.assign(objectSchema, {
90
47
  collaborative: true,
91
- builtin: "Account",
92
- create: (...args: any[]) => {
93
- return coSchema.create(...args);
94
- },
95
- createAs: (...args: any[]) => {
96
- return coSchema.createAs(...args);
97
- },
98
- getMe: (...args: any[]) => {
99
- return coSchema.getMe(...args);
100
- },
101
- load: (...args: any[]) => {
102
- return coSchema.load(...args);
103
- },
104
- subscribe: (...args: any[]) => {
105
- return coSchema.subscribe(...args);
106
- },
107
- withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
108
- return Object.assign(schema, helpers(schema));
109
- },
110
- fromRaw: (...args: any[]) => {
111
- return coSchema.fromRaw(...args);
112
- },
113
- withMigration: (
114
- migration: (
115
- value: any,
116
- creationProps?: AccountCreationProps,
117
- ) => void | Promise<void>,
118
- ) => {
119
- (coSchema.prototype as Account).migrate = async function (
120
- this,
121
- creationProps,
122
- ) {
123
- await migration(this, creationProps);
124
- };
125
-
126
- return enrichedSchema;
127
- },
128
- getCoSchema: () => {
129
- return coSchema;
130
- },
131
- }) as unknown as AccountSchema<Shape>;
132
-
133
- // Needs to be derived from the enriched schema
134
- const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
135
-
136
- return enrichedSchema;
137
- }
48
+ });
49
+ return zodSchemaToCoSchema(enrichedSchema);
50
+ };
138
51
 
139
52
  /**
140
53
  * Defines a collaborative account schema for Jazz applications.
@@ -183,11 +96,14 @@ export const coAccountDefiner = <Shape extends BaseAccountShape>(
183
96
  root: coMapDefiner({}),
184
97
  } as unknown as Shape,
185
98
  ): AccountSchema<Shape> => {
186
- const objectSchema = z.object(shape).meta({
99
+ const schema = z.object(shape).meta({
187
100
  collaborative: true,
188
101
  });
189
-
190
- return enrichAccountSchema(objectSchema) as unknown as AccountSchema<Shape>;
102
+ const enrichedSchema = Object.assign(schema, {
103
+ collaborative: true,
104
+ builtin: "Account",
105
+ }) as AnyAccountSchema<Shape>;
106
+ return zodSchemaToCoSchema(enrichedSchema);
191
107
  };
192
108
 
193
109
  export const coRecordDefiner = <
@@ -203,190 +119,86 @@ export const coRecordDefiner = <
203
119
  >;
204
120
  };
205
121
 
206
- function enrichCoListSchema<T extends z.core.$ZodType>(schema: z.ZodArray<T>) {
207
- const enrichedSchema = Object.assign(schema, {
208
- collaborative: true,
209
- create: (...args: any[]) => {
210
- return coSchema.create(...args);
211
- },
212
- load: (...args: any[]) => {
213
- return coSchema.load(...args);
214
- },
215
- subscribe: (...args: any[]) => {
216
- return coSchema.subscribe(...args);
217
- },
218
- withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
219
- return Object.assign(schema, helpers(schema));
220
- },
221
- getCoSchema: () => {
222
- return coSchema;
223
- },
224
- }) as unknown as CoListSchema<T>;
225
-
226
- // Needs to be derived from the enriched schema
227
- const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
228
-
229
- return enrichedSchema;
230
- }
231
-
232
122
  export const coListDefiner = <T extends z.core.$ZodType>(
233
123
  element: T,
234
124
  ): CoListSchema<T> => {
235
- const arraySchema = z.array(element).meta({
125
+ const schema = z.array(element).meta({
236
126
  collaborative: true,
237
127
  });
238
-
239
- return enrichCoListSchema(arraySchema);
128
+ const enrichedSchema = Object.assign(schema, {
129
+ collaborative: true,
130
+ }) as AnyCoListSchema<T>;
131
+ return zodSchemaToCoSchema(enrichedSchema) as unknown as CoListSchema<T>;
240
132
  };
241
133
 
242
134
  export const coProfileDefiner = <
243
135
  Shape extends z.core.$ZodLooseShape = Simplify<DefaultProfileShape>,
244
136
  >(
245
- shape: Shape & {
246
- name?: z.core.$ZodString<string>;
247
- inbox?: z.core.$ZodOptional<z.core.$ZodString>;
248
- inboxInvite?: z.core.$ZodOptional<z.core.$ZodString>;
249
- } = {} as any,
137
+ shape: Shape & Partial<DefaultProfileShape> = {} as any,
250
138
  ): CoProfileSchema<Shape> => {
251
- const ehnancedShape = Object.assign(shape ?? {}, {
139
+ const ehnancedShape = Object.assign(shape, {
252
140
  name: z.string(),
253
141
  inbox: z.optional(z.string()),
254
142
  inboxInvite: z.optional(z.string()),
255
143
  });
256
-
257
144
  return coMapDefiner(ehnancedShape) as CoProfileSchema<Shape>;
258
145
  };
259
146
 
260
- function enrichCoFeedSchema<T extends z.core.$ZodType>(
261
- schema: z.ZodCustom<CoFeed<unknown>, unknown>,
147
+ export const coFeedDefiner = <T extends z.core.$ZodType>(
262
148
  element: T,
263
- ) {
149
+ ): CoFeedSchema<T> => {
150
+ const schema = z.instanceof(CoFeed);
264
151
  const enrichedSchema = Object.assign(schema, {
265
152
  collaborative: true,
266
153
  builtin: "CoFeed",
267
154
  element,
268
- create: (...args: any[]) => {
269
- return coSchema.create(...args);
270
- },
271
- load: (...args: any[]) => {
272
- return coSchema.load(...args);
273
- },
274
- subscribe: (...args: any[]) => {
275
- return coSchema.subscribe(...args);
276
- },
277
- withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
278
- return Object.assign(schema, helpers(schema));
279
- },
280
- getCoSchema: () => {
281
- return coSchema;
282
- },
283
- }) as unknown as CoFeedSchema<T>;
284
-
285
- // Needs to be derived from the enriched schema
286
- const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
287
-
288
- return enrichedSchema;
289
- }
290
-
291
- export const coFeedDefiner = <T extends z.core.$ZodType>(
292
- element: T,
293
- ): CoFeedSchema<T> => {
294
- return enrichCoFeedSchema(z.instanceof(CoFeed), element);
155
+ }) as AnyCoFeedSchema<T>;
156
+ return zodSchemaToCoSchema(enrichedSchema);
295
157
  };
296
158
 
297
- function enrichFileStreamSchema(schema: z.ZodCustom<FileStream, unknown>) {
159
+ export const coFileStreamDefiner = (): FileStreamSchema => {
160
+ const schema = z.instanceof(FileStream);
298
161
  const enrichedSchema = Object.assign(schema, {
299
162
  collaborative: true,
300
163
  builtin: "FileStream",
301
- create: (...args: any[]) => {
302
- return coSchema.create(...args);
303
- },
304
- createFromBlob: (...args: any[]) => {
305
- return coSchema.createFromBlob(...args);
306
- },
307
- load: (...args: any[]) => {
308
- return coSchema.load(...args);
309
- },
310
- loadAsBlob: (...args: any[]) => {
311
- return coSchema.loadAsBlob(...args);
312
- },
313
- subscribe: (...args: any[]) => {
314
- return coSchema.subscribe(...args);
315
- },
316
- getCoSchema: () => {
317
- return coSchema;
318
- },
319
- }) as unknown as FileStreamSchema;
320
-
321
- // Needs to be derived from the enriched schema
322
- const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
323
-
324
- return enrichedSchema;
325
- }
326
-
327
- export const coFileStreamDefiner = (): FileStreamSchema => {
328
- return enrichFileStreamSchema(z.instanceof(FileStream));
164
+ }) as AnyFileStreamSchema;
165
+ return zodSchemaToCoSchema(enrichedSchema);
329
166
  };
330
167
 
331
- function enrichPlainTextSchema(schema: z.ZodCustom<CoPlainText, unknown>) {
168
+ export const coPlainTextDefiner = (): PlainTextSchema => {
169
+ const schema = z.instanceof(CoPlainText);
332
170
  const enrichedSchema = Object.assign(schema, {
333
171
  collaborative: true,
334
172
  builtin: "CoPlainText",
335
- create: (...args: any[]) => {
336
- return coSchema.create(...args);
337
- },
338
- load: (...args: any[]) => {
339
- return coSchema.load(...args);
340
- },
341
- subscribe: (...args: any[]) => {
342
- return coSchema.subscribe(...args);
343
- },
344
- fromRaw: (...args: any[]) => {
345
- return coSchema.fromRaw(...args);
346
- },
347
- getCoSchema: () => {
348
- return coSchema;
349
- },
350
- }) as unknown as PlainTextSchema;
351
-
352
- // Needs to be derived from the enriched schema
353
- const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
354
-
355
- return enrichedSchema;
356
- }
357
-
358
- export const coPlainTextDefiner = (): PlainTextSchema => {
359
- return enrichPlainTextSchema(z.instanceof(CoPlainText));
173
+ }) as AnyPlainTextSchema;
174
+ return zodSchemaToCoSchema(enrichedSchema);
360
175
  };
361
176
 
362
- function enrichRichTextSchema(schema: z.ZodCustom<CoRichText, unknown>) {
177
+ export const coRichTextDefiner = (): RichTextSchema => {
178
+ const schema = z.instanceof(CoRichText);
363
179
  const enrichedSchema = Object.assign(schema, {
364
180
  collaborative: true,
365
181
  builtin: "CoRichText",
366
- create: (...args: any[]) => {
367
- return coSchema.create(...args);
368
- },
369
- load: (...args: any[]) => {
370
- return coSchema.load(...args);
371
- },
372
- subscribe: (...args: any[]) => {
373
- return coSchema.subscribe(...args);
374
- },
375
- getCoSchema: () => {
376
- return coSchema;
377
- },
378
- }) as unknown as RichTextSchema;
379
-
380
- // Needs to be derived from the enriched schema
381
- const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
382
-
383
- return enrichedSchema;
384
- }
385
-
386
- export const coRichTextDefiner = (): RichTextSchema => {
387
- return enrichRichTextSchema(z.instanceof(CoRichText));
182
+ }) as AnyRichTextSchema;
183
+ return zodSchemaToCoSchema(enrichedSchema);
388
184
  };
389
185
 
390
186
  export const coImageDefiner = (): typeof ImageDefinition => {
391
187
  return ImageDefinition;
392
188
  };
189
+
190
+ export const coOptionalDefiner = <T extends AnyCoSchema>(
191
+ schema: T,
192
+ ): CoOptionalSchema<T> => {
193
+ return createCoOptionalSchema(schema);
194
+ };
195
+
196
+ export const coDiscriminatedUnionDefiner = <
197
+ T extends readonly [AnyDiscriminableCoSchema, ...AnyDiscriminableCoSchema[]],
198
+ >(
199
+ discriminator: string,
200
+ schemas: T,
201
+ ): CoDiscriminatedUnionSchema<T> => {
202
+ const schema = z.discriminatedUnion(discriminator, schemas as any);
203
+ return zodSchemaToCoSchema(schema);
204
+ };