jazz-tools 0.14.5 → 0.14.7
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/.turbo/turbo-build.log +7 -7
- package/CHANGELOG.md +13 -0
- package/dist/{chunk-52PJ4QZ3.js → chunk-RTUIFVYF.js} +21 -1
- package/dist/{chunk-52PJ4QZ3.js.map → chunk-RTUIFVYF.js.map} +1 -1
- package/dist/exports.d.ts +1 -1
- package/dist/exports.d.ts.map +1 -1
- package/dist/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +1 -1
- package/dist/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +1 -1
- package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +3 -1
- package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
- package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +3 -1
- package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
- package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +5 -3
- package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
- package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +19 -3
- package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
- package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +3 -2
- package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -1
- package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +3 -2
- package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -1
- package/dist/implementation/zodSchema/zodCo.d.ts.map +1 -1
- package/dist/implementation/zodSchema/zodReExport.d.ts +2 -0
- package/dist/implementation/zodSchema/zodReExport.d.ts.map +1 -0
- package/dist/index.js +75 -6
- package/dist/index.js.map +1 -1
- package/dist/testing.js +1 -1
- package/dist/tests/coFeed.test-d.d.ts +2 -0
- package/dist/tests/coFeed.test-d.d.ts.map +1 -0
- package/dist/tests/coList.test-d.d.ts +2 -0
- package/dist/tests/coList.test-d.d.ts.map +1 -0
- package/dist/tests/zod.test.d.ts +2 -0
- package/dist/tests/zod.test.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/exports.ts +1 -1
- package/src/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +18 -2
- package/src/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +7 -1
- package/src/implementation/zodSchema/schemaTypes/CoListSchema.ts +7 -1
- package/src/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +10 -3
- package/src/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +37 -3
- package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +43 -30
- package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +45 -32
- package/src/implementation/zodSchema/zodCo.ts +23 -4
- package/src/implementation/zodSchema/zodReExport.ts +37 -0
- package/src/tests/coFeed.test-d.ts +256 -0
- package/src/tests/coFeed.test.ts +41 -18
- package/src/tests/coList.test-d.ts +245 -0
- package/src/tests/coMap.record.test-d.ts +34 -1
- package/src/tests/coMap.test-d.ts +1 -11
- package/src/tests/zod.test.ts +419 -0
@@ -13,6 +13,12 @@ import { InstanceOfSchema } from "../typeConverters/InstanceOfSchema.js";
|
|
13
13
|
import { InstanceOrPrimitiveOfSchema } from "../typeConverters/InstanceOrPrimitiveOfSchema.js";
|
14
14
|
import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "../typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
|
15
15
|
|
16
|
+
type CoFeedInit<T extends z.core.$ZodType> = Array<
|
17
|
+
T extends z.core.$ZodOptional<any>
|
18
|
+
? InstanceOrPrimitiveOfSchemaCoValuesNullable<T>
|
19
|
+
: NonNullable<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>>
|
20
|
+
>;
|
21
|
+
|
16
22
|
export type CoFeedSchema<T extends z.core.$ZodType> = z.core.$ZodCustom<
|
17
23
|
CoFeed<InstanceOfSchema<T>>,
|
18
24
|
unknown
|
@@ -22,7 +28,7 @@ export type CoFeedSchema<T extends z.core.$ZodType> = z.core.$ZodCustom<
|
|
22
28
|
element: T;
|
23
29
|
|
24
30
|
create(
|
25
|
-
init:
|
31
|
+
init: CoFeedInit<T>,
|
26
32
|
options?: { owner: Account | Group } | Account | Group,
|
27
33
|
): CoFeedInstance<T>;
|
28
34
|
|
@@ -13,11 +13,17 @@ import { InstanceOrPrimitiveOfSchema } from "../typeConverters/InstanceOrPrimiti
|
|
13
13
|
import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "../typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
|
14
14
|
import { WithHelpers } from "../zodSchema.js";
|
15
15
|
|
16
|
+
type CoListInit<T extends z.core.$ZodType> = Array<
|
17
|
+
T extends z.core.$ZodOptional<any>
|
18
|
+
? InstanceOrPrimitiveOfSchemaCoValuesNullable<T>
|
19
|
+
: NonNullable<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>>
|
20
|
+
>;
|
21
|
+
|
16
22
|
export type CoListSchema<T extends z.core.$ZodType> = z.core.$ZodArray<T> & {
|
17
23
|
collaborative: true;
|
18
24
|
|
19
25
|
create: (
|
20
|
-
items:
|
26
|
+
items: CoListInit<T>,
|
21
27
|
options?: { owner: Account | Group } | Account | Group,
|
22
28
|
) => CoList<InstanceOrPrimitiveOfSchema<T>>;
|
23
29
|
|
@@ -16,6 +16,15 @@ import { InstanceOrPrimitiveOfSchema } from "../typeConverters/InstanceOrPrimiti
|
|
16
16
|
import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "../typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
|
17
17
|
import { WithHelpers } from "../zodSchema.js";
|
18
18
|
|
19
|
+
type CoRecordInit<
|
20
|
+
K extends z.core.$ZodString<string>,
|
21
|
+
V extends z.core.$ZodType,
|
22
|
+
> = {
|
23
|
+
[key in z.output<K>]: V extends z.core.$ZodOptional<any>
|
24
|
+
? InstanceOrPrimitiveOfSchemaCoValuesNullable<V>
|
25
|
+
: NonNullable<InstanceOrPrimitiveOfSchemaCoValuesNullable<V>>;
|
26
|
+
};
|
27
|
+
|
19
28
|
export type CoRecordSchema<
|
20
29
|
K extends z.core.$ZodString<string>,
|
21
30
|
V extends z.core.$ZodType,
|
@@ -23,9 +32,7 @@ export type CoRecordSchema<
|
|
23
32
|
collaborative: true;
|
24
33
|
|
25
34
|
create: (
|
26
|
-
init: Simplify<
|
27
|
-
[key in z.output<K>]?: InstanceOrPrimitiveOfSchema<V>;
|
28
|
-
}>,
|
35
|
+
init: Simplify<CoRecordInit<K, V>>,
|
29
36
|
options?:
|
30
37
|
| {
|
31
38
|
owner: Account | Group;
|
@@ -1,9 +1,43 @@
|
|
1
1
|
import z from "zod/v4";
|
2
|
-
import {
|
2
|
+
import {
|
3
|
+
Account,
|
4
|
+
AnonymousJazzAgent,
|
5
|
+
FileStream,
|
6
|
+
Group,
|
7
|
+
} from "../../../internal.js";
|
3
8
|
|
4
9
|
export type FileStreamSchema = z.core.$ZodCustom<FileStream, unknown> & {
|
5
10
|
collaborative: true;
|
6
11
|
builtin: "FileStream";
|
7
|
-
create:
|
8
|
-
createFromBlob
|
12
|
+
create(options?: { owner?: Account | Group } | Account | Group): FileStream;
|
13
|
+
createFromBlob(
|
14
|
+
blob: Blob | File,
|
15
|
+
options?:
|
16
|
+
| {
|
17
|
+
owner?: Group | Account;
|
18
|
+
onProgress?: (progress: number) => void;
|
19
|
+
}
|
20
|
+
| Account
|
21
|
+
| Group,
|
22
|
+
): Promise<FileStream>;
|
23
|
+
loadAsBlob(
|
24
|
+
id: string,
|
25
|
+
options?: {
|
26
|
+
allowUnfinished?: boolean;
|
27
|
+
loadAs?: Account | AnonymousJazzAgent;
|
28
|
+
},
|
29
|
+
): Promise<Blob | undefined>;
|
30
|
+
load(
|
31
|
+
id: string,
|
32
|
+
options: { loadAs: Account | AnonymousJazzAgent },
|
33
|
+
): Promise<FileStream>;
|
34
|
+
subscribe(
|
35
|
+
id: string,
|
36
|
+
options: { loadAs: Account | AnonymousJazzAgent },
|
37
|
+
listener: (value: FileStream, unsubscribe: () => void) => void,
|
38
|
+
): () => void;
|
39
|
+
subscribe(
|
40
|
+
id: string,
|
41
|
+
listener: (value: FileStream, unsubscribe: () => void) => void,
|
42
|
+
): () => void;
|
9
43
|
};
|
@@ -1,3 +1,4 @@
|
|
1
|
+
import { JsonValue } from "cojson";
|
1
2
|
import z from "zod/v4";
|
2
3
|
import {
|
3
4
|
Account,
|
@@ -57,36 +58,48 @@ export type InstanceOrPrimitiveOfSchema<
|
|
57
58
|
? FileStream
|
58
59
|
: S extends z.core.$ZodOptional<infer Inner>
|
59
60
|
? InstanceOrPrimitiveOfSchema<Inner> | undefined
|
60
|
-
: S extends z.
|
61
|
-
?
|
62
|
-
:
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
: S extends z.core.$
|
72
|
-
?
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
: S extends z.core.$
|
80
|
-
?
|
81
|
-
: S extends z.core.$
|
82
|
-
?
|
83
|
-
: S extends z.core.$
|
84
|
-
?
|
85
|
-
: S extends z.core.$
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
61
|
+
: S extends z.ZodJSONSchema
|
62
|
+
? JsonValue
|
63
|
+
: S extends z.core.$ZodUnion<infer Members>
|
64
|
+
? InstanceOrPrimitiveOfSchema<Members[number]>
|
65
|
+
: // primitives below here - we manually traverse to ensure we only allow what we can handle
|
66
|
+
S extends z.core.$ZodObject<infer Shape>
|
67
|
+
? {
|
68
|
+
-readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<
|
69
|
+
Shape[key]
|
70
|
+
>;
|
71
|
+
}
|
72
|
+
: S extends z.core.$ZodArray<infer Item>
|
73
|
+
? InstanceOrPrimitiveOfSchema<Item>[]
|
74
|
+
: S extends z.core.$ZodTuple<infer Items>
|
75
|
+
? {
|
76
|
+
[key in keyof Items]: InstanceOrPrimitiveOfSchema<
|
77
|
+
Items[key]
|
78
|
+
>;
|
79
|
+
}
|
80
|
+
: S extends z.core.$ZodString
|
81
|
+
? string
|
82
|
+
: S extends z.core.$ZodNumber
|
83
|
+
? number
|
84
|
+
: S extends z.core.$ZodBoolean
|
85
|
+
? boolean
|
86
|
+
: S extends z.core.$ZodLiteral<
|
87
|
+
infer Literal
|
88
|
+
>
|
89
|
+
? Literal
|
90
|
+
: S extends z.core.$ZodDate
|
91
|
+
? Date
|
92
|
+
: S extends z.core.$ZodEnum<infer Enum>
|
93
|
+
? Enum[keyof Enum]
|
94
|
+
: S extends z.core.$ZodTemplateLiteral<
|
95
|
+
infer pattern
|
96
|
+
>
|
97
|
+
? pattern
|
98
|
+
: S extends z.core.$ZodReadonly<
|
99
|
+
infer Inner
|
100
|
+
>
|
101
|
+
? InstanceOrPrimitiveOfSchema<Inner>
|
102
|
+
: never
|
90
103
|
: S extends CoValueClass
|
91
104
|
? InstanceType<S>
|
92
105
|
: never;
|
package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
import { JsonValue } from "cojson";
|
1
2
|
import z from "zod/v4";
|
2
3
|
import {
|
3
4
|
Account,
|
@@ -68,38 +69,50 @@ export type InstanceOrPrimitiveOfSchemaCoValuesNullable<
|
|
68
69
|
?
|
69
70
|
| InstanceOrPrimitiveOfSchemaCoValuesNullable<Inner>
|
70
71
|
| undefined
|
71
|
-
: S extends z.
|
72
|
-
?
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
: S extends z.core.$
|
85
|
-
?
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
: S extends z.core.$
|
93
|
-
?
|
94
|
-
: S extends z.core.$
|
95
|
-
?
|
96
|
-
: S extends z.core.$
|
97
|
-
?
|
98
|
-
: S extends z.core.$
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
72
|
+
: S extends z.ZodJSONSchema
|
73
|
+
? JsonValue
|
74
|
+
: S extends z.core.$ZodUnion<infer Members>
|
75
|
+
? InstanceOrPrimitiveOfSchemaCoValuesNullable<
|
76
|
+
Members[number]
|
77
|
+
>
|
78
|
+
: // primitives below here - we manually traverse to ensure we only allow what we can handle
|
79
|
+
S extends z.core.$ZodObject<infer Shape>
|
80
|
+
? {
|
81
|
+
-readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<
|
82
|
+
Shape[key]
|
83
|
+
>;
|
84
|
+
}
|
85
|
+
: S extends z.core.$ZodArray<infer Item>
|
86
|
+
? InstanceOrPrimitiveOfSchema<Item>[]
|
87
|
+
: S extends z.core.$ZodTuple<infer Items>
|
88
|
+
? {
|
89
|
+
[key in keyof Items]: InstanceOrPrimitiveOfSchema<
|
90
|
+
Items[key]
|
91
|
+
>;
|
92
|
+
}
|
93
|
+
: S extends z.core.$ZodString
|
94
|
+
? string
|
95
|
+
: S extends z.core.$ZodNumber
|
96
|
+
? number
|
97
|
+
: S extends z.core.$ZodBoolean
|
98
|
+
? boolean
|
99
|
+
: S extends z.core.$ZodLiteral<
|
100
|
+
infer Literal
|
101
|
+
>
|
102
|
+
? Literal
|
103
|
+
: S extends z.core.$ZodDate
|
104
|
+
? Date
|
105
|
+
: S extends z.core.$ZodEnum<infer Enum>
|
106
|
+
? Enum[keyof Enum]
|
107
|
+
: S extends z.core.$ZodTemplateLiteral<
|
108
|
+
infer pattern
|
109
|
+
>
|
110
|
+
? pattern
|
111
|
+
: S extends z.core.$ZodReadonly<
|
112
|
+
infer Inner
|
113
|
+
>
|
114
|
+
? InstanceOrPrimitiveOfSchema<Inner>
|
115
|
+
: never
|
103
116
|
: S extends CoValueClass
|
104
117
|
? InstanceType<S> | null
|
105
118
|
: never;
|
@@ -306,8 +306,11 @@ export const coFileStreamDefiner = (): FileStreamSchema => {
|
|
306
306
|
> & {
|
307
307
|
collaborative: true;
|
308
308
|
builtin: "FileStream";
|
309
|
-
create:
|
310
|
-
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
|
321
|
+
} as FileStreamSchema["create"];
|
319
322
|
|
320
323
|
fileStreamSchema.createFromBlob = function (blob: Blob, options: any) {
|
321
324
|
return FileStream.createFromBlob(blob, options);
|
322
|
-
} as
|
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
|
};
|
@@ -0,0 +1,37 @@
|
|
1
|
+
export {
|
2
|
+
string,
|
3
|
+
number,
|
4
|
+
boolean,
|
5
|
+
union,
|
6
|
+
object,
|
7
|
+
array,
|
8
|
+
templateLiteral,
|
9
|
+
json,
|
10
|
+
tuple,
|
11
|
+
date,
|
12
|
+
emoji,
|
13
|
+
base64,
|
14
|
+
base64url,
|
15
|
+
nanoid,
|
16
|
+
cuid,
|
17
|
+
cuid2,
|
18
|
+
ulid,
|
19
|
+
ipv4,
|
20
|
+
ipv6,
|
21
|
+
email,
|
22
|
+
url,
|
23
|
+
uuid,
|
24
|
+
literal,
|
25
|
+
enum,
|
26
|
+
cidrv4,
|
27
|
+
cidrv6,
|
28
|
+
iso,
|
29
|
+
int32,
|
30
|
+
strictObject,
|
31
|
+
discriminatedUnion,
|
32
|
+
// intersection,
|
33
|
+
// record,
|
34
|
+
int,
|
35
|
+
optional,
|
36
|
+
type ZodOptional,
|
37
|
+
} from "zod/v4";
|
@@ -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
|
+
});
|