jazz-tools 0.16.2 → 0.16.3

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 (73) hide show
  1. package/.turbo/turbo-build.log +33 -33
  2. package/CHANGELOG.md +11 -0
  3. package/dist/{chunk-CL3ROOZM.js → chunk-RAMDW4VL.js} +83 -29
  4. package/dist/chunk-RAMDW4VL.js.map +1 -0
  5. package/dist/index.js +1 -1
  6. package/dist/react-core/hooks.d.ts +150 -2
  7. package/dist/react-core/hooks.d.ts.map +1 -1
  8. package/dist/react-core/index.js.map +1 -1
  9. package/dist/testing.js +1 -1
  10. package/dist/tools/coValues/coFeed.d.ts.map +1 -1
  11. package/dist/tools/coValues/coList.d.ts.map +1 -1
  12. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  13. package/dist/tools/coValues/schemaUnion.d.ts +7 -2
  14. package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
  15. package/dist/tools/exports.d.ts +1 -1
  16. package/dist/tools/exports.d.ts.map +1 -1
  17. package/dist/tools/implementation/schema.d.ts +12 -2
  18. package/dist/tools/implementation/schema.d.ts.map +1 -1
  19. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +2 -4
  20. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  21. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +2 -4
  22. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  23. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +3 -6
  24. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  25. package/dist/tools/implementation/zodSchema/typeConverters/CoFieldInit.d.ts +20 -6
  26. package/dist/tools/implementation/zodSchema/typeConverters/CoFieldInit.d.ts.map +1 -1
  27. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +4 -4
  28. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
  29. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +3 -3
  30. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -1
  31. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +3 -25
  32. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -1
  33. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +3 -25
  34. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -1
  35. package/dist/tools/implementation/zodSchema/typeConverters/TypeOfZodSchema.d.ts +24 -0
  36. package/dist/tools/implementation/zodSchema/typeConverters/TypeOfZodSchema.d.ts.map +1 -0
  37. package/dist/tools/implementation/zodSchema/unionUtils.d.ts +2 -3
  38. package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
  39. package/dist/tools/implementation/zodSchema/zodSchema.d.ts +3 -4
  40. package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
  41. package/dist/tools/internal.d.ts +1 -0
  42. package/dist/tools/internal.d.ts.map +1 -1
  43. package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
  44. package/package.json +4 -4
  45. package/src/react-core/hooks.ts +143 -0
  46. package/src/tools/coValues/coFeed.ts +12 -4
  47. package/src/tools/coValues/coList.ts +41 -19
  48. package/src/tools/coValues/coMap.ts +12 -2
  49. package/src/tools/coValues/inbox.ts +1 -1
  50. package/src/tools/coValues/schemaUnion.ts +28 -3
  51. package/src/tools/exports.ts +0 -1
  52. package/src/tools/implementation/schema.ts +28 -2
  53. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +2 -7
  54. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +2 -7
  55. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +2 -10
  56. package/src/tools/implementation/zodSchema/typeConverters/CoFieldInit.ts +74 -9
  57. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +9 -4
  58. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +3 -3
  59. package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +3 -114
  60. package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +5 -129
  61. package/src/tools/implementation/zodSchema/typeConverters/TypeOfZodSchema.ts +79 -0
  62. package/src/tools/implementation/zodSchema/unionUtils.ts +6 -9
  63. package/src/tools/implementation/zodSchema/zodSchema.ts +3 -13
  64. package/src/tools/internal.ts +1 -0
  65. package/src/tools/subscribe/SubscriptionScope.ts +4 -2
  66. package/src/tools/subscribe/utils.ts +2 -2
  67. package/src/tools/tests/coFeed.test.ts +40 -0
  68. package/src/tools/tests/coList.test.ts +42 -0
  69. package/src/tools/tests/coMap.test-d.ts +41 -1
  70. package/src/tools/tests/coMap.test.ts +82 -5
  71. package/src/tools/tests/groupsAndAccounts.test.ts +54 -0
  72. package/src/tools/tests/zod.test.ts +16 -0
  73. package/dist/chunk-CL3ROOZM.js.map +0 -1
