@assemora/schema 0.1.0
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/LICENSE +202 -0
- package/README.md +35 -0
- package/dist/blocks.d.ts +56 -0
- package/dist/blocks.d.ts.map +1 -0
- package/dist/blocks.js +152 -0
- package/dist/blocks.js.map +1 -0
- package/dist/composites-bridge.d.ts +4 -0
- package/dist/composites-bridge.d.ts.map +1 -0
- package/dist/composites-bridge.js +4 -0
- package/dist/composites-bridge.js.map +1 -0
- package/dist/composites.d.ts +19 -0
- package/dist/composites.d.ts.map +1 -0
- package/dist/composites.js +120 -0
- package/dist/composites.js.map +1 -0
- package/dist/design.d.ts +92 -0
- package/dist/design.d.ts.map +1 -0
- package/dist/design.js +86 -0
- package/dist/design.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/patch.d.ts +18 -0
- package/dist/patch.d.ts.map +1 -0
- package/dist/patch.js +43 -0
- package/dist/patch.js.map +1 -0
- package/dist/primitives.d.ts +85 -0
- package/dist/primitives.d.ts.map +1 -0
- package/dist/primitives.js +377 -0
- package/dist/primitives.js.map +1 -0
- package/dist/types.d.ts +96 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +16 -0
- package/dist/types.js.map +1 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Assemora
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# `@assemora/schema`
|
|
2
|
+
|
|
3
|
+
Schema primitives: field definitions, type inference, neutral JSON representation.
|
|
4
|
+
|
|
5
|
+
**Implementation phase:** 1 — implemented.
|
|
6
|
+
|
|
7
|
+
One declaration is at once a runtime parser, a compile-time type and a JSON Schema
|
|
8
|
+
description, so validation, the database, Studio forms, OpenAPI, the SDK and MCP all
|
|
9
|
+
read a single source (SPEC.md §3.4, §42).
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { enumOf, number, object, string, uuid, type Infer } from '@assemora/schema'
|
|
13
|
+
|
|
14
|
+
const Article = object({
|
|
15
|
+
id: uuid(),
|
|
16
|
+
title: string().min(2),
|
|
17
|
+
status: enumOf('draft', 'published'),
|
|
18
|
+
views: number().optional(),
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
type Article = Infer<typeof Article>
|
|
22
|
+
// { id: string; title: string; status: 'draft' | 'published'; views?: number }
|
|
23
|
+
|
|
24
|
+
const result = Article.parse(payload)
|
|
25
|
+
if (!result.ok) result.issues // [{ path: ['title'], code: 'min', message: '...' }]
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Parsing never throws — the caller decides what a failure means. Keys outside the
|
|
29
|
+
declared shape are dropped rather than passed through, which is how mass assignment
|
|
30
|
+
is prevented (SPEC.md §85).
|
|
31
|
+
|
|
32
|
+
## Workspace dependencies
|
|
33
|
+
|
|
34
|
+
_none, and there never will be any._ `pnpm boundaries` enforces it: every layer and
|
|
35
|
+
every browser bundle reads this package.
|
package/dist/blocks.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type BlockDesign } from './design.js';
|
|
2
|
+
import type { Schema } from './types.js';
|
|
3
|
+
export type BlockNode = {
|
|
4
|
+
/** Stable and immutable for the life of the block (SPEC.md §54). */
|
|
5
|
+
readonly id: string;
|
|
6
|
+
readonly type: string;
|
|
7
|
+
readonly version: number;
|
|
8
|
+
readonly props: Readonly<Record<string, unknown>>;
|
|
9
|
+
readonly children: readonly BlockNode[];
|
|
10
|
+
/** Hidden blocks stay in the tree and out of the rendered page (SPEC.md §60). */
|
|
11
|
+
readonly hidden?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* The seven universal controls of SPEC.md §61.
|
|
14
|
+
*
|
|
15
|
+
* Beside `props` rather than inside it: `props` is the block author's, and a
|
|
16
|
+
* framework key in there would collide with a field somebody declared.
|
|
17
|
+
*/
|
|
18
|
+
readonly design?: BlockDesign;
|
|
19
|
+
};
|
|
20
|
+
export type BlockTree = {
|
|
21
|
+
readonly blocks: readonly BlockNode[];
|
|
22
|
+
};
|
|
23
|
+
export declare const emptyTree: () => BlockTree;
|
|
24
|
+
/** Depth-first, parents before children. The order Studio and a renderer walk in. */
|
|
25
|
+
export declare function walkBlocks(tree: BlockTree): Generator<BlockNode>;
|
|
26
|
+
export declare const findBlock: (tree: BlockTree, id: string) => BlockNode | undefined;
|
|
27
|
+
/** Every id in the tree, in walk order. Used to prove they stay unique. */
|
|
28
|
+
export declare const blockIds: (tree: BlockTree) => string[];
|
|
29
|
+
/** Validates a stored tree. What a JSONB column is checked against on the way in. */
|
|
30
|
+
export declare const blockTree: () => Schema<BlockTree>;
|
|
31
|
+
export type ChangedBlock = {
|
|
32
|
+
readonly id: string;
|
|
33
|
+
readonly type: string;
|
|
34
|
+
/** Which of the block's own props differ. Empty when only the design changed. */
|
|
35
|
+
readonly fields: readonly string[];
|
|
36
|
+
readonly design: boolean;
|
|
37
|
+
readonly hidden: boolean;
|
|
38
|
+
};
|
|
39
|
+
export type TreeChange = {
|
|
40
|
+
readonly added: readonly BlockNode[];
|
|
41
|
+
readonly removed: readonly BlockNode[];
|
|
42
|
+
readonly changed: readonly ChangedBlock[];
|
|
43
|
+
readonly moved: readonly BlockNode[];
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* What actually happened to a page (SPEC.md §65).
|
|
47
|
+
*
|
|
48
|
+
* A field-level diff of two pages says `draftTree` changed and hands over both
|
|
49
|
+
* trees, which is true and useless. This says "the hero's title changed" — the shape
|
|
50
|
+
* SPEC.md §65 shows in Studio, and the one a change set needs (SPEC.md §75).
|
|
51
|
+
*
|
|
52
|
+
* It lives here because it is knowledge about the tree, and the tree lives here: the
|
|
53
|
+
* editor, an agent and the API all read the same answer.
|
|
54
|
+
*/
|
|
55
|
+
export declare const diffTrees: (before: BlockTree, after: BlockTree) => TreeChange;
|
|
56
|
+
//# sourceMappingURL=blocks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"blocks.d.ts","sourceRoot":"","sources":["../src/blocks.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,KAAK,WAAW,EAAe,MAAM,aAAa,CAAA;AAC3D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAExC,MAAM,MAAM,SAAS,GAAG;IACtB,oEAAoE;IACpE,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAA;IACnB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAA;IACjD,QAAQ,CAAC,QAAQ,EAAE,SAAS,SAAS,EAAE,CAAA;IACvC,iFAAiF;IACjF,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAA;IACzB;;;;;OAKG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAA;CAC9B,CAAA;AAED,MAAM,MAAM,SAAS,GAAG;IACtB,QAAQ,CAAC,MAAM,EAAE,SAAS,SAAS,EAAE,CAAA;CACtC,CAAA;AAED,eAAO,MAAM,SAAS,QAAO,SAA6B,CAAA;AAE1D,qFAAqF;AACrF,wBAAiB,UAAU,CAAC,IAAI,EAAE,SAAS,GAAG,SAAS,CAAC,SAAS,CAAC,CASjE;AAED,eAAO,MAAM,SAAS,SAAU,SAAS,MAAM,MAAM,KAAG,SAAS,GAAG,SAMnE,CAAA;AAED,2EAA2E;AAC3E,eAAO,MAAM,QAAQ,SAAU,SAAS,KAAG,MAAM,EAAkD,CAAA;AAyCnG,qFAAqF;AACrF,eAAO,MAAM,SAAS,QAAO,MAAM,CAAC,SAAS,CAmB5C,CAAA;AAED,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAA;IACnB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,iFAAiF;IACjF,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAA;IAClC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAA;IACxB,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAA;CACzB,CAAA;AAED,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,KAAK,EAAE,SAAS,SAAS,EAAE,CAAA;IACpC,QAAQ,CAAC,OAAO,EAAE,SAAS,SAAS,EAAE,CAAA;IACtC,QAAQ,CAAC,OAAO,EAAE,SAAS,YAAY,EAAE,CAAA;IACzC,QAAQ,CAAC,KAAK,EAAE,SAAS,SAAS,EAAE,CAAA;CACrC,CAAA;AAgCD;;;;;;;;;GASG;AACH,eAAO,MAAM,SAAS,WAAY,SAAS,SAAS,SAAS,KAAG,UAuC/D,CAAA"}
|
package/dist/blocks.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The block tree (SPEC.md §54).
|
|
3
|
+
*
|
|
4
|
+
* A page is a tree of blocks and never an HTML blob (SPEC.md §125.14), so the shape
|
|
5
|
+
* of that tree is data every layer agrees on: the page layer edits it, Studio renders
|
|
6
|
+
* it, an agent reads and proposes changes to it, and `@assemora/react` draws it.
|
|
7
|
+
*
|
|
8
|
+
* It lives in this package for one reason: `@assemora/react` depends on `schema` and
|
|
9
|
+
* on nothing else. Were these types in `@assemora/pages`, a browser bundle would drag
|
|
10
|
+
* the whole server layer in behind them (docs/architecture/package-graph.md).
|
|
11
|
+
*/
|
|
12
|
+
import { array, boolean, json, number, object, string } from './composites-bridge.js';
|
|
13
|
+
import { blockDesign } from './design.js';
|
|
14
|
+
export const emptyTree = () => ({ blocks: [] });
|
|
15
|
+
/** Depth-first, parents before children. The order Studio and a renderer walk in. */
|
|
16
|
+
export function* walkBlocks(tree) {
|
|
17
|
+
const visit = function* (nodes) {
|
|
18
|
+
for (const node of nodes) {
|
|
19
|
+
yield node;
|
|
20
|
+
yield* visit(node.children);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
yield* visit(tree.blocks);
|
|
24
|
+
}
|
|
25
|
+
export const findBlock = (tree, id) => {
|
|
26
|
+
for (const node of walkBlocks(tree)) {
|
|
27
|
+
if (node.id === id)
|
|
28
|
+
return node;
|
|
29
|
+
}
|
|
30
|
+
return undefined;
|
|
31
|
+
};
|
|
32
|
+
/** Every id in the tree, in walk order. Used to prove they stay unique. */
|
|
33
|
+
export const blockIds = (tree) => [...walkBlocks(tree)].map((node) => node.id);
|
|
34
|
+
const blockNodeSchema = () => {
|
|
35
|
+
const node = {
|
|
36
|
+
kind: 'object',
|
|
37
|
+
isOptional: false,
|
|
38
|
+
isNullable: false,
|
|
39
|
+
description: 'A block in a page tree',
|
|
40
|
+
parse: (value) => {
|
|
41
|
+
const shape = object({
|
|
42
|
+
id: string().min(1),
|
|
43
|
+
type: string().min(1),
|
|
44
|
+
version: number().integer(),
|
|
45
|
+
props: json(),
|
|
46
|
+
children: array(node),
|
|
47
|
+
hidden: boolean().optional(),
|
|
48
|
+
design: blockDesign().optional(),
|
|
49
|
+
});
|
|
50
|
+
const result = shape.parse(value);
|
|
51
|
+
return result.ok ? { ok: true, value: result.value } : result;
|
|
52
|
+
},
|
|
53
|
+
toJsonSchema: () => ({
|
|
54
|
+
type: 'object',
|
|
55
|
+
properties: {
|
|
56
|
+
id: { type: 'string' },
|
|
57
|
+
type: { type: 'string' },
|
|
58
|
+
version: { type: 'integer' },
|
|
59
|
+
props: { type: 'object' },
|
|
60
|
+
children: { type: 'array', items: { $ref: '#' } },
|
|
61
|
+
hidden: { type: 'boolean' },
|
|
62
|
+
design: blockDesign().toJsonSchema(),
|
|
63
|
+
},
|
|
64
|
+
required: ['id', 'type', 'version', 'props', 'children'],
|
|
65
|
+
}),
|
|
66
|
+
};
|
|
67
|
+
return node;
|
|
68
|
+
};
|
|
69
|
+
/** Validates a stored tree. What a JSONB column is checked against on the way in. */
|
|
70
|
+
export const blockTree = () => {
|
|
71
|
+
const blocks = array(blockNodeSchema());
|
|
72
|
+
return {
|
|
73
|
+
kind: 'object',
|
|
74
|
+
isOptional: false,
|
|
75
|
+
isNullable: false,
|
|
76
|
+
description: 'A page as a tree of blocks',
|
|
77
|
+
parse: (value) => {
|
|
78
|
+
const result = object({ blocks }).parse(value);
|
|
79
|
+
return result.ok ? { ok: true, value: result.value } : result;
|
|
80
|
+
},
|
|
81
|
+
toJsonSchema: () => ({
|
|
82
|
+
type: 'object',
|
|
83
|
+
properties: { blocks: { type: 'array' } },
|
|
84
|
+
required: ['blocks'],
|
|
85
|
+
}),
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
const byId = (tree) => new Map([...walkBlocks(tree)].map((node) => [node.id, node]));
|
|
89
|
+
const differ = (left, right) => JSON.stringify(left ?? null) !== JSON.stringify(right ?? null);
|
|
90
|
+
/**
|
|
91
|
+
* Where each block sits, as its parent and the siblings before it.
|
|
92
|
+
*
|
|
93
|
+
* The order among *surviving* siblings is what matters: a block that shifted down
|
|
94
|
+
* because something was inserted above it has not moved, and saying it did would
|
|
95
|
+
* bury the one edit somebody made under a list of everything after it.
|
|
96
|
+
*/
|
|
97
|
+
const positions = (tree, common) => {
|
|
98
|
+
const at = new Map();
|
|
99
|
+
const visit = (nodes, parent) => {
|
|
100
|
+
const surviving = nodes.filter((node) => common.has(node.id));
|
|
101
|
+
for (const node of nodes) {
|
|
102
|
+
at.set(node.id, `${parent}/${surviving.findIndex((sibling) => sibling.id === node.id)}`);
|
|
103
|
+
visit(node.children, node.id);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
visit(tree.blocks, '');
|
|
107
|
+
return at;
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* What actually happened to a page (SPEC.md §65).
|
|
111
|
+
*
|
|
112
|
+
* A field-level diff of two pages says `draftTree` changed and hands over both
|
|
113
|
+
* trees, which is true and useless. This says "the hero's title changed" — the shape
|
|
114
|
+
* SPEC.md §65 shows in Studio, and the one a change set needs (SPEC.md §75).
|
|
115
|
+
*
|
|
116
|
+
* It lives here because it is knowledge about the tree, and the tree lives here: the
|
|
117
|
+
* editor, an agent and the API all read the same answer.
|
|
118
|
+
*/
|
|
119
|
+
export const diffTrees = (before, after) => {
|
|
120
|
+
const was = byId(before);
|
|
121
|
+
const is = byId(after);
|
|
122
|
+
const common = new Set([...was.keys()].filter((id) => is.has(id)));
|
|
123
|
+
const wasAt = positions(before, common);
|
|
124
|
+
const isAt = positions(after, common);
|
|
125
|
+
const added = [];
|
|
126
|
+
const removed = [];
|
|
127
|
+
const changed = [];
|
|
128
|
+
const moved = [];
|
|
129
|
+
for (const [id, node] of is) {
|
|
130
|
+
const previous = was.get(id);
|
|
131
|
+
if (previous === undefined) {
|
|
132
|
+
added.push(node);
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
const fields = [...new Set([...Object.keys(previous.props), ...Object.keys(node.props)])]
|
|
136
|
+
.filter((name) => differ(previous.props[name], node.props[name]))
|
|
137
|
+
.sort();
|
|
138
|
+
const design = differ(previous.design, node.design);
|
|
139
|
+
const hidden = (previous.hidden ?? false) !== (node.hidden ?? false);
|
|
140
|
+
if (fields.length > 0 || design || hidden) {
|
|
141
|
+
changed.push({ id, type: node.type, fields, design, hidden });
|
|
142
|
+
}
|
|
143
|
+
if (wasAt.get(id) !== isAt.get(id))
|
|
144
|
+
moved.push(node);
|
|
145
|
+
}
|
|
146
|
+
for (const [id, node] of was) {
|
|
147
|
+
if (!is.has(id))
|
|
148
|
+
removed.push(node);
|
|
149
|
+
}
|
|
150
|
+
return { added, removed, changed, moved };
|
|
151
|
+
};
|
|
152
|
+
//# sourceMappingURL=blocks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"blocks.js","sourceRoot":"","sources":["../src/blocks.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AACrF,OAAO,EAAoB,WAAW,EAAE,MAAM,aAAa,CAAA;AAyB3D,MAAM,CAAC,MAAM,SAAS,GAAG,GAAc,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,CAAA;AAE1D,qFAAqF;AACrF,MAAM,SAAS,CAAC,CAAC,UAAU,CAAC,IAAe;IACzC,MAAM,KAAK,GAAG,QAAQ,CAAC,EAAE,KAA2B;QAClD,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,MAAM,IAAI,CAAA;YACV,KAAK,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QAC7B,CAAC;IACH,CAAC,CAAA;IAED,KAAK,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;AAC3B,CAAC;AAED,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,IAAe,EAAE,EAAU,EAAyB,EAAE;IAC9E,KAAK,MAAM,IAAI,IAAI,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;QACpC,IAAI,IAAI,CAAC,EAAE,KAAK,EAAE;YAAE,OAAO,IAAI,CAAA;IACjC,CAAC;IAED,OAAO,SAAS,CAAA;AAClB,CAAC,CAAA;AAED,2EAA2E;AAC3E,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,IAAe,EAAY,EAAE,CAAC,CAAC,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,CAAC,QAAQ,CAAC,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,IAAI,KAAK,CAAC,CAAA;QAEpE,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,IAAI,MAAM,IAAI,MAAM,EAAE,CAAC;YAC1C,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAA;QAC/D,CAAC;QAED,IAAI,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;YAAE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACtD,CAAC;IAED,KAAK,MAAM,CAAC,EAAE,EAAE,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC;QAC7B,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC;YAAE,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACrC,CAAC;IAED,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAA;AAC3C,CAAC,CAAA"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Re-exported so `blocks.ts` can compose primitives without a circular import. */
|
|
2
|
+
export { array, type ObjectSchema, object } from './composites.js';
|
|
3
|
+
export { boolean, enumOf, json, number, string } from './primitives.js';
|
|
4
|
+
//# sourceMappingURL=composites-bridge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"composites-bridge.d.ts","sourceRoot":"","sources":["../src/composites-bridge.ts"],"names":[],"mappings":"AAAA,mFAAmF;AACnF,OAAO,EAAE,KAAK,EAAE,KAAK,YAAY,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAClE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"composites-bridge.js","sourceRoot":"","sources":["../src/composites-bridge.ts"],"names":[],"mappings":"AAAA,mFAAmF;AACnF,OAAO,EAAE,KAAK,EAAqB,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAClE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Infer, type InferShape, type OptionalSchema, type Schema, type Shape } from './types.js';
|
|
2
|
+
export type ObjectSchema<S extends Shape> = Schema<InferShape<S>> & {
|
|
3
|
+
readonly shape: S;
|
|
4
|
+
describe(text: string): ObjectSchema<S>;
|
|
5
|
+
optional(): OptionalSchema<InferShape<S>>;
|
|
6
|
+
nullable(): Schema<InferShape<S> | null>;
|
|
7
|
+
};
|
|
8
|
+
/** `object({ email: string(), age: number().optional() })` */
|
|
9
|
+
export declare const object: <S extends Shape>(shape: S) => ObjectSchema<S>;
|
|
10
|
+
export type ArraySchema<E extends Schema> = Schema<Infer<E>[]> & {
|
|
11
|
+
readonly element: E;
|
|
12
|
+
min(length: number): ArraySchema<E>;
|
|
13
|
+
max(length: number): ArraySchema<E>;
|
|
14
|
+
describe(text: string): ArraySchema<E>;
|
|
15
|
+
optional(): OptionalSchema<Infer<E>[]>;
|
|
16
|
+
nullable(): Schema<Infer<E>[] | null>;
|
|
17
|
+
};
|
|
18
|
+
export declare const array: <E extends Schema>(element: E) => ArraySchema<E>;
|
|
19
|
+
//# sourceMappingURL=composites.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"composites.d.ts","sourceRoot":"","sources":["../src/composites.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,KAAK,EACV,KAAK,UAAU,EAIf,KAAK,cAAc,EAGnB,KAAK,MAAM,EACX,KAAK,KA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|
2
|
+
const optionalOf = (inner) => ({
|
|
3
|
+
...inner,
|
|
4
|
+
isOptional: true,
|
|
5
|
+
parse: (value) => value === undefined ? ok(undefined) : inner.parse(value),
|
|
6
|
+
});
|
|
7
|
+
const nullableOf = (inner) => ({
|
|
8
|
+
...inner,
|
|
9
|
+
isNullable: true,
|
|
10
|
+
parse: (value) => value === null ? ok(null) : inner.parse(value),
|
|
11
|
+
toJsonSchema: () => ({ ...inner.toJsonSchema(), nullable: true }),
|
|
12
|
+
});
|
|
13
|
+
const buildObject = (shape, description) => {
|
|
14
|
+
const self = {
|
|
15
|
+
kind: 'object',
|
|
16
|
+
isOptional: false,
|
|
17
|
+
isNullable: false,
|
|
18
|
+
description,
|
|
19
|
+
shape,
|
|
20
|
+
parse: (value) => {
|
|
21
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
22
|
+
return fail('type', 'Expected an object');
|
|
23
|
+
}
|
|
24
|
+
const source = value;
|
|
25
|
+
const issues = [];
|
|
26
|
+
const parsed = {};
|
|
27
|
+
for (const [key, field] of Object.entries(shape)) {
|
|
28
|
+
// `hasOwn` rather than plain indexing, and rather than `in`: a shape's keys are
|
|
29
|
+
// caller-chosen — a dynamic collection names its fields in stored JSON
|
|
30
|
+
// (SPEC.md §37, §86) — and `constructor`, `toString`, `valueOf` and
|
|
31
|
+
// `hasOwnProperty` are all legal field names that `Object.prototype` answers on
|
|
32
|
+
// a value that has never been given them. Read without this, a group field
|
|
33
|
+
// called `constructor` parsed a function for a key nobody sent, and the entry
|
|
34
|
+
// could never be saved.
|
|
35
|
+
const present = Object.hasOwn(source, key);
|
|
36
|
+
const result = field.parse(present ? source[key] : undefined);
|
|
37
|
+
if (!result.ok) {
|
|
38
|
+
issues.push(...nest(key, result.issues));
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
// An absent optional key stays absent rather than becoming `undefined`,
|
|
42
|
+
// which `exactOptionalPropertyTypes` treats as two different things.
|
|
43
|
+
if (result.value === undefined && !present)
|
|
44
|
+
continue;
|
|
45
|
+
parsed[key] = result.value;
|
|
46
|
+
}
|
|
47
|
+
// Keys that are not part of the shape are dropped, never carried through.
|
|
48
|
+
// Passing them on is how mass assignment happens (SPEC.md §85).
|
|
49
|
+
return issues.length > 0 ? failWith(issues) : ok(parsed);
|
|
50
|
+
},
|
|
51
|
+
toJsonSchema: () => {
|
|
52
|
+
const properties = {};
|
|
53
|
+
const required = [];
|
|
54
|
+
for (const [key, field] of Object.entries(shape)) {
|
|
55
|
+
properties[key] = field.toJsonSchema();
|
|
56
|
+
if (!field.isOptional)
|
|
57
|
+
required.push(key);
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
type: 'object',
|
|
61
|
+
properties,
|
|
62
|
+
...(required.length > 0 ? { required } : {}),
|
|
63
|
+
additionalProperties: false,
|
|
64
|
+
...(description === undefined ? {} : { description }),
|
|
65
|
+
};
|
|
66
|
+
},
|
|
67
|
+
describe: (text) => buildObject(shape, text),
|
|
68
|
+
optional: () => optionalOf(self),
|
|
69
|
+
nullable: () => nullableOf(self),
|
|
70
|
+
};
|
|
71
|
+
return self;
|
|
72
|
+
};
|
|
73
|
+
/** `object({ email: string(), age: number().optional() })` */
|
|
74
|
+
export const object = (shape) => buildObject(shape, undefined);
|
|
75
|
+
const buildArray = (element, state) => {
|
|
76
|
+
const self = {
|
|
77
|
+
kind: 'array',
|
|
78
|
+
isOptional: false,
|
|
79
|
+
isNullable: false,
|
|
80
|
+
description: state.description,
|
|
81
|
+
element,
|
|
82
|
+
parse: (value) => {
|
|
83
|
+
if (!Array.isArray(value))
|
|
84
|
+
return fail('type', 'Expected an array');
|
|
85
|
+
if (state.minLength !== undefined && value.length < state.minLength) {
|
|
86
|
+
return fail('min', `Must contain at least ${state.minLength} items`);
|
|
87
|
+
}
|
|
88
|
+
if (state.maxLength !== undefined && value.length > state.maxLength) {
|
|
89
|
+
return fail('max', `Must contain at most ${state.maxLength} items`);
|
|
90
|
+
}
|
|
91
|
+
const issues = [];
|
|
92
|
+
const parsed = [];
|
|
93
|
+
for (const [index, item] of value.entries()) {
|
|
94
|
+
const result = element.parse(item);
|
|
95
|
+
if (result.ok) {
|
|
96
|
+
parsed.push(result.value);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
issues.push(...nest(index, result.issues));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return issues.length > 0 ? failWith(issues) : ok(parsed);
|
|
103
|
+
},
|
|
104
|
+
toJsonSchema: () => ({
|
|
105
|
+
type: 'array',
|
|
106
|
+
items: element.toJsonSchema(),
|
|
107
|
+
...(state.minLength === undefined ? {} : { minItems: state.minLength }),
|
|
108
|
+
...(state.maxLength === undefined ? {} : { maxItems: state.maxLength }),
|
|
109
|
+
...(state.description === undefined ? {} : { description: state.description }),
|
|
110
|
+
}),
|
|
111
|
+
min: (length) => buildArray(element, { ...state, minLength: length }),
|
|
112
|
+
max: (length) => buildArray(element, { ...state, maxLength: length }),
|
|
113
|
+
describe: (text) => buildArray(element, { ...state, description: text }),
|
|
114
|
+
optional: () => optionalOf(self),
|
|
115
|
+
nullable: () => nullableOf(self),
|
|
116
|
+
};
|
|
117
|
+
return self;
|
|
118
|
+
};
|
|
119
|
+
export const array = (element) => buildArray(element, { minLength: undefined, maxLength: undefined, description: undefined });
|
|
120
|
+
//# sourceMappingURL=composites.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"composites.js","sourceRoot":"","sources":["../src/composites.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,IAAI,EACJ,QAAQ,EAKR,IAAI,EAEJ,EAAE,GAIH,MAAM,YAAY,CAAA;AAEnB,MAAM,UAAU,GAAG,CAAI,KAAgB,EAAqB,EAAE,CAAC,CAAC;IAC9D,GAAG,KAAK;IACR,UAAU,EAAE,IAAI;IAChB,KAAK,EAAE,CAAC,KAAc,EAA8B,EAAE,CACpD,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,SA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