jazz-tools 0.15.12 → 0.15.13

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/dist/index.js CHANGED
@@ -34,7 +34,7 @@ import {
34
34
  subscribeToCoValue,
35
35
  zodReExport_exports,
36
36
  zodSchemaToCoSchema
37
- } from "./chunk-DZFT4EZM.js";
37
+ } from "./chunk-6CCJYSYQ.js";
38
38
 
39
39
  // src/tools/auth/clerk/index.ts
40
40
  import {
@@ -1,3 +1,5 @@
1
+ "use client";
2
+
1
3
  // src/react/provider.tsx
2
4
  import {
3
5
  JazzBrowserContextManager
@@ -696,4 +698,4 @@ export {
696
698
  usePassphraseAuth2 as usePassphraseAuth,
697
699
  useProgressiveImg
698
700
  };
699
- //# sourceMappingURL=index.js.mapRL=index.js.map
701
+ //# sourceMappingURL=index.js.map
@@ -1,3 +1,5 @@
1
+ "use client";
2
+
1
3
  // src/react/testing.tsx
2
4
  export * from "jazz-tools/react-core/testing";
3
- //# sourceMappingURL=testing.js.map=testing.js.map
5
+ //# sourceMappingURL=testing.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/react/testing.tsx"],"sourcesContent":["export * from \"jazz-tools/react-core/testing\";\n"],"mappings":";AAAA,cAAc;","names":[]}]}
1
+ {"version":3,"sources":["../../src/react/testing.tsx"],"sourcesContent":["export * from \"jazz-tools/react-core/testing\";\n"],"mappings":";;;AAAA,cAAc;","names":[]}
package/dist/testing.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  createAnonymousJazzContext,
7
7
  createJazzContext,
8
8
  randomSessionProvider
9
- } from "./chunk-DZFT4EZM.js";
9
+ } from "./chunk-6CCJYSYQ.js";
10
10
 
11
11
  // src/tools/testing.ts
12
12
  import { LocalNode } from "cojson";
@@ -7,6 +7,6 @@ import { AnyCoSchema, ZodPrimitiveSchema } from "../zodSchema.js";
7
7
  type SchemaField = AnyCoSchema | CoValueClass | ZodPrimitiveSchema | z.core.$ZodOptional<z.core.$ZodType> | z.core.$ZodUnion<z.core.$ZodType[]> | z.core.$ZodObject<z.core.$ZodLooseShape> | z.core.$ZodArray<z.core.$ZodType> | z.core.$ZodTuple<z.core.$ZodType[]> | z.core.$ZodReadonly<z.core.$ZodType> | z.core.$ZodLazy<z.core.$ZodType> | z.core.$ZodTemplateLiteral<any> | z.core.$ZodLiteral<any> | z.core.$ZodCatch<z.core.$ZodType> | z.core.$ZodEnum<any> | z.core.$ZodDefault<z.core.$ZodType> | z.core.$ZodCatch<z.core.$ZodType> | (z.core.$ZodCustom<any, any> & {
8
8
  builtin: any;
9
9
  });
10
- export declare function schemaFieldToCoFieldDef(schema: SchemaField): string | number | boolean | Date | import("../../../internal.js").CoValue | CoMap | import("../../../internal.js").CoPlainText | import("../../../internal.js").BinaryCoStream | import("../../../internal.js").CoStream<unknown> | import("../../../internal.js").CoList<unknown> | null | undefined;
10
+ export declare function schemaFieldToCoFieldDef(schema: SchemaField, isOptional?: boolean): string | number | boolean | Date | import("../../../internal.js").CoValue | CoMap | import("../../../internal.js").CoPlainText | import("../../../internal.js").BinaryCoStream | import("../../../internal.js").CoStream<unknown> | import("../../../internal.js").CoList<unknown> | null | undefined;
11
11
  export {};
12
12
  //# sourceMappingURL=zodFieldToCoFieldDef.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"zodFieldToCoFieldDef.d.ts","sourceRoot":"","sources":["../../../../../src/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,YAAY,EAAkB,MAAM,sBAAsB,CAAC;AAQ3E,OAAO,EAKL,CAAC,EACF,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAMlE;;GAEG;AACH,KAAK,WAAW,GAEZ,WAAW,GAEX,YAAY,GACZ,kBAAkB,GAClB,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,CAA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ACjC,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG;IAAE,OAAO,EAAE,GAAG,CAAA;CAAE,CAAC,CAAC;AAErD,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,WAAW,EACnB,UAAU,UAAQ,ySA4HnB"}
package/package.json CHANGED
@@ -139,7 +139,7 @@
139
139
  },