@@ -29,6 +29,7 @@ import {
29
29
  coValuesCache,
30
30
  ensureCoValueLoaded,
31
31
  inspect,
32
+ instantiateRefEncodedWithInit,
32
33
  isRefEncoded,
33
34
  loadCoValueWithoutMe,
34
35
  makeRefs,
@@ -240,7 +241,7 @@ export class CoList<out Item = any> extends Array<Item> implements CoValue {
240
241
  const { owner } = parseCoValueCreateOptions(options);
241
242
  const instance = new this({ init: items, owner });
242
243
  const raw = owner._raw.createList(
243
- toRawItems(items, instance._schema[ItemsSym]),
244
+ toRawItems(items, instance._schema[ItemsSym], owner),
244
245
  );
245
246
 
246
247
  Object.defineProperties(instance, {
@@ -256,7 +257,7 @@ export class CoList<out Item = any> extends Array<Item> implements CoValue {
256
257
 
257
258
  push(...items: Item[]): number {
258
259
  this._raw.appendItems(
259
- toRawItems(items, this._schema[ItemsSym]),
260
+ toRawItems(items, this._schema[ItemsSym], this._owner),
260
261
  undefined,
261
262
  "private",
262
263
  );
@@ -265,7 +266,11 @@ export class CoList<out Item = any> extends Array<Item> implements CoValue {
265
266
  }
266
267
 
267
268
  unshift(...items: Item[]): number {
268
- for (const item of toRawItems(items as Item[], this._schema[ItemsSym])) {
269
+ for (const item of toRawItems(
270
+ items as Item[],
271
+ this._schema[ItemsSym],
272
+ this._owner,
273
+ )) {
269
274
  this._raw.prepend(item);
270
275
  }
271
276
 
@@ -306,7 +311,11 @@ export class CoList<out Item = any> extends Array<Item> implements CoValue {
306
311
  this._raw.delete(idxToDelete);
307
312
  }
308
313
 
309
- const rawItems = toRawItems(items as Item[], this._schema[ItemsSym]);
314
+ const rawItems = toRawItems(
315
+ items as Item[],
316
+ this._schema[ItemsSym],
317
+ this._owner,
318
+ );
310
319
 
311
320
  // If there are no items to insert, return the deleted items
312
321
  if (rawItems.length === 0) {
@@ -551,23 +560,36 @@ export class CoList<out Item = any> extends Array<Item> implements CoValue {
551
560
  * Convert an array of items to a raw array of items.
552
561
  * @param items - The array of items to convert.
553
562
  * @param itemDescriptor - The descriptor of the items.
563
+ * @param owner - The owner of the CoList.
554
564
  * @returns The raw array of items.
555
565
  */
556
- function toRawItems<Item>(items: Item[], itemDescriptor: Schema) {
557
- const rawItems =
558
- itemDescriptor === "json"
559
- ? (items as JsonValue[])
560
- : "encoded" in itemDescriptor
561
- ? items?.map((e) => itemDescriptor.encoded.encode(e))
562
- : isRefEncoded(itemDescriptor)
563
- ? items?.map((v) => {
564
- if (!v) return null;
565
-
566
- return (v as unknown as CoValue).id;
567
- })
568
- : (() => {
569
- throw new Error("Invalid element descriptor");
570
- })();
566
+ function toRawItems<Item>(
567
+ items: Item[],
568
+ itemDescriptor: Schema,
569
+ owner: Account | Group,
570
+ ) {
571
+ let rawItems: JsonValue[] = [];
572
+ if (itemDescriptor === "json") {
573
+ rawItems = items as JsonValue[];
574
+ } else if ("encoded" in itemDescriptor) {
575
+ rawItems = items?.map((e) => itemDescriptor.encoded.encode(e));
576
+ } else if (isRefEncoded(itemDescriptor)) {
577
+ rawItems = items?.map((value) => {
578
+ if (value == null) return null;
579
+ let refId = (value as unknown as CoValue).id;
580
+ if (!refId) {
581
+ const coValue = instantiateRefEncodedWithInit(
582
+ itemDescriptor,
583
+ value,
584
+ owner,
585
+ );
586
+ refId = coValue.id;
587
+ }
588
+ return refId;
589
+ });
590
+ } else {
591
+ throw new Error("Invalid element descriptor");
592
+ }
571
593
  return rawItems;
572
594
  }
573
595
 
@@ -37,6 +37,7 @@ import {
37
37
  activeAccountContext,
38
38
  ensureCoValueLoaded,
39
39
  inspect,
40
+ instantiateRefEncodedWithInit,
40
41
  isRefEncoded,
41
42
  loadCoValueWithoutMe,
42
43
  makeRefs,
@@ -424,8 +425,17 @@ export class CoMap extends CoValueBase implements CoValue {
424
425
  if (descriptor === "json") {
425
426
  rawInit[key] = initValue as JsonValue;
426
427
  } else if (isRefEncoded(descriptor)) {
427
- if (initValue) {
428
- rawInit[key] = (initValue as unknown as CoValue).id;
428
+ if (initValue != null) {
429
+ let refId = (initValue as unknown as CoValue).id;
430
+ if (!refId) {
431
+ const coValue = instantiateRefEncodedWithInit(
432
+ descriptor,
433
+ initValue,
434
+ owner,
435
+ );
436
+ refId = coValue.id;
437
+ }
438
+ rawInit[key] = refId;
429
439
  }
430
440
  } else if ("encoded" in descriptor) {
431
441
  rawInit[key] = descriptor.encoded.encode(
@@ -145,7 +145,7 @@ export class Inbox {
145
145
 
146
146
  for (const [sessionID, items] of Object.entries(stream.items) as [
147
147
  SessionID,
148
- CoStreamItem<CoID<InboxMessage<InstanceOfSchema<M>, O>>>[],
148
+ CoStreamItem<CoID<InboxMessage<NonNullable<InstanceOfSchema<M>>, O>>>[],
149
149
  ][]) {
150
150
  const accountID = getAccountIDfromSessionID(sessionID);
151
151
 
@@ -1,12 +1,16 @@
1
+ import { JsonValue, RawCoMap } from "cojson";
1
2
  import {
2
3
  Account,
3
4
  AnonymousJazzAgent,
5
+ CoMapInit,
4
6
  CoValue,
5
7
  CoValueBase,
6
8
  CoValueClass,
7
9
  CoValueFromRaw,
10
+ Group,
8
11
  ID,
9
12
  Resolved,
13
+ Simplify,
10
14
  SubscribeListenerOptions,
11
15
  SubscribeRestArgs,
12
16
  loadCoValueWithoutMe,
@@ -20,6 +24,10 @@ import {
20
24
  export type SchemaUnionConcreteSubclass<V extends CoValue> =
21
25
  typeof SchemaUnion & CoValueClass<V>;
22
26
 
27
+ export type SchemaUnionDiscriminator<V extends CoValue> = (discriminable: {
28
+ get(key: string): JsonValue | undefined;
29
+ }) => CoValueClass<V> & CoValueFromRaw<V>;
30
+
23
31
  /**
24
32
  * SchemaUnion allows you to create union types of CoValues that can be discriminated at runtime.
25
33
  *
@@ -89,28 +97,45 @@ export abstract class SchemaUnion extends CoValueBase implements CoValue {
89
97
  * @category Declaration
90
98
  **/
91
99
  static Of<V extends CoValue>(
92
- discriminator: (raw: V["_raw"]) => CoValueClass<V> & CoValueFromRaw<V>,
100
+ discriminator: SchemaUnionDiscriminator<V>,
93
101
  ): SchemaUnionConcreteSubclass<V> {
94
102
  return class SchemaUnionClass extends SchemaUnion {
103
+ static override create<V extends CoValue>(
104
+ this: CoValueClass<V>,
105
+ init: Simplify<CoMapInit<V>>,
106
+ owner: Account | Group,
107
+ ): V {
108
+ const ResolvedClass = discriminator(new Map(Object.entries(init)));
109
+ // @ts-expect-error - create is a static method in the CoMap class
110
+ return ResolvedClass.create(init, owner);
111
+ }
112
+
95
113
  static override fromRaw<T extends CoValue>(
96
114
  this: CoValueClass<T> & CoValueFromRaw<T>,
97
115
  raw: T["_raw"],
98
116
  ): T {
99
117
  const ResolvedClass = discriminator(
100
- raw as V["_raw"],
118
+ raw as RawCoMap,
101
119
  ) as unknown as CoValueClass<T> & CoValueFromRaw<T>;
102
120
  return ResolvedClass.fromRaw(raw);
103
121
  }
104
122
  } as unknown as SchemaUnionConcreteSubclass<V>;
105
123
  }
106
124
 
125
+ static create<V extends CoValue>(
126
+ this: CoValueClass<V>,
127
+ init: Simplify<CoMapInit<V>>,
128
+ owner: Account | Group,
129
+ ): V {
130
+ throw new Error("Not implemented");
131
+ }
132
+
107
133
  /**
108
134
  * Create an instance from raw data. This is called internally and should not be used directly.
109
135
  * Use {@link SchemaUnion.Of} to create a union type instead.
110
136
  *
111
137
  * @internal
112
138
  */
113
- // @ts-ignore
114
139
  static fromRaw<V extends CoValue>(this: CoValueClass<V>, raw: V["_raw"]): V {
115
140
  throw new Error("Not implemented");
116
141
  }
@@ -107,7 +107,6 @@ export {
107
107
  type CoreAccountSchema as AnyAccountSchema,
108
108
  type ResolveQuery,
109
109
  type ResolveQueryStrict,
110
- type InitFor,
111
110
  } from "./internal.js";
112
111
 
113
112
  export {
@@ -1,11 +1,12 @@
1
1
  import type { JsonValue, RawCoValue } from "cojson";
2
2
  import { CojsonInternalTypes } from "cojson";
3
3
  import {
4
+ Account,
4
5
  type CoValue,
5
6
  type CoValueClass,
6
7
  CoValueFromRaw,
8
+ Group,
7
9
  ItemsSym,
8
- JazzToolsSymbol,
9
10
  SchemaInit,
10
11
  isCoValueClass,
11
12
  } from "../internal.js";
@@ -140,7 +141,7 @@ export function isRefEncoded<V extends CoValue>(
140
141
  );
141
142
  }
142
143
 
143
- export function instantiateRefEncoded<V extends CoValue>(
144
+ export function instantiateRefEncodedFromRaw<V extends CoValue>(
144
145
  schema: RefEncoded<V>,
145
146
  raw: RawCoValue,
146
147
  ): V {
@@ -151,6 +152,31 @@ export function instantiateRefEncoded<V extends CoValue>(
151
152
  ).fromRaw(raw);
152
153
  }
153
154
 
155
+ /**
156
+ * Creates a new CoValue of the given ref type, using the provided init values.
157
+ *
158
+ * @param schema - The schema of the CoValue to create.
159
+ * @param init - The init values to use to create the CoValue.
160
+ * @param parentOwner - The owner of the referencing CoValue. Will be used
161
+ * as the parent group of the created CoValue's group
162
+ * @returns The created CoValue.
163
+ */
164
+ export function instantiateRefEncodedWithInit<V extends CoValue>(
165
+ schema: RefEncoded<V>,
166
+ init: any,
167
+ parentOwner: Account | Group,
168
+ ): V {
169
+ if (!isCoValueClass<V>(schema.ref)) {
170
+ throw Error(
171
+ `Cannot automatically create CoValue from value: ${JSON.stringify(init)}. Use the CoValue schema's create() method instead.`,
172
+ );
173
+ }
174
+ const owner = Group.create();
175
+ owner.addMember(parentOwner.castAs(Group));
176
+ // @ts-expect-error - create is a static method in all CoValue classes
177
+ return schema.ref.create(init, owner);
178
+ }
179
+
154
180
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
155
181
  export type Schema = JsonEncoded | RefEncoded<CoValue> | EncodedAs<any>;
156
182
 
@@ -6,21 +6,16 @@ import {
6
6
  RefsToResolve,
7
7
  RefsToResolveStrict,
8
8
  Resolved,
9
- Simplify,
10
9
  SubscribeListenerOptions,
11
10
  coOptionalDefiner,
12
11
  } from "../../../internal.js";
13
12
  import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
14
- import { CoFieldInit } from "../typeConverters/CoFieldInit.js";
13
+ import { CoFeedInit } from "../typeConverters/CoFieldInit.js";
15
14
  import { InstanceOrPrimitiveOfSchema } from "../typeConverters/InstanceOrPrimitiveOfSchema.js";
16
15
  import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "../typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
17
16
  import { CoOptionalSchema } from "./CoOptionalSchema.js";
18
17
  import { CoreCoValueSchema } from "./CoValueSchema.js";
19
18
 
20
- type CoFeedInit<T extends AnyZodOrCoValueSchema> = Simplify<
21
- Array<CoFieldInit<T>>
22
- >;
23
-
24
19
  export class CoFeedSchema<T extends AnyZodOrCoValueSchema>
25
20
  implements CoreCoFeedSchema<T>
26
21
  {
@@ -36,7 +31,7 @@ export class CoFeedSchema<T extends AnyZodOrCoValueSchema>
36
31
  init: CoFeedInit<T>,
37
32
  options?: { owner: Account | Group } | Account | Group,
38
33
  ): CoFeedInstance<T> {
39
- return this.coValueClass.create(init, options) as CoFeedInstance<T>;
34
+ return this.coValueClass.create(init as any, options) as CoFeedInstance<T>;
40
35
  }
41
36
 
42
37
  load<const R extends RefsToResolve<CoFeedInstanceCoValuesNullable<T>> = true>(
@@ -5,22 +5,17 @@ import {
5
5
  RefsToResolve,
6
6
  RefsToResolveStrict,
7
7
  Resolved,
8
- Simplify,
9
8
  SubscribeListenerOptions,
10
9
  coOptionalDefiner,
11
10
  } from "../../../internal.js";
12
11
  import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
13
- import { CoFieldInit } from "../typeConverters/CoFieldInit.js";
12
+ import { CoListInit } from "../typeConverters/CoFieldInit.js";
14
13
  import { InstanceOrPrimitiveOfSchema } from "../typeConverters/InstanceOrPrimitiveOfSchema.js";
15
14
  import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "../typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
16
15
  import { AnyZodOrCoValueSchema } from "../zodSchema.js";
17
16
  import { CoOptionalSchema } from "./CoOptionalSchema.js";
18
17
  import { CoreCoValueSchema } from "./CoValueSchema.js";
19
18
 
20
- type CoListInit<T extends AnyZodOrCoValueSchema> = Simplify<
21
- Array<CoFieldInit<T>>
22
- >;
23
-
24
19
  export class CoListSchema<T extends AnyZodOrCoValueSchema>
25
20
  implements CoreCoListSchema<T>
26
21
  {
@@ -36,7 +31,7 @@ export class CoListSchema<T extends AnyZodOrCoValueSchema>
36
31
  items: CoListInit<T>,
37
32
  options?: { owner: Account | Group } | Account | Group,
38
33
  ): CoListInstance<T> {
39
- return this.coValueClass.create(items, options) as CoListInstance<T>;
34
+ return this.coValueClass.create(items as any, options) as CoListInstance<T>;
40
35
  }
41
36
 
42
37
  load<const R extends RefsToResolve<CoListInstanceCoValuesNullable<T>> = true>(
@@ -5,7 +5,6 @@ import {
5
5
  DiscriminableCoValueSchemaDefinition,
6
6
  DiscriminableCoreCoValueSchema,
7
7
  Group,
8
- PartialOnUndefined,
9
8
  RefsToResolve,
10
9
  RefsToResolveStrict,
11
10
  Resolved,
@@ -17,7 +16,7 @@ import {
17
16
  } from "../../../internal.js";
18
17
  import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
19
18
  import { removeGetters } from "../../schemaUtils.js";
20
- import { CoFieldInit } from "../typeConverters/CoFieldInit.js";
19
+ import { CoMapSchemaInit } from "../typeConverters/CoFieldInit.js";
21
20
  import { InstanceOrPrimitiveOfSchema } from "../typeConverters/InstanceOrPrimitiveOfSchema.js";
22
21
  import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "../typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
23
22
  import { z } from "../zodReExport.js";
@@ -30,7 +29,7 @@ export interface CoMapSchema<
30
29
  Owner extends Account | Group = Account | Group,
31
30
  > extends CoreCoMapSchema<Shape, CatchAll> {
32
31
  create: (
33
- init: Simplify<CoMapSchemaInit<Shape>>,
32
+ init: CoMapSchemaInit<Shape>,
34
33
  options?:
35
34
  | {
36
35
  owner: Owner;
@@ -227,13 +226,6 @@ export function enrichCoMapSchema<
227
226
  return coValueSchema;
228
227
  }
229
228
 
230
- // Due to a TS limitation with types that contain known properties and
231
- // an index signature, we cannot accept catchall properties on creation
232
- export type CoMapSchemaInit<Shape extends z.core.$ZodLooseShape> =
233
- PartialOnUndefined<{
234
- [key in keyof Shape]: CoFieldInit<Shape[key]>;
235
- }>;
236
-
237
229
  export interface CoMapSchemaDefinition<
238
230
  Shape extends z.core.$ZodLooseShape = z.core.$ZodLooseShape,
239
231
  CatchAll extends AnyZodOrCoValueSchema | unknown = unknown,
@@ -1,13 +1,78 @@
1
- import { NotNull } from "../../../internal.js";
1
+ import {
2
+ CoDiscriminatedUnionSchema,
3
+ CoValueClass,
4
+ CoreCoFeedSchema,
5
+ CoreCoListSchema,
6
+ CoreCoMapSchema,
7
+ CoreCoRecordSchema,
8
+ CorePlainTextSchema,
9
+ PartialOnUndefined,
10
+ Simplify,
11
+ } from "../../../internal.js";
12
+ import { CoreCoOptionalSchema } from "../schemaTypes/CoOptionalSchema.js";
13
+ import { CoreCoValueSchema } from "../schemaTypes/CoValueSchema.js";
14
+ import { CoreRichTextSchema } from "../schemaTypes/RichTextSchema.js";
2
15
  import { z } from "../zodReExport.js";
3
- import { AnyZodOrCoValueSchema } from "../zodSchema.js";
4
- import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "./InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
16
+ import { AnyZodOrCoValueSchema, Loaded } from "../zodSchema.js";
17
+ import { TypeOfZodSchema } from "./TypeOfZodSchema.js";
5
18
 
6
19
  /**
7
- * Returns the type of the value that should be used to initialize a coField
8
- * of the given schema.
20
+ * The type of value that can be used to initialize a CoField of the given schema.
21
+ *
22
+ * For CoValue fields, this can be either a shallowly-loaded CoValue instance
23
+ * or a JSON object that will be used to create the CoValue.
9
24
  */
10
- export type CoFieldInit<T extends AnyZodOrCoValueSchema> =
11
- T extends z.core.$ZodNullable
12
- ? InstanceOrPrimitiveOfSchemaCoValuesNullable<T>
13
- : NotNull<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>>;
25
+ export type CoFieldInit<S extends CoValueClass | AnyZodOrCoValueSchema> =
26
+ S extends CoreCoValueSchema
27
+ ?
28
+ | Loaded<S>
29
+ | (S extends CoreCoRecordSchema<infer K, infer V>
30
+ ? CoMapSchemaInit<{ [key in z.output<K> & string]: V }>
31
+ : S extends CoreCoMapSchema<infer Shape>
32
+ ? CoMapSchemaInit<Shape>
33
+ : S extends CoreCoListSchema<infer T>
34
+ ? CoListInit<T>
35
+ : S extends CoreCoFeedSchema<infer T>
36
+ ? CoFeedInit<T>
37
+ : S extends CorePlainTextSchema | CoreRichTextSchema
38
+ ? string
39
+ : S extends CoreCoOptionalSchema<infer T>
40
+ ? CoFieldInit<T> | undefined
41
+ : S extends CoDiscriminatedUnionSchema<infer Members>
42
+ ? CoFieldInit<Members[number]>
43
+ : never)
44
+ : S extends z.core.$ZodType
45
+ ? TypeOfZodSchema<S>
46
+ : S extends CoValueClass
47
+ ? InstanceType<S>
48
+ : never;
49
+
50
+ // Due to a TS limitation with types that contain known properties and
51
+ // an index signature, we cannot accept catchall properties on creation
52
+ export type CoMapSchemaInit<Shape extends z.core.$ZodLooseShape> = Simplify<
53
+ {
54
+ /**
55
+ * Cannot use {@link PartialOnUndefined} because evaluating CoFieldInit<Shape[Key]>
56
+ * to know if the value can be undefined does not work with recursive types.
57
+ */
58
+ [Key in keyof Shape as Shape[Key] extends
59
+ | CoreCoOptionalSchema
60
+ | z.core.$ZodOptional
61
+ ? never
62
+ : Key]: CoFieldInit<Shape[Key]>;
63
+ } & {
64
+ [Key in keyof Shape as Shape[Key] extends
65
+ | CoreCoOptionalSchema
66
+ | z.core.$ZodOptional
67
+ ? Key
68
+ : never]?: CoFieldInit<Shape[Key]>;
69
+ }
70
+ >;
71
+
72
+ export type CoListInit<T extends AnyZodOrCoValueSchema> = Simplify<
73
+ ReadonlyArray<CoFieldInit<T>>
74
+ >;
75
+
76
+ export type CoFeedInit<T extends AnyZodOrCoValueSchema> = Simplify<
77
+ ReadonlyArray<CoFieldInit<T>>
78
+ >;
@@ -27,15 +27,20 @@ export type InstanceOfSchema<S extends CoValueClass | AnyZodOrCoValueSchema> =
27
27
  S extends CoreCoValueSchema
28
28
  ? S extends CoreAccountSchema<infer Shape>
29
29
  ? {
30
- [key in keyof Shape]: InstanceOrPrimitiveOfSchema<Shape[key]>;
30
+ -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<
31
+ Shape[key]
32
+ >;
31
33
  } & Account
32
34
  : S extends CoreCoRecordSchema<infer K, infer V>
33
35
  ? {
34
- [key in z.output<K> & string]: InstanceOrPrimitiveOfSchema<V>;
36
+ -readonly [key in z.output<K> &
37
+ string]: InstanceOrPrimitiveOfSchema<V>;
35
38
  } & CoMap
36
39
  : S extends CoreCoMapSchema<infer Shape, infer CatchAll>
37
40
  ? {
38
- [key in keyof Shape]: InstanceOrPrimitiveOfSchema<Shape[key]>;
41
+ -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<
42
+ Shape[key]
43
+ >;
39
44
  } & (CatchAll extends AnyZodOrCoValueSchema
40
45
  ? {
41
46
  [key: string]: InstanceOrPrimitiveOfSchema<CatchAll>;
@@ -53,7 +58,7 @@ export type InstanceOfSchema<S extends CoValueClass | AnyZodOrCoValueSchema> =
53
58
  : S extends CoreFileStreamSchema
54
59
  ? FileStream
55
60
  : S extends CoreCoOptionalSchema<infer T>
56
- ? InstanceOrPrimitiveOfSchema<T>
61
+ ? InstanceOrPrimitiveOfSchema<T> | undefined
57
62
  : S extends CoDiscriminatedUnionSchema<infer Members>
58
63
  ? InstanceOrPrimitiveOfSchema<Members[number]>
59
64
  : never
@@ -29,7 +29,7 @@ export type InstanceOfSchemaCoValuesNullable<
29
29
  ? S extends CoreAccountSchema<infer Shape>
30
30
  ?
31
31
  | ({
32
- [key in keyof Shape]: InstanceOrPrimitiveOfSchemaCoValuesNullable<
32
+ -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchemaCoValuesNullable<
33
33
  Shape[key]
34
34
  >;
35
35
  } & Account)
@@ -37,14 +37,14 @@ export type InstanceOfSchemaCoValuesNullable<
37
37
  : S extends CoreCoRecordSchema<infer K, infer V>
38
38
  ?
39
39
  | ({
40
- [key in z.output<K> &
40
+ -readonly [key in z.output<K> &
41
41
  string]: InstanceOrPrimitiveOfSchemaCoValuesNullable<V>;
42
42
  } & CoMap)
43
43
  | null
44
44
  : S extends CoreCoMapSchema<infer Shape, infer CatchAll>
45
45
  ?
46
46
  | ({
47
- [key in keyof Shape]: InstanceOrPrimitiveOfSchemaCoValuesNullable<
47
+ -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchemaCoValuesNullable<
48
48
  Shape[key]
49
49
  >;
50
50
  } & (CatchAll extends AnyZodOrCoValueSchema
@@ -1,122 +1,11 @@
1
- import { JsonValue } from "cojson";
2
1
  import {
3
- Account,
4
2
  AnyZodOrCoValueSchema,
5
- CoDiscriminatedUnionSchema,
6
- CoFeed,
7
- CoList,
8
- CoMap,
9
- CoPlainText,
10
- CoRichText,
11
3
  CoValueClass,
12
- CoreAccountSchema,
13
- CoreCoRecordSchema,
14
- FileStream,
15
- Profile,
4
+ InstanceOfSchema,
16
5
  } from "../../../internal.js";
17
- import { CoreCoFeedSchema } from "../schemaTypes/CoFeedSchema.js";
18
- import { CoreCoListSchema } from "../schemaTypes/CoListSchema.js";
19
- import { CoreCoMapSchema } from "../schemaTypes/CoMapSchema.js";
20
- import { CoreCoOptionalSchema } from "../schemaTypes/CoOptionalSchema.js";
21
- import { CoreCoValueSchema } from "../schemaTypes/CoValueSchema.js";
22
- import { CoreFileStreamSchema } from "../schemaTypes/FileStreamSchema.js";
23
- import { CorePlainTextSchema } from "../schemaTypes/PlainTextSchema.js";
24
- import { CoreRichTextSchema } from "../schemaTypes/RichTextSchema.js";
25
6
  import { z } from "../zodReExport.js";
7
+ import { TypeOfZodSchema } from "./TypeOfZodSchema.js";
26
8
 
27
9
  export type InstanceOrPrimitiveOfSchema<
28
10
  S extends CoValueClass | AnyZodOrCoValueSchema,
29
- > = S extends CoreCoValueSchema
30
- ? S extends CoreAccountSchema<infer Shape>
31
- ? {
32
- -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<Shape[key]>;
33
- } & { profile: Profile } & Account
34
- : S extends CoreCoRecordSchema<infer K, infer V>
35
- ? {
36
- -readonly [key in z.output<K> &
37
- string]: InstanceOrPrimitiveOfSchema<V>;
38
- } & CoMap
39
- : S extends CoreCoMapSchema<infer Shape, infer CatchAll>
40
- ? {
41
- -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<
42
- Shape[key]
43
- >;
44
- } & (CatchAll extends AnyZodOrCoValueSchema
45
- ? {
46
- [key: string]: InstanceOrPrimitiveOfSchema<CatchAll>;
47
- }
48
- : {}) &
49
- CoMap
50
- : S extends CoreCoListSchema<infer T>
51
- ? CoList<InstanceOrPrimitiveOfSchema<T>>
52
- : S extends CoreCoFeedSchema<infer T>
53
- ? CoFeed<InstanceOrPrimitiveOfSchema<T>>
54
- : S extends CorePlainTextSchema
55
- ? CoPlainText
56
- : S extends CoreRichTextSchema
57
- ? CoRichText
58
- : S extends CoreFileStreamSchema
59
- ? FileStream
60
- : S extends CoreCoOptionalSchema<infer T>
61
- ? InstanceOrPrimitiveOfSchema<T> | undefined
62
- : S extends CoDiscriminatedUnionSchema<infer Members>
63
- ? InstanceOrPrimitiveOfSchema<Members[number]>
64
- : never
65
- : S extends z.core.$ZodType
66
- ? S extends z.core.$ZodOptional<infer Inner extends z.core.$ZodType>
67
- ? InstanceOrPrimitiveOfSchema<Inner> | undefined
68
- : S extends z.core.$ZodNullable<infer Inner extends z.core.$ZodType>
69
- ? InstanceOrPrimitiveOfSchema<Inner> | null
70
- : S extends z.ZodJSONSchema
71
- ? JsonValue
72
- : S extends z.core.$ZodUnion<infer Members extends z.core.$ZodType[]>
73
- ? InstanceOrPrimitiveOfSchema<Members[number]>
74
- : // primitives below here - we manually traverse to ensure we only allow what we can handle
75
- S extends z.core.$ZodObject<infer Shape>
76
- ? {
77
- -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<
78
- Shape[key]
79
- >;
80
- }
81
- : S extends z.core.$ZodArray<infer Item extends z.core.$ZodType>
82
- ? InstanceOrPrimitiveOfSchema<Item>[]
83
- : S extends z.core.$ZodTuple<
84
- infer Items extends readonly z.core.$ZodType[]
85
- >
86
- ? {
87
- [key in keyof Items]: InstanceOrPrimitiveOfSchema<
88
- Items[key]
89
- >;
90
- }
91
- : S extends z.core.$ZodString
92
- ? string
93
- : S extends z.core.$ZodNumber
94
- ? number
95
- : S extends z.core.$ZodBoolean
96
- ? boolean
97
- : S extends z.core.$ZodLiteral<infer Literal>
98
- ? Literal
99
- : S extends z.core.$ZodDate
100
- ? Date
101
- : S extends z.core.$ZodEnum<infer Enum>
102
- ? Enum[keyof Enum]
103
- : S extends z.core.$ZodTemplateLiteral<
104
- infer pattern
105
- >
106
- ? pattern
107
- : S extends z.core.$ZodReadonly<
108
- infer Inner extends z.core.$ZodType
109
- >
110
- ? InstanceOrPrimitiveOfSchema<Inner>
111
- : S extends z.core.$ZodDefault<
112
- infer Default extends z.core.$ZodType
113
- >
114
- ? InstanceOrPrimitiveOfSchema<Default>
115
- : S extends z.core.$ZodCatch<
116
- infer Catch extends z.core.$ZodType
117
- >
118
- ? InstanceOrPrimitiveOfSchema<Catch>
119
- : never
120
- : S extends CoValueClass
121
- ? InstanceType<S>
122
- : never;
11
+ > = S extends z.core.$ZodType ? TypeOfZodSchema<S> : InstanceOfSchema<S>;