jazz-tools 0.14.4 → 0.14.6

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 (47) hide show
  1. package/.turbo/turbo-build.log +7 -7
  2. package/CHANGELOG.md +14 -0
  3. package/dist/{chunk-6QMAFVZO.js → chunk-E2B2W453.js} +36 -17
  4. package/dist/chunk-E2B2W453.js.map +1 -0
  5. package/dist/coValues/extensions/imageDef.d.ts +2 -1
  6. package/dist/coValues/extensions/imageDef.d.ts.map +1 -1
  7. package/dist/implementation/zodSchema/coExport.d.ts +3 -0
  8. package/dist/implementation/zodSchema/coExport.d.ts.map +1 -0
  9. package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +3 -1
  10. package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  11. package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +3 -1
  12. package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  13. package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +11 -3
  14. package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  15. package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +5 -3
  16. package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  17. package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +19 -3
  18. package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  19. package/dist/implementation/zodSchema/zodCo.d.ts +21 -27
  20. package/dist/implementation/zodSchema/zodCo.d.ts.map +1 -1
  21. package/dist/implementation/zodSchema/zodSchema.d.ts +1 -4
  22. package/dist/implementation/zodSchema/zodSchema.d.ts.map +1 -1
  23. package/dist/index.js +3 -3
  24. package/dist/internal.d.ts +1 -0
  25. package/dist/internal.d.ts.map +1 -1
  26. package/dist/testing.js +1 -1
  27. package/dist/tests/coFeed.test-d.d.ts +2 -0
  28. package/dist/tests/coFeed.test-d.d.ts.map +1 -0
  29. package/dist/tests/coList.test-d.d.ts +2 -0
  30. package/dist/tests/coList.test-d.d.ts.map +1 -0
  31. package/package.json +1 -1
  32. package/src/coValues/extensions/imageDef.ts +7 -1
  33. package/src/implementation/zodSchema/coExport.ts +13 -0
  34. package/src/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +7 -1
  35. package/src/implementation/zodSchema/schemaTypes/CoListSchema.ts +7 -1
  36. package/src/implementation/zodSchema/schemaTypes/CoMapSchema.ts +20 -8
  37. package/src/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +10 -3
  38. package/src/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +37 -3
  39. package/src/implementation/zodSchema/zodCo.ts +32 -24
  40. package/src/implementation/zodSchema/zodSchema.ts +0 -5
  41. package/src/internal.ts +1 -0
  42. package/src/tests/coFeed.test-d.ts +256 -0
  43. package/src/tests/coFeed.test.ts +41 -18
  44. package/src/tests/coList.test-d.ts +245 -0
  45. package/src/tests/coMap.record.test-d.ts +33 -0
  46. package/src/tests/coMap.test-d.ts +63 -0
  47. package/dist/chunk-6QMAFVZO.js.map +0 -1
@@ -89,7 +89,7 @@ export const coMapDefiner = <Shape extends z.core.$ZodLooseShape>(
89
89
  return coMapSchema as unknown as CoMapSchema<Shape>;
90
90
  };
91
91
 