140
140
  "type": "module",
141
141
  "license": "MIT",
142
- "version": "0.15.12",
142
+ "version": "0.15.13",
143
143
  "dependencies": {
144
144
  "@manuscripts/prosemirror-recreate-steps": "^0.1.4",
145
145
  "@scure/base": "1.2.1",
@@ -158,9 +158,9 @@
158
158
  "prosemirror-state": "^1.4.3",
159
159
  "prosemirror-transform": "^1.9.0",
160
160
  "zod": "3.25.28",
161
- "cojson": "0.15.12",
162
- "cojson-storage-indexeddb": "0.15.12",
163
- "cojson-transport-ws": "0.15.12"
161
+ "cojson": "0.15.13",
162
+ "cojson-storage-indexeddb": "0.15.13",
163
+ "cojson-transport-ws": "0.15.13"
164
164
  },
165
165
  "devDependencies": {
166
166
  "@scure/bip39": "^1.3.0",
@@ -43,27 +43,35 @@ type SchemaField =
43
43
  | z.core.$ZodCatch<z.core.$ZodType>
44
44
  | (z.core.$ZodCustom<any, any> & { builtin: any });
45
45
 
46
- export function schemaFieldToCoFieldDef(schema: SchemaField) {
46
+ export function schemaFieldToCoFieldDef(
47
+ schema: SchemaField,
48
+ isOptional = false,
49
+ ) {
47
50
  if (isCoValueClass(schema)) {
48
- return coField.ref(schema);
51
+ if (isOptional) {
52
+ return coField.ref(schema, { optional: true });
53
+ } else {
54
+ return coField.ref(schema);
55
+ }
49
56
  } else if (isCoValueSchema(schema)) {
50
57
  if (isAnyCoOptionalSchema(schema)) {
51
58
  return coField.ref(schema.getCoValueClass(), {
52
59
  optional: true,
53
60
  });
54
61
  }
55
- return coField.ref(schema.getCoValueClass());
62
+
63
+ if (isOptional) {
64
+ return coField.ref(schema.getCoValueClass(), { optional: true });
65
+ } else {
66
+ return coField.ref(schema.getCoValueClass());
67
+ }
56
68
  } else {
57
69
  if ("_zod" in schema) {
58
70
  if (schema._zod.def.type === "optional") {
59
71
  const inner = zodSchemaToCoSchemaOrKeepPrimitive(
60
72
  schema._zod.def.innerType,
61
73
  );
62
- if (isCoValueClass(inner)) {
63
- return coField.ref(inner, { optional: true });
64
- } else {
65
- return schemaFieldToCoFieldDef(inner);
66
- }
74
+ return schemaFieldToCoFieldDef(inner, true);
67
75
  } else if (schema._zod.def.type === "string") {
68
76
  return coField.string;
69
77
  } else if (schema._zod.def.type === "number") {
@@ -77,6 +85,7 @@ export function schemaFieldToCoFieldDef(schema: SchemaField) {
77
85
  } else if (schema._zod.def.type === "readonly") {
78
86
  return schemaFieldToCoFieldDef(
79
87
  (schema as unknown as ZodReadonly).def.innerType as SchemaField,
88
+ isOptional,
80
89
  );
81
90
  } else if (schema._zod.def.type === "date") {
82
91
  return coField.optional.Date;
@@ -86,6 +95,7 @@ export function schemaFieldToCoFieldDef(schema: SchemaField) {
86
95
  // Mostly to support z.json()
87
96
  return schemaFieldToCoFieldDef(
88
97
  (schema as unknown as ZodLazy).unwrap() as SchemaField,
98
+ isOptional,
89
99
  );
90
100
  } else if (
91
101
  schema._zod.def.type === "default" ||
@@ -98,6 +108,7 @@ export function schemaFieldToCoFieldDef(schema: SchemaField) {
98
108
  return schemaFieldToCoFieldDef(
99
109
  (schema as unknown as ZodDefault | ZodCatch).def
100
110
  .innerType as SchemaField,
111
+ isOptional,
101
112
  );
102
113
  } else if (schema._zod.def.type === "literal") {
103
114
  if (
@@ -129,7 +140,7 @@ export function schemaFieldToCoFieldDef(schema: SchemaField) {
129
140
  return coField.json();
130
141
  } else if (schema._zod.def.type === "custom") {
131
142
  if ("builtin" in schema) {
132
- return schemaFieldToCoFieldDef(schema.builtin);
143
+ return schemaFieldToCoFieldDef(schema.builtin, isOptional);
133
144
  } else {
134
145
  throw new Error(`Unsupported custom zod type`);
135
146
  }
@@ -137,9 +148,13 @@ export function schemaFieldToCoFieldDef(schema: SchemaField) {
137
148
  if (isUnionOfPrimitivesDeeply(schema)) {
138
149
  return coField.json();
139
150
  } else if (isUnionOfCoMapsDeeply(schema)) {
140
- return coField.ref<CoValueClass<CoMap>>(
141
- schemaUnionDiscriminatorFor(schema),
142
- );
151
+ const result = schemaUnionDiscriminatorFor(schema);
152
+
153
+ if (isOptional) {
154
+ return coField.ref<CoValueClass<CoMap>>(result, { optional: true });
155
+ } else {
156
+ return coField.ref<CoValueClass<CoMap>>(result);
157
+ }
143
158
  } else {
144
159
  throw new Error(
145
160
  "z.union()/z.discriminatedUnion() of mixed collaborative and non-collaborative types is not supported",
@@ -42,6 +42,120 @@ test("return null if id is invalid", async () => {
42
42
  expect(john).toBeNull();
43
43
  });
44
44
 
45
+ test("load a missing optional value (co.optional)", async () => {
46
+ const Dog = co.map({
47
+ name: z.string(),
48
+ });
49
+
50
+ const Person = co.map({
51
+ name: z.string(),
52
+ dog: co.optional(Dog),
53
+ });
54
+
55
+ const group = Group.create();
56
+ const map = Person.create({ name: "John" }, group);
57
+ group.addMember("everyone", "reader");
58
+
59
+ const alice = await createJazzTestAccount();
60
+
61
+ const john = await Person.load(map.id, {
62
+ loadAs: alice,
63
+ resolve: { dog: true },
64
+ });
65
+
66
+ assert(john);
67
+
68
+ expect(john.name).toBe("John");
69
+ expect(john.dog).toBeUndefined();
70
+ });
71
+
72
+ test("load a missing optional value (z.optional)", async () => {
73
+ const Dog = co.map({
74
+ name: z.string(),
75
+ });
76
+
77
+ const Person = co.map({
78
+ name: z.string(),
79
+ dog: z.optional(Dog),
80
+ });
81
+
82
+ const group = Group.create();
83
+ const map = Person.create({ name: "John" }, group);
84
+ group.addMember("everyone", "reader");
85
+
86
+ const alice = await createJazzTestAccount();
87
+
88
+ const john = await Person.load(map.id, {
89
+ loadAs: alice,
90
+ resolve: { dog: true },
91
+ });
92
+
93
+ assert(john);
94
+
95
+ expect(john.name).toBe("John");
96
+ expect(john.dog).toBeUndefined();
97
+ });
98
+
99
+ test("load a missing optional value (Schema.optional)", async () => {
100
+ const Dog = co.map({
101
+ name: z.string(),
102
+ });
103
+
104
+ const Person = co.map({
105
+ name: z.string(),
106
+ dog: Dog.optional(),
107
+ });
108
+
109
+ const group = Group.create();
110
+ const map = Person.create({ name: "John" }, group);
111
+ group.addMember("everyone", "reader");
112
+
113
+ const alice = await createJazzTestAccount();
114
+
115
+ const john = await Person.load(map.id, {
116
+ loadAs: alice,
117
+ resolve: { dog: true },
118
+ });
119
+
120
+ assert(john);
121
+
122
+ expect(john.name).toBe("John");
123
+ expect(john.dog).toBeUndefined();
124
+ });
125
+
126
+ test("load a missing optional value (optional discrminatedUnion)", async () => {
127
+ const Dog = co.map({
128
+ type: z.literal("dog"),
129
+ name: z.string(),
130
+ });
131
+
132
+ const Cat = co.map({
133
+ type: z.literal("cat"),
134
+ name: z.string(),
135
+ });
136
+
137
+ const Person = co.map({
138
+ name: z.string(),
139
+ pet: co.discriminatedUnion("type", [Dog, Cat]).optional(),
140
+ });
141
+
142
+ const group = Group.create();
143
+ const map = Person.create({ name: "John" }, group);
144
+ group.addMember("everyone", "reader");
145
+
146
+ const alice = await createJazzTestAccount();
147
+
148
+ const john = await Person.load(map.id, {
149
+ loadAs: alice,
150
+ resolve: { pet: true },
151
+ });
152
+
153
+ assert(john);
154
+
155
+ expect(john.name).toBe("John");
156
+ expect(john.pet).toBeUndefined();
157
+ });
158
+
45
159
  test("retry an unavailable value", async () => {
46
160
  const Person = co.map({
47
161
  name: z.string(),