jazz-tools 0.7.0-alpha.0 → 0.7.0-alpha.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. package/.eslintrc.cjs +8 -1
  2. package/CHANGELOG.md +8 -0
  3. package/dist/coValues/account.js +138 -0
  4. package/dist/coValues/account.js.map +1 -0
  5. package/dist/coValues/coList.js +196 -0
  6. package/dist/coValues/coList.js.map +1 -0
  7. package/dist/coValues/coMap.js +249 -0
  8. package/dist/coValues/coMap.js.map +1 -0
  9. package/dist/coValues/coStream.js +189 -0
  10. package/dist/coValues/coStream.js.map +1 -0
  11. package/dist/coValues/extensions/imageDef.js +17 -12
  12. package/dist/coValues/extensions/imageDef.js.map +1 -1
  13. package/dist/coValues/group.js +96 -0
  14. package/dist/coValues/group.js.map +1 -0
  15. package/dist/coValues/interfaces.js +58 -0
  16. package/dist/coValues/interfaces.js.map +1 -0
  17. package/dist/implementation/errors.js.map +1 -0
  18. package/dist/implementation/inspect.js +2 -0
  19. package/dist/implementation/inspect.js.map +1 -0
  20. package/dist/{refs.js → implementation/refs.js} +11 -13
  21. package/dist/implementation/refs.js.map +1 -0
  22. package/dist/implementation/schema.js +6 -0
  23. package/dist/implementation/schema.js.map +1 -0
  24. package/dist/{subscriptionScope.js → implementation/subscriptionScope.js} +1 -1
  25. package/dist/implementation/subscriptionScope.js.map +1 -0
  26. package/dist/index.js +23 -27
  27. package/dist/index.js.map +1 -1
  28. package/dist/internal.js +13 -0
  29. package/dist/internal.js.map +1 -0
  30. package/dist/tests/coList.test.js +26 -22
  31. package/dist/tests/coList.test.js.map +1 -1
  32. package/dist/tests/coMap.test.js +91 -43
  33. package/dist/tests/coMap.test.js.map +1 -1
  34. package/dist/tests/coStream.test.js +35 -24
  35. package/dist/tests/coStream.test.js.map +1 -1
  36. package/package.json +11 -4
  37. package/src/coValues/account.ts +297 -0
  38. package/src/coValues/coList.ts +320 -0
  39. package/src/coValues/coMap.ts +447 -0
  40. package/src/coValues/coStream.ts +325 -0
  41. package/src/coValues/extensions/imageDef.ts +24 -16
  42. package/src/coValues/group.ts +187 -0
  43. package/src/coValues/interfaces.ts +216 -0
  44. package/src/implementation/inspect.ts +2 -0
  45. package/src/{refs.ts → implementation/refs.ts} +21 -32
  46. package/src/implementation/schema.ts +69 -0
  47. package/src/{subscriptionScope.ts → implementation/subscriptionScope.ts} +11 -13
  48. package/src/index.ts +22 -50
  49. package/src/internal.ts +15 -0
  50. package/src/tests/coList.test.ts +27 -25
  51. package/src/tests/coMap.test.ts +125 -50
  52. package/src/tests/coStream.test.ts +51 -38
  53. package/dist/coValueInterfaces.js +0 -8
  54. package/dist/coValueInterfaces.js.map +0 -1
  55. package/dist/coValues/account/account.js +0 -11
  56. package/dist/coValues/account/account.js.map +0 -1
  57. package/dist/coValues/account/accountOf.js +0 -150
  58. package/dist/coValues/account/accountOf.js.map +0 -1
  59. package/dist/coValues/account/migration.js +0 -4
  60. package/dist/coValues/account/migration.js.map +0 -1
  61. package/dist/coValues/coList/coList.js +0 -2
  62. package/dist/coValues/coList/coList.js.map +0 -1
  63. package/dist/coValues/coList/coListOf.js +0 -235
  64. package/dist/coValues/coList/coListOf.js.map +0 -1
  65. package/dist/coValues/coList/internalDocs.js +0 -2
  66. package/dist/coValues/coList/internalDocs.js.map +0 -1
  67. package/dist/coValues/coMap/coMap.js +0 -2
  68. package/dist/coValues/coMap/coMap.js.map +0 -1
  69. package/dist/coValues/coMap/coMapOf.js +0 -262
  70. package/dist/coValues/coMap/coMapOf.js.map +0 -1
  71. package/dist/coValues/coMap/internalDocs.js +0 -2
  72. package/dist/coValues/coMap/internalDocs.js.map +0 -1
  73. package/dist/coValues/coStream/coStream.js +0 -2
  74. package/dist/coValues/coStream/coStream.js.map +0 -1
  75. package/dist/coValues/coStream/coStreamOf.js +0 -244
  76. package/dist/coValues/coStream/coStreamOf.js.map +0 -1
  77. package/dist/coValues/construction.js +0 -34
  78. package/dist/coValues/construction.js.map +0 -1
  79. package/dist/coValues/group/group.js +0 -2
  80. package/dist/coValues/group/group.js.map +0 -1
  81. package/dist/coValues/group/groupOf.js +0 -109
  82. package/dist/coValues/group/groupOf.js.map +0 -1
  83. package/dist/coValues/resolution.js +0 -66
  84. package/dist/coValues/resolution.js.map +0 -1
  85. package/dist/errors.js.map +0 -1
  86. package/dist/refs.js.map +0 -1
  87. package/dist/schemaHelpers.js +0 -14
  88. package/dist/schemaHelpers.js.map +0 -1
  89. package/dist/subscriptionScope.js.map +0 -1
  90. package/dist/tests/types.test.js +0 -33
  91. package/dist/tests/types.test.js.map +0 -1
  92. package/src/coValueInterfaces.ts +0 -105
  93. package/src/coValues/account/account.ts +0 -106
  94. package/src/coValues/account/accountOf.ts +0 -284
  95. package/src/coValues/account/migration.ts +0 -12
  96. package/src/coValues/coList/coList.ts +0 -57
  97. package/src/coValues/coList/coListOf.ts +0 -377
  98. package/src/coValues/coList/internalDocs.ts +0 -1
  99. package/src/coValues/coMap/coMap.ts +0 -110
  100. package/src/coValues/coMap/coMapOf.ts +0 -451
  101. package/src/coValues/coMap/internalDocs.ts +0 -1
  102. package/src/coValues/coStream/coStream.ts +0 -63
  103. package/src/coValues/coStream/coStreamOf.ts +0 -404
  104. package/src/coValues/construction.ts +0 -110
  105. package/src/coValues/group/group.ts +0 -27
  106. package/src/coValues/group/groupOf.ts +0 -183
  107. package/src/coValues/resolution.ts +0 -111
  108. package/src/schemaHelpers.ts +0 -72
  109. package/src/tests/types.test.ts +0 -37
  110. /package/dist/{errors.js → implementation/errors.js} +0 -0
  111. /package/src/{errors.ts → implementation/errors.ts} +0 -0