92
- const coAccountDefiner = <
92
+ export const coAccountDefiner = <
93
93
  Shape extends {
94
94
  profile: AnyCoMapSchema<{
95
95
  name: z.core.$ZodString<string>;
@@ -100,12 +100,12 @@ const coAccountDefiner = <
100
100
  },
101
101
  >(
102
102
  shape: Shape = {
103
- profile: co.map({
103
+ profile: coMapDefiner({
104
104
  name: z.string(),
105
105
  inbox: z.optional(z.string()),
106
106
  inboxInvite: z.optional(z.string()),
107
107
  }),
108
- root: co.map({}),
108
+ root: coMapDefiner({}),
109
109
  } as unknown as Shape,
110
110
  ): AccountSchema<Shape> => {
111
111
  const objectSchema = z.object(shape).meta({
@@ -183,7 +183,7 @@ const coAccountDefiner = <
183
183
  return accountSchema as unknown as AccountSchema<Shape>;
184
184
  };
185
185
 
186
- const coRecordDefiner = <
186
+ export const coRecordDefiner = <
187
187
  K extends z.core.$ZodString<string>,
188
188
  V extends z.core.$ZodType,
189
189
  >(
@@ -196,7 +196,7 @@ const coRecordDefiner = <
196
196
  >;
197
197
  };
198
198
 
199
- const coListDefiner = <T extends z.core.$ZodType>(
199
+ export const coListDefiner = <T extends z.core.$ZodType>(
200
200
  element: T,
201
201
  ): CoListSchema<T> => {
202
202
  const arraySchema = z.array(element).meta({
@@ -237,7 +237,7 @@ const coListDefiner = <T extends z.core.$ZodType>(
237
237
  return coListSchema;
238
238
  };
239
239
 
240
- const coProfileDefiner = <
240
+ export const coProfileDefiner = <
241
241
  Shape extends z.core.$ZodLooseShape = Simplify<DefaultProfileShape>,
242
242
  >(
243
243
  shape: Shape & {
@@ -261,7 +261,7 @@ const coProfileDefiner = <
261
261
  } as CoProfileSchema<Shape>;
262
262
  };
263
263
 
264
- const coFeedDefiner = <T extends z.core.$ZodType>(
264
+ export const coFeedDefiner = <T extends z.core.$ZodType>(
265
265
  element: T,
266
266
  ): CoFeedSchema<T> => {
267
267
  const placeholderSchema = z.instanceof(CoFeed);
@@ -306,8 +306,11 @@ export const coFileStreamDefiner = (): FileStreamSchema => {
306
306
  > & {
307
307
  collaborative: true;
308
308
  builtin: "FileStream";
309
- create: (typeof FileStream)["create"];
310
- createFromBlob: (typeof FileStream)["createFromBlob"];
309
+ create: FileStreamSchema["create"];
310
+ createFromBlob: FileStreamSchema["createFromBlob"];
311
+ load: FileStreamSchema["load"];
312
+ loadAsBlob: FileStreamSchema["loadAsBlob"];
313
+ subscribe: FileStreamSchema["subscribe"];
311
314
  };
312
315
 
313
316
  fileStreamSchema.collaborative = true;
@@ -315,11 +318,27 @@ export const coFileStreamDefiner = (): FileStreamSchema => {
315
318
 
316
319
  fileStreamSchema.create = function (options: any) {
317
320
  return FileStream.create(options);
318
- } as (typeof FileStream)["create"];
321
+ } as FileStreamSchema["create"];
319
322
 
320
323
  fileStreamSchema.createFromBlob = function (blob: Blob, options: any) {
321
324
  return FileStream.createFromBlob(blob, options);
322
- } as (typeof FileStream)["createFromBlob"];
325
+ } as FileStreamSchema["createFromBlob"];
326
+
327
+ fileStreamSchema.load = function (id: string, options: any) {
328
+ return FileStream.load(id, options);
329
+ } as FileStreamSchema["load"];
330
+
331
+ fileStreamSchema.loadAsBlob = function (id: string, options: any) {
332
+ return FileStream.loadAsBlob(id, options);
333
+ } as FileStreamSchema["loadAsBlob"];
334
+
335
+ fileStreamSchema.subscribe = function (
336
+ id: string,
337
+ options: any,
338
+ listener: any,
339
+ ) {
340
+ return FileStream.subscribe(id, options, listener);
341
+ } as FileStreamSchema["subscribe"];
323
342
 
324
343
  return fileStreamSchema;
325
344
  };
@@ -393,17 +412,6 @@ export const coRichTextDefiner = (): RichTextSchema => {
393
412
  return richTextSchema;
394
413
  };
395
414
 
396
- export const co = {
397
- map: coMapDefiner,
398
- record: coRecordDefiner,
399
- list: coListDefiner,
400
- feed: coFeedDefiner,
401
- plainText: coPlainTextDefiner,
402
- richText: coRichTextDefiner,
403
- fileStream: coFileStreamDefiner,
404
- image: (): typeof ImageDefinition => {
405
- return ImageDefinition;
406
- },
407
- account: coAccountDefiner,
408
- profile: coProfileDefiner,
415
+ export const coImageDefiner = (): typeof ImageDefinition => {
416
+ return ImageDefinition;
409
417
  };
@@ -31,11 +31,6 @@ export type WithHelpers<
31
31
  Helpers extends object,
32
32
  > = Base & Helpers;
33
33
 
34
- export type FullyOrPartiallyLoaded<S extends z.core.$ZodType | CoValueClass> =
35
- InstanceOrPrimitiveOfSchema<S> extends CoValue
36
- ? NonNullable<InstanceOrPrimitiveOfSchemaCoValuesNullable<S>>
37
- : InstanceOrPrimitiveOfSchema<S>;
38
-
39
34
  export type ZodPrimitiveSchema =
40
35
  | z.core.$ZodString
41
36
  | z.core.$ZodNumber
package/src/internal.ts CHANGED
@@ -31,6 +31,7 @@ export * from "./implementation/createContext.js";
31
31
  export * from "./types.js";
32
32
  export * from "./implementation/zodSchema/zodSchema.js";
33
33
  export * from "./implementation/zodSchema/zodCo.js";
34
+ export * as co from "./implementation/zodSchema/coExport.js";
34
35
  export * from "./implementation/zodSchema/schemaTypes/CoMapSchema.js";
35
36
  export * from "./implementation/zodSchema/schemaTypes/CoRecordSchema.js";
36
37
  export * from "./implementation/zodSchema/schemaTypes/CoListSchema.js";
@@ -0,0 +1,256 @@
1
+ import { assert, describe, expectTypeOf, test } from "vitest";
2
+ import { Group, co, z } from "../exports.js";
3
+ import { Account } from "../index.js";
4
+ import { CoFeed, FileStream, Loaded } from "../internal.js";
5
+
6
+ describe("CoFeed", () => {
7
+ describe("init", () => {
8
+ test("create a CoFeed with basic property access", () => {
9
+ const StringFeed = co.feed(z.string());
10
+
11
+ const feed = StringFeed.create(["milk"]);
12
+
13
+ type ExpectedType = string;
14
+
15
+ function matches(value: ExpectedType | undefined) {
16
+ return value;
17
+ }
18
+
19
+ matches(feed.perAccount[Account.getMe().id]!.value);
20
+ });
21
+
22
+ test("has the _owner property", () => {
23
+ const StringFeed = co.feed(z.string());
24
+
25
+ const feed = StringFeed.create(["milk"], Account.getMe());
26
+
27
+ expectTypeOf(feed._owner).toEqualTypeOf<Account | Group>();
28
+ });
29
+
30
+ test("CoFeed with reference", () => {
31
+ const Dog = co.map({
32
+ name: z.string(),
33
+ breed: z.string(),
34
+ });
35
+
36
+ const DogFeed = co.feed(Dog);
37
+
38
+ const feed = DogFeed.create([
39
+ Dog.create({ name: "Rex", breed: "Labrador" }),
40
+ ]);
41
+
42
+ type ExpectedType = Loaded<typeof Dog> | undefined | null;
43
+
44
+ function matches(value: ExpectedType) {
45
+ return value;
46
+ }
47
+
48
+ matches(feed.perAccount[Account.getMe().id]!.value);
49
+ });
50
+
51
+ test("CoFeed with optional reference", () => {
52
+ const Dog = co.map({
53
+ name: z.string(),
54
+ breed: z.string(),
55
+ });
56
+
57
+ const DogFeed = co.feed(Dog.optional());
58
+
59
+ const feed = DogFeed.create([
60
+ Dog.create({ name: "Rex", breed: "Labrador" }),
61
+ ]);
62
+
63
+ type ExpectedType = Loaded<typeof Dog> | undefined | null;
64
+
65
+ function matches(value: ExpectedType) {
66
+ return value;
67
+ }
68
+
69
+ matches(feed.perAccount[Account.getMe().id]?.value);
70
+ });
71
+
72
+ test("CoFeed create with partially loaded, reference and optional", () => {
73
+ const Dog = co.map({
74
+ name: z.string(),
75
+ breed: co.map({ type: z.literal("labrador"), value: z.string() }),
76
+ });
77
+ type Dog = co.loaded<typeof Dog>;
78
+
79
+ const DogFeed = co.feed(Dog.optional());
80
+
81
+ const dog = Dog.create({
82
+ name: "Rex",
83
+ breed: Dog.def.shape.breed.create({
84
+ type: "labrador",
85
+ value: "Labrador",
86
+ }),
87
+ }) as Dog;
88
+
89
+ const feed = DogFeed.create([dog, undefined]);
90
+
91
+ type ExpectedType = Loaded<typeof Dog> | undefined | null;
92
+
93
+ function matches(value: ExpectedType) {
94
+ return value;
95
+ }
96
+
97
+ matches(feed.perAccount[Account.getMe().id]!.value);
98
+ });
99
+
100
+ test("CoFeed with nested feeds", () => {
101
+ const NestedFeed = co.feed(co.feed(z.string()));
102
+
103
+ const feed = NestedFeed.create([co.feed(z.string()).create(["milk"])]);
104
+
105
+ type ExpectedType = CoFeed<string> | undefined | null;
106
+
107
+ function matches(value: ExpectedType) {
108
+ return value;
109
+ }
110
+
111
+ matches(feed.perAccount[Account.getMe().id]?.value);
112
+ });
113
+
114
+ test("CoFeed with enum type", () => {
115
+ const EnumFeed = co.feed(z.enum(["a", "b", "c"]));
116
+
117
+ const feed = EnumFeed.create(["a"]);
118
+
119
+ type ExpectedType = "a" | "b" | "c" | undefined | null;
120
+
121
+ function matches(value: ExpectedType) {
122
+ return value;
123
+ }
124
+
125
+ matches(feed.perAccount[Account.getMe().id]?.value);
126
+ });
127
+ });
128
+
129
+ describe("CoFeed resolution", () => {
130
+ test("loading a feed with deep resolve", async () => {
131
+ const Dog = co.map({
132
+ name: z.string(),
133
+ breed: z.string(),
134
+ });
135
+
136
+ const DogFeed = co.feed(Dog);
137
+
138
+ const feed = DogFeed.create([
139
+ Dog.create({ name: "Rex", breed: "Labrador" }),
140
+ ]);
141
+
142
+ const loadedFeed = await DogFeed.load(feed.id, {
143
+ resolve: true,
144
+ });
145
+
146
+ type ExpectedType = Loaded<typeof Dog> | null | undefined;
147
+
148
+ function matches(value: ExpectedType) {
149
+ return value;
150
+ }
151
+
152
+ matches(loadedFeed?.perAccount[Account.getMe().id]?.value);
153
+
154
+ assert(loadedFeed);
155
+ const dog = loadedFeed.perAccount[Account.getMe().id]?.value;
156
+ assert(dog);
157
+ expectTypeOf(dog.name).toEqualTypeOf<string>();
158
+ });
159
+
160
+ test("loading a feed with $onError", async () => {
161
+ const Dog = co.map({
162
+ name: z.string(),
163
+ breed: z.string(),
164
+ });
165
+
166
+ const DogFeed = co.feed(Dog);
167
+
168
+ const feed = DogFeed.create([
169
+ Dog.create({ name: "Rex", breed: "Labrador" }),
170
+ ]);
171
+
172
+ const loadedFeed = await DogFeed.load(feed.id, {
173
+ resolve: { $each: { $onError: null } },
174
+ });
175
+
176
+ type ExpectedType = Loaded<typeof Dog> | null | undefined;
177
+
178
+ function matches(value: ExpectedType) {
179
+ return value;
180
+ }
181
+
182
+ matches(loadedFeed?.perAccount[Account.getMe().id]?.value);
183
+ });
184
+
185
+ test("loading a nested feed with deep resolve", async () => {
186
+ const Dog = co.map({
187
+ name: z.string(),
188
+ breed: z.string(),
189
+ });
190
+
191
+ const DogFeed = co.feed(Dog);
192
+ const NestedFeed = co.feed(DogFeed);
193
+
194
+ const feed = NestedFeed.create([
195
+ DogFeed.create([Dog.create({ name: "Rex", breed: "Labrador" })]),
196
+ ]);
197
+
198
+ const loadedFeed = await NestedFeed.load(feed.id, {
199
+ resolve: {
200
+ $each: true,
201
+ },
202
+ });
203
+
204
+ type ExpectedType = Loaded<typeof Dog> | null | undefined;
205
+
206
+ function matches(value: ExpectedType) {
207
+ return value;
208
+ }
209
+
210
+ const nestedFeed = loadedFeed?.perAccount[Account.getMe().id]?.value;
211
+ assert(nestedFeed);
212
+ matches(nestedFeed.perAccount[Account.getMe().id]?.value);
213
+
214
+ assert(loadedFeed);
215
+ const dog = nestedFeed.perAccount[Account.getMe().id]?.value;
216
+ assert(dog);
217
+ expectTypeOf(dog.name).toEqualTypeOf<string>();
218
+ });
219
+ });
220
+ });
221
+
222
+ describe("co.fileStream", () => {
223
+ test("create function type", () => {
224
+ const FileStreamFeed = co.fileStream();
225
+
226
+ const feed = FileStreamFeed.create({ owner: Account.getMe() });
227
+
228
+ type ExpectedType = FileStream;
229
+
230
+ function matches(value: ExpectedType) {
231
+ return value;
232
+ }
233
+
234
+ matches(feed);
235
+ });
236
+
237
+ test("createFromBlob function type", async () => {
238
+ const FileStreamFeed = co.fileStream();
239
+ const blob = new Blob(["test"], { type: "text/plain" });
240
+
241
+ const feed = await FileStreamFeed.createFromBlob(blob, {
242
+ owner: Account.getMe(),
243
+ onProgress: (progress: number) => {
244
+ console.log(`Progress: ${progress}`);
245
+ },
246
+ });
247
+
248
+ type ExpectedType = FileStream;
249
+
250
+ function matches(value: ExpectedType) {
251
+ return value;
252
+ }
253
+
254
+ matches(feed);
255
+ });
256
+ });
@@ -2,18 +2,14 @@ import { WasmCrypto } from "cojson/crypto/WasmCrypto";
2
2
  import { describe, expect, expectTypeOf, test } from "vitest";
3
3
  import {
4
4
  Account,
5
- CoFeed,
6
5
  FileStream,
7
6
  Group,
8
- ID,
9
7
  co,
10
- coField,
11
8
  cojsonInternals,
12
9
  isControlledAccount,
13
10
  z,
14
11
  } from "../index.js";
15
12
  import {
16
- AnyCoFeedSchema,
17
13
  Loaded,
18
14
  createJazzContextFromExistingCredentials,
19
15
  randomSessionProvider,
@@ -350,23 +346,50 @@ describe("Simple FileStream operations", async () => {
350
346
 
351
347
  const stream = FileStream.create({ owner: me });
352
348
 
353
- test("Construction", () => {
354
- expect(stream.getChunks()).toBe(undefined);
349
+ describe("FileStream", () => {
350
+ test("Construction", () => {
351
+ expect(stream.getChunks()).toBe(undefined);
352
+ });
353
+
354
+ test("Mutation", () => {
355
+ stream.start({ mimeType: "text/plain" });
356
+ stream.push(new Uint8Array([1, 2, 3]));
357
+ stream.push(new Uint8Array([4, 5, 6]));
358
+ stream.end();
359
+
360
+ const chunks = stream.getChunks();
361
+ expect(chunks?.mimeType).toBe("text/plain");
362
+ expect(chunks?.chunks).toEqual([
363
+ new Uint8Array([1, 2, 3]),
364
+ new Uint8Array([4, 5, 6]),
365
+ ]);
366
+ expect(chunks?.finished).toBe(true);
367
+ });
355
368
  });
356
369
 
357
- test("Mutation", () => {
358
- stream.start({ mimeType: "text/plain" });
359
- stream.push(new Uint8Array([1, 2, 3]));
360
- stream.push(new Uint8Array([4, 5, 6]));
361
- stream.end();
370
+ describe("co.fileStream", () => {
371
+ const fs = co.fileStream().create({ owner: me });
362
372
 
363
- const chunks = stream.getChunks();
364
- expect(chunks?.mimeType).toBe("text/plain");
365
- expect(chunks?.chunks).toEqual([
366
- new Uint8Array([1, 2, 3]),
367
- new Uint8Array([4, 5, 6]),
368
- ]);
369
- expect(chunks?.finished).toBe(true);
373
+ test("Construction", () => {
374
+ expect(fs.getChunks()).toBe(undefined);
375
+ });
376
+
377
+ test("Type compatibility", () => {
378
+ // Check base functionality works
379
+ expectTypeOf(co.fileStream()).toHaveProperty("create");
380
+
381
+ // We can acknowledge the type error exists
382
+ // This is a runtime test that verifies that despite the TypeScript error,
383
+ // the functionality still works as expected
384
+ expect(typeof fs.getChunks).toBe("function");
385
+ });
386
+
387
+ test("Mutation", () => {
388
+ fs.start({ mimeType: "text/plain" });
389
+ fs.push(new Uint8Array([1, 2, 3]));
390
+ fs.push(new Uint8Array([4, 5, 6]));
391
+ fs.end();
392
+ });
370
393
  });
371
394
  });
372
395