@@ -1,183 +0,0 @@
1
- import {
2
- CoValueSchema,
3
- ID,
4
- inspect,
5
- isCoValueSchema,
6
- } from "../../coValueInterfaces.js";
7
- import * as S from "@effect/schema/Schema";
8
- import { AnyGroup, GroupSchema } from "./group.js";
9
- import {
10
- AnyAccount,
11
- ControlledAccount,
12
- isControlledAccount,
13
- } from "../account/account.js";
14
- import { AST, Schema } from "@effect/schema";
15
- import { constructorOfSchemaSym } from "../resolution.js";
16
- import { pipeArguments } from "effect/Pipeable";
17
- import { Everyone, RawGroup, Role } from "cojson";
18
- import { ValueRef } from "../../refs.js";
19
- import { controlledAccountFromNode } from "../account/accountOf.js";
20
- import { SharedCoValueConstructor } from "../construction.js";
21
-
22
- export function GroupOf<
23
- P extends CoValueSchema | S.Schema<null>,
24
- R extends CoValueSchema | S.Schema<null>,
25
- >(fields: { profile: P; root: R }) {
26
- class GroupOfProfileAndRoot
27
- extends SharedCoValueConstructor
28
- implements AnyGroup<P, R>
29
- {
30
- static get ast() {
31
- return AST.setAnnotation(
32
- Schema.instanceOf(this).ast,
33
- constructorOfSchemaSym,
34
- this
35
- );
36
- }
37
- static [Schema.TypeId]: Schema.Schema.Variance<
38
- AnyGroup<P, R> & GroupOfProfileAndRoot,
39
- AnyGroup<P, R> & GroupOfProfileAndRoot,
40
- never
41
- >[Schema.TypeId];
42
- static pipe() {
43
- // eslint-disable-next-line prefer-rest-params
44
- return pipeArguments(this, arguments);
45
- }
46
- static type = "Group" as const;
47
-
48
- id!: ID<this>;
49
- _type!: "Group";
50
- _owner!: AnyAccount | AnyGroup;
51
- _refs!: AnyGroup<P, R>["_refs"];
52
- _raw!: RawGroup;
53
- _loadedAs!: ControlledAccount;
54
- _schema!: typeof GroupOfProfileAndRoot;
55
-
56
- constructor(options: { owner: AnyAccount | AnyGroup });
57
- constructor(init: any, options: { fromRaw: RawGroup });
58
- constructor(init: undefined, options: { owner: AnyAccount | AnyGroup });
59
- constructor(
60
- init: undefined | { owner: AnyAccount | AnyGroup },
61
- options?: { fromRaw: RawGroup } | { owner: AnyAccount | AnyGroup }
62
- ) {
63
- super();
64
- let raw: RawGroup;
65
-
66
- if (options && "fromRaw" in options) {
67
- raw = options.fromRaw;
68
- } else {
69
- const initOwner = options?.owner || init?.owner;
70
- if (!initOwner) throw new Error("No owner provided");
71
- if (isControlledAccount(initOwner)) {
72
- const rawOwner = initOwner._raw;
73
- raw = rawOwner.createGroup();
74
- } else {
75
- throw new Error(
76
- "Can only construct group as a controlled account"
77
- );
78
- }
79
- }
80
-
81
- const refs = {
82
- get profile() {
83
- if (isCoValueSchema(fields.profile)) {
84
- const profileID = raw.get("profile");
85
- return (
86
- profileID &&
87
- new ValueRef(
88
- profileID as unknown as ID<
89
- Schema.Schema.To<typeof fields.profile>
90
- >,
91
- controlledAccountFromNode(raw.core.node),
92
- fields.profile
93
- )
94
- );
95
- }
96
- },
97
- get root() {
98
- if (isCoValueSchema(fields.root)) {
99
- const rootID = raw.get("root");
100
- return (
101
- rootID &&
102
- new ValueRef(
103
- rootID as unknown as ID<
104
- Schema.Schema.To<typeof fields.root>
105
- >,
106
- controlledAccountFromNode(raw.core.node),
107
- fields.root
108
- )
109
- );
110
- }
111
- },
112
- };
113
-
114
- Object.defineProperties(this, {
115
- id: { value: raw.id, enumerable: false },
116
- _type: { value: "Group", enumerable: false },
117
- _owner: { value: this, enumerable: false },
118
- _refs: { value: refs, enumerable: false },
119
- _raw: { value: raw, enumerable: false },
120
- _loadedAs: {
121
- get: () => controlledAccountFromNode(raw.core.node),
122
- enumerable: false,
123
- },
124
- _schema: { value: GroupOfProfileAndRoot, enumerable: false },
125
- });
126
- }
127
-
128
- static fromRaw(raw: RawGroup) {
129
- return new GroupOfProfileAndRoot(undefined, {
130
- fromRaw: raw,
131
- });
132
- }
133
-
134
- get profile(): S.Schema.To<P> | undefined {
135
- return this._refs.profile.accessFrom(this);
136
- }
137
-
138
- get root(): S.Schema.To<R> | undefined {
139
- return this._refs.root.accessFrom(this);
140
- }
141
-
142
- addMember(member: AnyAccount | Everyone, role: Role) {
143
- this._raw.addMember(
144
- typeof member === "string" ? member : member._raw,
145
- role
146
- );
147
-
148
- return this;
149
- }
150
-
151
- myRole(): Role | undefined {
152
- return this._raw.myRole();
153
- }
154
-
155
- toJSON() {
156
- return {
157
- co: {
158
- id: this.id,
159
- type: this._type,
160
- },
161
- profile: this.profile?.toJSON(),
162
- root: this.root?.toJSON(),
163
- };
164
- }
165
-
166
- [inspect]() {
167
- return this.toJSON();
168
- }
169
-
170
- static as<SubClass>() {
171
- return this as unknown as GroupSchema<SubClass, P, R>;
172
- }
173
- }
174
-
175
- return GroupOfProfileAndRoot as GroupSchema<GroupOfProfileAndRoot, P, R> & {
176
- as<SubClass>(): GroupSchema<SubClass, P, R>;
177
- };
178
- }
179
-
180
- export class Group extends GroupOf({
181
- profile: S.null,
182
- root: S.null,
183
- }).as<Group>() {}
@@ -1,111 +0,0 @@
1
- import { Schema, AST } from "@effect/schema";
2
- import { RawCoValue } from "cojson";
3
- import { CoValue, CoValueSchema } from "../coValueInterfaces.js";
4
-
5
- // TODO: can we get rid of this because effect schema already has an annotation for the constructor?
6
- export const constructorOfSchemaSym = Symbol.for("@jazz/constructorOfSymbol");
7
- export type constructorOfSchemaSym = typeof constructorOfSchemaSym;
8
-
9
- export function propertyIsCoValueSchema(
10
- prop:
11
- | Schema.Schema<any>
12
- | Schema.PropertySignature<any, boolean, any, boolean, never>
13
- ): prop is CoValueSchema {
14
- if ("propertySignatureAST" in prop) {
15
- return astIsCoValueSchema(
16
- (prop.propertySignatureAST as { from: AST.AST }).from
17
- );
18
- } else {
19
- return astIsCoValueSchema((prop as Schema.Schema<unknown>).ast);
20
- }
21
- }
22
-
23
- function astIsCoValueSchema(ast: AST.AST): boolean {
24
- if (
25
- (ast._tag === "Declaration") &&
26
- ast.annotations[constructorOfSchemaSym]
27
- ) {
28
- return true;
29
- } else if (ast._tag === "Union") {
30
- return ast.types.every(
31
- (member) =>
32
- member._tag === "UndefinedKeyword" || astIsCoValueSchema(member)
33
- );
34
- } else if (
35
- ast._tag === "BooleanKeyword" ||
36
- ast._tag === "NumberKeyword" ||
37
- ast._tag === "StringKeyword" ||
38
- ast._tag === "Literal" ||
39
- ast._tag === "Tuple"
40
- ) {
41
- return false;
42
- } else if (ast._tag === "Refinement") {
43
- return astIsCoValueSchema(ast.from);
44
- } else if (ast._tag === "Transform") {
45
- return astIsCoValueSchema(ast.from);
46
- } else {
47
- throw new Error(
48
- `astIsCoValueSchema can't yet handle ${ast._tag}: ${JSON.stringify(
49
- ast
50
- )}`
51
- );
52
- }
53
- }
54
-
55
- export function getCoValueConstructorInProperty(
56
- prop:
57
- | Schema.Schema<unknown>
58
- | Schema.PropertySignature<unknown, boolean, unknown, boolean, never>,
59
- rawValue: RawCoValue
60
- ):
61
- | (new (init: undefined, options: { fromRaw: RawCoValue }) => CoValue)
62
- | undefined {
63
- if ("propertySignatureAST" in prop) {
64
- return getCoValueConstructorInAST(
65
- (prop.propertySignatureAST as { from: AST.AST }).from,
66
- rawValue
67
- );
68
- } else {
69
- return getCoValueConstructorInAST(
70
- (prop as Schema.Schema<unknown>).ast,
71
- rawValue
72
- );
73
- }
74
- }
75
-
76
- // TODO (optimization): make this meta, so this creates a tailor-made function that will take a RawCoValue at call time
77
- function getCoValueConstructorInAST(
78
- ast: AST.AST,
79
- rawValue: RawCoValue
80
- ):
81
- | (new (init: undefined, options: { fromRaw: RawCoValue }) => CoValue)
82
- | undefined {
83
- if (
84
- (ast._tag === "Declaration") &&
85
- ast.annotations[constructorOfSchemaSym]
86
- ) {
87
- return ast.annotations[constructorOfSchemaSym] as new (
88
- init: undefined,
89
- options: { fromRaw: RawCoValue }
90
- ) => CoValue;
91
- } else if (ast._tag === "Union" && ast.types.length === 2) {
92
- const [a, b] = ast.types;
93
- if (a._tag === "UndefinedKeyword") {
94
- return getCoValueConstructorInAST(b, rawValue);
95
- } else if (b._tag === "UndefinedKeyword") {
96
- return getCoValueConstructorInAST(a, rawValue);
97
- } else {
98
- throw new Error(
99
- `getCoValueConstructorInAST can't yet handle Union of: ${JSON.stringify(
100
- ast.types
101
- )}`
102
- );
103
- }
104
- } else {
105
- throw new Error(
106
- `getCoValueConstructorInAST can't yet handle ${
107
- ast._tag
108
- }: ${JSON.stringify(ast)}`
109
- );
110
- }
111
- }
@@ -1,72 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { AST } from "@effect/schema";
3
- import * as S from "@effect/schema/Schema";
4
-
5
- export type SchemaWithOutput<I> = S.Schema<any, any, never> & {
6
- [S.TypeId]: {
7
- _I: (_: any) => I;
8
- };
9
- };
10
-
11
- export type SchemaWithInputAndOutput<A, I = A> = S.Schema<any, any, never> & {
12
- [S.TypeId]: {
13
- _A: (_: any) => A;
14
- _I: (_: any) => I;
15
- };
16
- };
17
-
18
- export type PropertySignatureWithOutput<I> = S.PropertySignature<
19
- any,
20
- boolean,
21
- any,
22
- boolean,
23
- never
24
- > & {
25
- [S.TypeId]: {
26
- _I: (_: any) => I | undefined;
27
- };
28
- };
29
-
30
- export type PropertySignatureWithInputAndOutput<I, A> = S.PropertySignature<
31
- any,
32
- boolean,
33
- any,
34
- boolean,
35
- never
36
- > & {
37
- [S.TypeId]: {
38
- _A: (_: any) => A | undefined;
39
- _I: (_: any) => I | undefined;
40
- };
41
- };
42
-
43
- export type PropDef<I> =
44
- | S.Schema<I>
45
- | S.PropertySignature<I, boolean, I, boolean, never>;
46
-
47
- export function CoSchema<Raw extends S.Schema<any>>(
48
- raw: Raw
49
- ): Raw & S.Schema<Raw, any, any> {
50
- return raw as any;
51
- }
52
-
53
- export function propSigToSchema<
54
- S extends S.Schema<any> | PropertySignatureWithInputAndOutput<any, any>,
55
- >(
56
- propSig: S
57
- ): S extends S.Schema<any>
58
- ? S
59
- : S extends PropertySignatureWithInputAndOutput<infer A, infer I>
60
- ? S.Schema<A, I>
61
- : never {
62
- if ("ast" in propSig) {
63
- return propSig as any;
64
- } else {
65
- const ast = (
66
- propSig as unknown as {
67
- propertySignatureAST: { from: AST.AST };
68
- }
69
- ).propertySignatureAST;
70
- return S.make<any, unknown, never>(ast.from) as any;
71
- }
72
- }
@@ -1,37 +0,0 @@
1
- import { Account, Co, ImageDefinition, S, jazzReady } from "..";
2
- import { describe, test, beforeEach, expectTypeOf } from "vitest";
3
- import { ValueRef } from "../refs";
4
- import { webcrypto } from "node:crypto";
5
-
6
- if (!("crypto" in globalThis)) {
7
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
- (globalThis as any).crypto = webcrypto;
9
- }
10
-
11
- beforeEach(async () => {
12
- await jazzReady;
13
- });
14
-
15
- class TestMap extends Co.map({
16
- name: S.string,
17
- image: Co.media.imageDef,
18
- maybeImage: S.optional(Co.media.imageDef),
19
- }).as<TestMap>() {}
20
-
21
- describe("CoMap type tests", () => {
22
- test("Optional field refs work", async () => {
23
- const me = await Account.create({
24
- name: "Hermes Puggington",
25
- });
26
-
27
- const map = new TestMap({
28
- name: "Hermes",
29
- image: new ImageDefinition({
30
- originalSize: [100, 100],
31
- placeholderDataURL: "data:image/png;base64,",
32
- }, { owner: me }),
33
- }, {owner: me});
34
- expectTypeOf(map._refs.image).toMatchTypeOf<ValueRef<ImageDefinition>>();
35
- expectTypeOf(map._refs.maybeImage).toMatchTypeOf<ValueRef<ImageDefinition>>();
36
- })
37
- })
File without changes
File without changes