@keemakr/ui-sdk 1.7.0 → 1.8.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/README.md +163 -1
- package/catalog.json +7 -19
- package/dist/build.js +48 -2
- package/dist/check-cli.d.ts +9 -0
- package/dist/check-cli.js +145 -0
- package/dist/check.d.ts +166 -0
- package/dist/check.js +408 -0
- package/dist/cli.js +5 -1
- package/dist/define.d.ts +7 -1
- package/dist/define.js +3 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.js +3 -2
- package/dist/media.d.ts +12 -0
- package/dist/media.js +19 -1
- package/dist/negotiate.d.ts +11 -2
- package/dist/negotiate.js +24 -1
- package/dist/node-limits.d.ts +3 -3
- package/dist/node-limits.js +2 -2
- package/dist/page.d.ts +307 -0
- package/dist/page.js +482 -0
- package/dist/reach.d.ts +66 -0
- package/dist/reach.js +151 -0
- package/dist/schema.d.ts +46 -0
- package/dist/schema.js +80 -8
- package/dist/stories.d.ts +63 -0
- package/dist/stories.js +297 -0
- package/dist/testkit.d.ts +13 -2
- package/dist/testkit.js +8 -2
- package/dist/ui-build.d.ts +46 -0
- package/dist/ui-build.js +61 -0
- package/dist/ui-nodes.d.ts +195 -0
- package/dist/ui-nodes.js +197 -0
- package/dist/ui.d.ts +61 -0
- package/dist/ui.js +22 -0
- package/package.json +22 -2
package/dist/stories.js
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
// Stories: one format for a sample page, and synthetic rows to fill it (WS28-S7).
|
|
2
|
+
//
|
|
3
|
+
// A story is a page as the SDK writes it (a screen tree) plus the declarations of the datasets it
|
|
4
|
+
// places. A `*.stories.ts` file default-exports `defineStories({ title, stories })`. The builder's
|
|
5
|
+
// preview, the catalogue and a module's own tests read the same file, so a page is tried against
|
|
6
|
+
// an empty dataset, a few rows and a full one (and long text, and an answer the platform refuses)
|
|
7
|
+
// before anyone sees it with real data.
|
|
8
|
+
//
|
|
9
|
+
// export default defineStories({
|
|
10
|
+
// title: 'Leads',
|
|
11
|
+
// stories: [{ name: 'Leads as a table', tree, datasets: [leads] }],
|
|
12
|
+
// });
|
|
13
|
+
//
|
|
14
|
+
// The rows are synthetic and deterministic: the same dataset, count, variant and seed give the
|
|
15
|
+
// same rows on every run, with no clock and no randomness from outside. They never contain
|
|
16
|
+
// anything real: names come from a fixed list of made-up words.
|
|
17
|
+
//
|
|
18
|
+
// `sampleAnswer` answers one dataset query from those rows the way a module would (filters,
|
|
19
|
+
// search, sort, keys, a page and its cursor), so core's own dataset expansion can draw a story
|
|
20
|
+
// exactly as it draws a live screen, with the sample answer in place of the module's.
|
|
21
|
+
//
|
|
22
|
+
// Pure: no network, no core, no clock.
|
|
23
|
+
import { DATASET_LIMITS, datasetDeclarationSchema, datasetManifestSchema, datasetQueryRequestSchema, } from './dataset.js';
|
|
24
|
+
import { screenTreeSchema } from './schema.js';
|
|
25
|
+
/**
|
|
26
|
+
* How the sample rows look.
|
|
27
|
+
* - `plain`: ordinary rows, with a blank optional value now and then.
|
|
28
|
+
* - `long_text`: every text value as long as a dataset allows, and as many tags as it allows.
|
|
29
|
+
* - `error`: rows the platform refuses (no key), so a view shows what a failed read looks like.
|
|
30
|
+
*/
|
|
31
|
+
export const SAMPLE_VARIANTS = ['plain', 'long_text', 'error'];
|
|
32
|
+
export class StoryError extends Error {
|
|
33
|
+
story;
|
|
34
|
+
constructor(story, message) {
|
|
35
|
+
super(`Story "${story}": ${message}`);
|
|
36
|
+
this.story = story;
|
|
37
|
+
this.name = 'StoryError';
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** The dataset names a tree places, in order, each once. */
|
|
41
|
+
export function datasetsPlaced(tree) {
|
|
42
|
+
const names = new Set();
|
|
43
|
+
const walk = (nodes) => {
|
|
44
|
+
for (const node of nodes) {
|
|
45
|
+
if (node.type === 'dataset')
|
|
46
|
+
names.add(node.dataset);
|
|
47
|
+
else if (node.type === 'section')
|
|
48
|
+
walk(node.children);
|
|
49
|
+
else if (node.type === 'tabs')
|
|
50
|
+
for (const tab of node.tabs)
|
|
51
|
+
walk(tab.children);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
walk(tree.children);
|
|
55
|
+
return [...names];
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Check a stories file: every tree passes the screen schema, every dataset declaration passes the
|
|
59
|
+
* dataset schema and is declared once, every dataset a tree places is declared, and no two stories
|
|
60
|
+
* share a name.
|
|
61
|
+
* Throws a `StoryError` naming the story.
|
|
62
|
+
*/
|
|
63
|
+
export function defineStories(file) {
|
|
64
|
+
const names = new Set();
|
|
65
|
+
const stories = file.stories.map((story) => {
|
|
66
|
+
if (names.has(story.name))
|
|
67
|
+
throw new StoryError(story.name, 'the name is used twice');
|
|
68
|
+
names.add(story.name);
|
|
69
|
+
const tree = screenTreeSchema.safeParse(story.tree);
|
|
70
|
+
if (!tree.success) {
|
|
71
|
+
const issue = tree.error.issues[0];
|
|
72
|
+
throw new StoryError(story.name, `the tree does not pass the schema at ${issue?.path.join('.') || '(root)'}: ${issue?.message}`);
|
|
73
|
+
}
|
|
74
|
+
const datasets = (story.datasets ?? []).map((input) => {
|
|
75
|
+
const parsed = datasetDeclarationSchema.safeParse(input);
|
|
76
|
+
if (!parsed.success) {
|
|
77
|
+
throw new StoryError(story.name, `dataset "${String(input.name)}" does not pass the schema: ${parsed.error.issues[0]?.message}`);
|
|
78
|
+
}
|
|
79
|
+
return parsed.data;
|
|
80
|
+
});
|
|
81
|
+
// One name, one declaration, as in a module's manifest: rows made from one copy and checked
|
|
82
|
+
// against another would show an error the author cannot explain.
|
|
83
|
+
const manifest = datasetManifestSchema.safeParse(datasets);
|
|
84
|
+
if (!manifest.success)
|
|
85
|
+
throw new StoryError(story.name, manifest.error.issues[0].message);
|
|
86
|
+
const declared = new Set(datasets.map((dataset) => dataset.name));
|
|
87
|
+
for (const name of datasetsPlaced(tree.data)) {
|
|
88
|
+
if (!declared.has(name)) {
|
|
89
|
+
throw new StoryError(story.name, `the tree places dataset "${name}", which is not declared`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
name: story.name,
|
|
94
|
+
...(story.description ? { description: story.description } : {}),
|
|
95
|
+
tree: tree.data,
|
|
96
|
+
datasets,
|
|
97
|
+
};
|
|
98
|
+
});
|
|
99
|
+
return { title: file.title, stories };
|
|
100
|
+
}
|
|
101
|
+
// ── Sample rows ────────────────────────────────────────────────────────────────────────────────
|
|
102
|
+
/** Made-up words for titles and text: no real names, companies or places. */
|
|
103
|
+
const WORDS = [
|
|
104
|
+
'amber',
|
|
105
|
+
'birch',
|
|
106
|
+
'cobalt',
|
|
107
|
+
'delta',
|
|
108
|
+
'ember',
|
|
109
|
+
'fjord',
|
|
110
|
+
'granite',
|
|
111
|
+
'harbor',
|
|
112
|
+
'indigo',
|
|
113
|
+
'juniper',
|
|
114
|
+
'kestrel',
|
|
115
|
+
'lumen',
|
|
116
|
+
'meadow',
|
|
117
|
+
'nimbus',
|
|
118
|
+
'onyx',
|
|
119
|
+
'pebble',
|
|
120
|
+
'quartz',
|
|
121
|
+
'ridge',
|
|
122
|
+
'sable',
|
|
123
|
+
'tundra',
|
|
124
|
+
];
|
|
125
|
+
/** Made-up people for owner fields. */
|
|
126
|
+
const PEOPLE = [
|
|
127
|
+
'Sample Person A',
|
|
128
|
+
'Sample Person B',
|
|
129
|
+
'Sample Person C',
|
|
130
|
+
'Sample Person D',
|
|
131
|
+
'Sample Person E',
|
|
132
|
+
];
|
|
133
|
+
/** The first sample time: a fixed instant, so rows never depend on the clock. */
|
|
134
|
+
const SAMPLE_EPOCH = Date.UTC(2026, 0, 5, 9, 0, 0);
|
|
135
|
+
const HOUR = 3_600_000;
|
|
136
|
+
const DAY = 24 * HOUR;
|
|
137
|
+
/** In `plain`, every Nth row leaves its optional fields blank. */
|
|
138
|
+
const BLANK_EVERY = 9;
|
|
139
|
+
/** The longest tag a row may hold (dataset.ts, datasetCellSchema). */
|
|
140
|
+
const TAG_MAX = 40;
|
|
141
|
+
/** FNV-1a: a stable 32-bit seed from a string. */
|
|
142
|
+
function hash(text) {
|
|
143
|
+
let h = 0x811c9dc5;
|
|
144
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
145
|
+
h ^= text.charCodeAt(i);
|
|
146
|
+
h = Math.imul(h, 0x01000193);
|
|
147
|
+
}
|
|
148
|
+
return h >>> 0;
|
|
149
|
+
}
|
|
150
|
+
/** mulberry32: a small seeded generator, the same sequence on every platform. */
|
|
151
|
+
function generator(seed) {
|
|
152
|
+
let state = seed >>> 0;
|
|
153
|
+
return () => {
|
|
154
|
+
state = (state + 0x6d2b79f5) >>> 0;
|
|
155
|
+
let t = state;
|
|
156
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
157
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
158
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4_294_967_296;
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
/** Words, repeated, cut to exactly `length` characters. */
|
|
162
|
+
function longText(start, length) {
|
|
163
|
+
let out = '';
|
|
164
|
+
for (let i = start; out.length < length; i += 1) {
|
|
165
|
+
out += (out ? ' ' : '') + WORDS[i % WORDS.length];
|
|
166
|
+
}
|
|
167
|
+
return out.slice(0, length).trimEnd().padEnd(length, '.');
|
|
168
|
+
}
|
|
169
|
+
function keyValue(dataset, field, index) {
|
|
170
|
+
return field.type === 'number' ? index + 1 : `${dataset.name}-${index + 1}`;
|
|
171
|
+
}
|
|
172
|
+
function cell(dataset, field, index, long, next) {
|
|
173
|
+
const pick = (list) => list[Math.floor(next() * list.length)];
|
|
174
|
+
switch (field.type) {
|
|
175
|
+
case 'text':
|
|
176
|
+
if (long)
|
|
177
|
+
return longText(index, DATASET_LIMITS.text);
|
|
178
|
+
if (field.role === 'owner')
|
|
179
|
+
return pick(PEOPLE);
|
|
180
|
+
if (field.role === 'title')
|
|
181
|
+
return `${capital(pick(WORDS))} ${pick(WORDS)} ${index + 1}`;
|
|
182
|
+
return `${capital(pick(WORDS))} ${pick(WORDS)}`;
|
|
183
|
+
case 'number':
|
|
184
|
+
// An amount reads as money, two places; any other number as a count.
|
|
185
|
+
return field.role === 'amount'
|
|
186
|
+
? Math.round(next() * 10_000_000) / 100
|
|
187
|
+
: Math.floor(next() * 1000);
|
|
188
|
+
case 'boolean':
|
|
189
|
+
return next() < 0.5;
|
|
190
|
+
case 'datetime':
|
|
191
|
+
return new Date(SAMPLE_EPOCH + index * 3 * HOUR + Math.floor(next() * 60) * 60_000)
|
|
192
|
+
.toISOString()
|
|
193
|
+
.replace(/\.\d{3}Z$/, 'Z');
|
|
194
|
+
case 'date':
|
|
195
|
+
return new Date(SAMPLE_EPOCH + index * DAY).toISOString().slice(0, 10);
|
|
196
|
+
case 'enum': {
|
|
197
|
+
// Walk the options in order, so a board or a chart has every column once there are enough rows.
|
|
198
|
+
const options = field.options ?? [];
|
|
199
|
+
return options.length > 0 ? options[index % options.length].value : null;
|
|
200
|
+
}
|
|
201
|
+
case 'url':
|
|
202
|
+
return `https://example.com/${dataset.name}/${index + 1}`;
|
|
203
|
+
case 'tags':
|
|
204
|
+
if (long) {
|
|
205
|
+
return Array.from({ length: DATASET_LIMITS.tags }, (_, i) => longText(index + i, TAG_MAX));
|
|
206
|
+
}
|
|
207
|
+
return Array.from({ length: Math.floor(next() * 4) }, () => pick(WORDS));
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const capital = (word) => word.charAt(0).toUpperCase() + word.slice(1);
|
|
211
|
+
/**
|
|
212
|
+
* `count` synthetic rows for a dataset, the same on every call with the same arguments. Every row
|
|
213
|
+
* has a unique key (1, 2, 3 … for a number key; `<dataset>-1` … for a text one) and a value of the
|
|
214
|
+
* declared type for every field. An enum field walks its options in order.
|
|
215
|
+
*
|
|
216
|
+
* `error` rows leave out the key, which the platform refuses (`datasetResponseIssues`): at least
|
|
217
|
+
* one row, so even an empty page shows the failure.
|
|
218
|
+
*/
|
|
219
|
+
export function sampleRows(dataset, count, variant = 'plain', seed = 0) {
|
|
220
|
+
if (!Number.isInteger(count) || count < 0) {
|
|
221
|
+
throw new RangeError(`sampleRows: count must be a whole number of rows, not ${count}`);
|
|
222
|
+
}
|
|
223
|
+
const declaration = datasetDeclarationSchema.parse(dataset);
|
|
224
|
+
const next = generator(hash(`${declaration.name}:${variant}:${seed}`));
|
|
225
|
+
const long = variant === 'long_text';
|
|
226
|
+
const total = variant === 'error' ? Math.max(count, 1) : count;
|
|
227
|
+
return Array.from({ length: total }, (_, index) => {
|
|
228
|
+
const blank = variant === 'plain' && index % BLANK_EVERY === BLANK_EVERY - 1;
|
|
229
|
+
const row = {};
|
|
230
|
+
for (const field of declaration.fields) {
|
|
231
|
+
if (field.name === declaration.key) {
|
|
232
|
+
if (variant !== 'error')
|
|
233
|
+
row[field.name] = keyValue(declaration, field, index);
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
// A title is never blank: every view needs one to name the row.
|
|
237
|
+
row[field.name] =
|
|
238
|
+
blank && field.role !== 'title' ? null : cell(declaration, field, index, long, next);
|
|
239
|
+
}
|
|
240
|
+
return row;
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
// ── Answering a query from sample rows ─────────────────────────────────────────────────────────
|
|
244
|
+
const text = (value) => value === null || value === undefined
|
|
245
|
+
? ''
|
|
246
|
+
: Array.isArray(value)
|
|
247
|
+
? value.join(' ')
|
|
248
|
+
: String(value);
|
|
249
|
+
/** Ascending or descending by one field; a blank value sorts last either way. */
|
|
250
|
+
function compare(a, b, sign) {
|
|
251
|
+
const blankA = a === null || a === undefined;
|
|
252
|
+
const blankB = b === null || b === undefined;
|
|
253
|
+
if (blankA || blankB)
|
|
254
|
+
return blankA === blankB ? 0 : blankA ? 1 : -1;
|
|
255
|
+
if (typeof a === 'number' && typeof b === 'number')
|
|
256
|
+
return sign * (a - b);
|
|
257
|
+
return sign * text(a).localeCompare(text(b));
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Answer one dataset query from `rows`, as a module would: the asked keys only, then the equality
|
|
261
|
+
* filters, the search over text fields, the sort, and one page of `limit` rows from the cursor (an
|
|
262
|
+
* offset), with `total` and, while more rows follow, `nextCursor`. It does not check the rows: core checks every
|
|
263
|
+
* answer against the declaration (`datasetResponseIssues`), so `error` rows fail there.
|
|
264
|
+
*/
|
|
265
|
+
export function sampleAnswer(dataset, rows, request = {}) {
|
|
266
|
+
const declaration = datasetDeclarationSchema.parse(dataset);
|
|
267
|
+
const query = datasetQueryRequestSchema.parse(request);
|
|
268
|
+
let matched = [...rows];
|
|
269
|
+
if (query.keys) {
|
|
270
|
+
const keys = new Set(query.keys);
|
|
271
|
+
matched = matched.filter((row) => {
|
|
272
|
+
const key = row[declaration.key];
|
|
273
|
+
return key === null || key === undefined || keys.has(String(key));
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
for (const [name, value] of Object.entries(query.filters)) {
|
|
277
|
+
matched = matched.filter((row) => text(row[name]) === value);
|
|
278
|
+
}
|
|
279
|
+
if (query.search) {
|
|
280
|
+
const needle = query.search.toLowerCase();
|
|
281
|
+
const searched = declaration.fields.filter((field) => field.type === 'text');
|
|
282
|
+
matched = matched.filter((row) => searched.some((field) => text(row[field.name]).toLowerCase().includes(needle)));
|
|
283
|
+
}
|
|
284
|
+
const sort = query.sort ?? declaration.defaultSort;
|
|
285
|
+
if (sort) {
|
|
286
|
+
const sign = sort.direction === 'desc' ? -1 : 1;
|
|
287
|
+
matched.sort((a, b) => compare(a[sort.field], b[sort.field], sign));
|
|
288
|
+
}
|
|
289
|
+
const offset = query.cursor ? Math.max(0, Number.parseInt(query.cursor, 10) || 0) : 0;
|
|
290
|
+
const page = matched.slice(offset, offset + query.limit);
|
|
291
|
+
const end = offset + page.length;
|
|
292
|
+
return {
|
|
293
|
+
rows: page,
|
|
294
|
+
total: matched.length,
|
|
295
|
+
...(end < matched.length ? { nextCursor: String(end) } : {}),
|
|
296
|
+
};
|
|
297
|
+
}
|
package/dist/testkit.d.ts
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
|
-
import { type ClientTarget, type Fallback, type NodeSet } from './negotiate.js';
|
|
1
|
+
import { type ClientTarget, type Fallback, type NodeSet, type UiSchemaClaim } from './negotiate.js';
|
|
2
2
|
import { type ScreenTree, type ScreenTreeInput } from './schema.js';
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* A client, at every one of its releases, or at a named version. A `claim` is what a channel build
|
|
5
|
+
* says about the schema it parses (`web/0.2.0+abc1234.ui-current`): such a build gets what core
|
|
6
|
+
* sends it where core honours claims (dev, stage, a personal tier), the web app "current" beside
|
|
7
|
+
* "prod". Production honours none, so leave it out for what production sends.
|
|
8
|
+
*/
|
|
4
9
|
export type ClientSpec = ClientTarget | {
|
|
5
10
|
client: ClientTarget;
|
|
6
11
|
version: string;
|
|
12
|
+
claim?: UiSchemaClaim;
|
|
7
13
|
};
|
|
8
14
|
/** The clients that render module screens. Desktop, the extension and the CLI do not. */
|
|
9
15
|
export declare const SCREEN_CLIENTS: readonly ClientTarget[];
|
|
@@ -22,6 +28,11 @@ export interface ClientRender {
|
|
|
22
28
|
* (each released build parses and draws its own set), so `['mobile']` covers the
|
|
23
29
|
* oldest phone still in use as well as the newest. Throws the schema's error on
|
|
24
30
|
* an invalid tree.
|
|
31
|
+
*
|
|
32
|
+
* Each client's node set comes from `nodeSetForClaim`, the call core makes for
|
|
33
|
+
* every `X-Kee-Client` (src/lib/modules/ui-negotiation.ts), and `negotiateTree`
|
|
34
|
+
* both swaps the nodes it cannot draw and conforms the rest to the schema it
|
|
35
|
+
* parses: the tree here is the tree core sends that client.
|
|
25
36
|
*/
|
|
26
37
|
export declare function renderForClients(tree: ScreenTreeInput, clients?: readonly ClientSpec[]): ClientRender[];
|
|
27
38
|
export declare class ScreenNotDrawableError extends Error {
|
package/dist/testkit.js
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
//
|
|
14
14
|
// Pure: no network, no core, no clock. The same pattern as the agent-sdk
|
|
15
15
|
// `channel/testkit`: test against what production does, not a copy of it.
|
|
16
|
-
import { negotiateTree, releasesOf,
|
|
16
|
+
import { negotiateTree, releasesOf, nodeSetForClaim, } from './negotiate.js';
|
|
17
17
|
import { screenTreeSchema } from './schema.js';
|
|
18
18
|
/** The clients that render module screens. Desktop, the extension and the CLI do not. */
|
|
19
19
|
export const SCREEN_CLIENTS = ['web', 'mobile'];
|
|
@@ -23,14 +23,20 @@ export const SCREEN_CLIENTS = ['web', 'mobile'];
|
|
|
23
23
|
* (each released build parses and draws its own set), so `['mobile']` covers the
|
|
24
24
|
* oldest phone still in use as well as the newest. Throws the schema's error on
|
|
25
25
|
* an invalid tree.
|
|
26
|
+
*
|
|
27
|
+
* Each client's node set comes from `nodeSetForClaim`, the call core makes for
|
|
28
|
+
* every `X-Kee-Client` (src/lib/modules/ui-negotiation.ts), and `negotiateTree`
|
|
29
|
+
* both swaps the nodes it cannot draw and conforms the rest to the schema it
|
|
30
|
+
* parses: the tree here is the tree core sends that client.
|
|
26
31
|
*/
|
|
27
32
|
export function renderForClients(tree, clients = SCREEN_CLIENTS) {
|
|
28
33
|
const parsed = screenTreeSchema.parse(tree);
|
|
29
34
|
return clients.flatMap((spec) => {
|
|
30
35
|
const client = typeof spec === 'string' ? spec : spec.client;
|
|
36
|
+
const claim = typeof spec === 'string' ? null : (spec.claim ?? null);
|
|
31
37
|
const versions = typeof spec === 'string' ? releasesOf(spec).map((row) => row.since) : [spec.version];
|
|
32
38
|
return versions.map((version) => {
|
|
33
|
-
const nodeSet =
|
|
39
|
+
const nodeSet = nodeSetForClaim(client, version, claim);
|
|
34
40
|
const { tree: negotiated, fallbacks } = negotiateTree(parsed, nodeSet);
|
|
35
41
|
return { client, version, nodeSet, tree: negotiated, fallbacks };
|
|
36
42
|
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { type ScreenRootNode, type ScreenTree, type ScreenTreeInput } from './schema.js';
|
|
2
|
+
/** A node of a screen's `children`, as an author writes it (defaults may be left out). */
|
|
3
|
+
export type UiNodeInput = ScreenTreeInput['children'][number];
|
|
4
|
+
/** Every node type a screen may hold. */
|
|
5
|
+
export type UiNodeType = ScreenRootNode['type'];
|
|
6
|
+
type WithoutType<T> = T extends unknown ? Omit<T, 'type'> : never;
|
|
7
|
+
/** The fields a builder takes for one node type: the node as written, without `type`. */
|
|
8
|
+
export type UiNodeProps<T extends UiNodeType> = WithoutType<Extract<UiNodeInput, {
|
|
9
|
+
type: T;
|
|
10
|
+
}>>;
|
|
11
|
+
/** What a builder returns for one node type: the node with its defaults filled in. */
|
|
12
|
+
export type UiNode<T extends UiNodeType> = Extract<ScreenRootNode, {
|
|
13
|
+
type: T;
|
|
14
|
+
}>;
|
|
15
|
+
/** The fields `screen()` takes: a screen tree without `version`, which the builder sets. */
|
|
16
|
+
export type UiScreenProps = Omit<ScreenTreeInput, 'version'>;
|
|
17
|
+
/** One value a builder refused. */
|
|
18
|
+
export interface UiBuildIssue {
|
|
19
|
+
/** Where, from the node (or the screen) the builder makes: `rows[3].cells.name.value`. */
|
|
20
|
+
path: string;
|
|
21
|
+
message: string;
|
|
22
|
+
/**
|
|
23
|
+
* For a value past a size cap: the cap (characters, items or keys), as `NODE_LIMITS` holds it.
|
|
24
|
+
* A number past its range (`section.columns`) is not a cap, and has none.
|
|
25
|
+
*/
|
|
26
|
+
limit?: number;
|
|
27
|
+
}
|
|
28
|
+
/** Thrown by a builder for a value the schema refuses. The first issue is in the message. */
|
|
29
|
+
export declare class UiBuildError extends Error {
|
|
30
|
+
/** `ui.table`, `ui.screen` … */
|
|
31
|
+
readonly builder: string;
|
|
32
|
+
/** The first refused value's path (`''` when the node itself is refused). */
|
|
33
|
+
readonly path: string;
|
|
34
|
+
readonly issues: UiBuildIssue[];
|
|
35
|
+
constructor(builder: string, issues: UiBuildIssue[]);
|
|
36
|
+
}
|
|
37
|
+
/** `['rows', 3, 'cells', 'first name']` as `rows[3].cells["first name"]`. */
|
|
38
|
+
export declare function formatUiPath(path: readonly PropertyKey[]): string;
|
|
39
|
+
/** Builds one node. The generated builders in ui-nodes.ts call this with their own type. */
|
|
40
|
+
export declare function buildNode<T extends UiNodeType>(type: T, props: UiNodeProps<T>): UiNode<T>;
|
|
41
|
+
/**
|
|
42
|
+
* A whole screen: sets `version` to `UI_SCHEMA_VERSION` and parses the tree with
|
|
43
|
+
* `screenTreeSchema`, children included, exactly as core will.
|
|
44
|
+
*/
|
|
45
|
+
export declare function buildScreen(props: UiScreenProps): ScreenTree;
|
|
46
|
+
export {};
|
package/dist/ui-build.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { nodeSchema, screenTreeSchema, UI_SCHEMA_VERSION, } from './schema.js';
|
|
2
|
+
/** Thrown by a builder for a value the schema refuses. The first issue is in the message. */
|
|
3
|
+
export class UiBuildError extends Error {
|
|
4
|
+
/** `ui.table`, `ui.screen` … */
|
|
5
|
+
builder;
|
|
6
|
+
/** The first refused value's path (`''` when the node itself is refused). */
|
|
7
|
+
path;
|
|
8
|
+
issues;
|
|
9
|
+
constructor(builder, issues) {
|
|
10
|
+
const first = issues[0];
|
|
11
|
+
const where = first.path === '' ? '' : ` ${first.path}`;
|
|
12
|
+
const more = issues.length > 1 ? ` (and ${issues.length - 1} more)` : '';
|
|
13
|
+
super(`${builder}:${where} ${first.message}${more}`);
|
|
14
|
+
this.name = 'UiBuildError';
|
|
15
|
+
this.builder = builder;
|
|
16
|
+
this.path = first.path;
|
|
17
|
+
this.issues = issues;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
21
|
+
/** `['rows', 3, 'cells', 'first name']` as `rows[3].cells["first name"]`. */
|
|
22
|
+
export function formatUiPath(path) {
|
|
23
|
+
let out = '';
|
|
24
|
+
for (const key of path) {
|
|
25
|
+
if (typeof key === 'number')
|
|
26
|
+
out += `[${key}]`;
|
|
27
|
+
else if (typeof key === 'string' && IDENTIFIER.test(key))
|
|
28
|
+
out += out === '' ? key : `.${key}`;
|
|
29
|
+
else
|
|
30
|
+
out += `[${JSON.stringify(String(key))}]`;
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
/** The kinds of value whose `too_big` is a size cap (what `NODE_LIMITS` lists), not a range. */
|
|
35
|
+
const SIZED = new Set(['string', 'array', 'set', 'map', 'record']);
|
|
36
|
+
function issuesOf(error) {
|
|
37
|
+
return error.issues.map((issue) => {
|
|
38
|
+
const out = { path: formatUiPath(issue.path), message: issue.message };
|
|
39
|
+
if (issue.code === 'too_big' && SIZED.has(issue.origin) && typeof issue.maximum === 'number') {
|
|
40
|
+
out.limit = issue.maximum;
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/** Builds one node. The generated builders in ui-nodes.ts call this with their own type. */
|
|
46
|
+
export function buildNode(type, props) {
|
|
47
|
+
const parsed = nodeSchema.safeParse({ ...props, type });
|
|
48
|
+
if (!parsed.success)
|
|
49
|
+
throw new UiBuildError(`ui.${type}`, issuesOf(parsed.error));
|
|
50
|
+
return parsed.data;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* A whole screen: sets `version` to `UI_SCHEMA_VERSION` and parses the tree with
|
|
54
|
+
* `screenTreeSchema`, children included, exactly as core will.
|
|
55
|
+
*/
|
|
56
|
+
export function buildScreen(props) {
|
|
57
|
+
const parsed = screenTreeSchema.safeParse({ ...props, version: UI_SCHEMA_VERSION });
|
|
58
|
+
if (!parsed.success)
|
|
59
|
+
throw new UiBuildError('ui.screen', issuesOf(parsed.error));
|
|
60
|
+
return parsed.data;
|
|
61
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { type UiNode, type UiNodeProps } from './ui-build.js';
|
|
2
|
+
/** The catalog these builders were generated from. A test holds it to a fresh build. */
|
|
3
|
+
export declare const UI_NODES_CATALOG_ID = "kee/1+c8e17129b699";
|
|
4
|
+
/**
|
|
5
|
+
* One builder per screen node. Each takes the node's fields without `type`, applies the
|
|
6
|
+
* schema's defaults, and throws a `UiBuildError` naming the field path for a value the schema
|
|
7
|
+
* refuses, a value past its cap among them.
|
|
8
|
+
*/
|
|
9
|
+
export declare const nodeBuilders: {
|
|
10
|
+
/**
|
|
11
|
+
* A `heading` node. Since ui-sdk 1.1.0. Kee may draw it in a chat reply.
|
|
12
|
+
* Caps: `text` 120. Every cap is in `NODE_LIMITS.heading`.
|
|
13
|
+
*/
|
|
14
|
+
heading: (props: UiNodeProps<"heading">) => UiNode<"heading">;
|
|
15
|
+
/**
|
|
16
|
+
* A `text` node. Since ui-sdk 1.1.0. Kee may draw it in a chat reply.
|
|
17
|
+
* Caps: `value` 5000. Every cap is in `NODE_LIMITS.text`.
|
|
18
|
+
*/
|
|
19
|
+
text: (props: UiNodeProps<"text">) => UiNode<"text">;
|
|
20
|
+
/**
|
|
21
|
+
* A `markdown` node. Since ui-sdk 1.1.0.
|
|
22
|
+
* Caps: `value` 20000. Every cap is in `NODE_LIMITS.markdown`.
|
|
23
|
+
*/
|
|
24
|
+
markdown: (props: UiNodeProps<"markdown">) => UiNode<"markdown">;
|
|
25
|
+
/**
|
|
26
|
+
* A `stat` node. Since ui-sdk 1.1.0. Kee may draw it in a chat reply.
|
|
27
|
+
* Caps: `hint` 120, `label` 80, `value` 40. Every cap is in `NODE_LIMITS.stat`.
|
|
28
|
+
*/
|
|
29
|
+
stat: (props: UiNodeProps<"stat">) => UiNode<"stat">;
|
|
30
|
+
/**
|
|
31
|
+
* A `button` node. Since ui-sdk 1.1.0.
|
|
32
|
+
* Caps: `disabledReason` 200, `hint` 200, `label` 80. Every cap is in `NODE_LIMITS.button`.
|
|
33
|
+
*/
|
|
34
|
+
button: (props: UiNodeProps<"button">) => UiNode<"button">;
|
|
35
|
+
/**
|
|
36
|
+
* A `form` node. Since ui-sdk 1.1.0.
|
|
37
|
+
* Caps: `fields` 30, `key` 64, `secondary` 3. Every cap is in `NODE_LIMITS.form`.
|
|
38
|
+
*/
|
|
39
|
+
form: (props: UiNodeProps<"form">) => UiNode<"form">;
|
|
40
|
+
/**
|
|
41
|
+
* A `table` node. Since ui-sdk 1.1.0. Holds child nodes. Kee may draw it in a chat reply.
|
|
42
|
+
* Caps: `columns` 12, `empty` 200, `rows` 500. Every cap is in `NODE_LIMITS.table`.
|
|
43
|
+
*/
|
|
44
|
+
table: (props: UiNodeProps<"table">) => UiNode<"table">;
|
|
45
|
+
/**
|
|
46
|
+
* A `timeline` node. Since ui-sdk 1.1.0. Kee may draw it in a chat reply.
|
|
47
|
+
* Caps: `items` 200. Every cap is in `NODE_LIMITS.timeline`.
|
|
48
|
+
*/
|
|
49
|
+
timeline: (props: UiNodeProps<"timeline">) => UiNode<"timeline">;
|
|
50
|
+
/**
|
|
51
|
+
* A `pending_request` node. Since ui-sdk 1.1.0.
|
|
52
|
+
* Caps: `options` 1, `requestId` 128. Every cap is in `NODE_LIMITS.pending_request`.
|
|
53
|
+
*/
|
|
54
|
+
pending_request: (props: UiNodeProps<"pending_request">) => UiNode<"pending_request">;
|
|
55
|
+
/**
|
|
56
|
+
* A `pending_requests` node. Since ui-sdk 1.1.0.
|
|
57
|
+
* No size caps.
|
|
58
|
+
*/
|
|
59
|
+
pending_requests: (props: UiNodeProps<"pending_requests">) => UiNode<"pending_requests">;
|
|
60
|
+
/**
|
|
61
|
+
* A `choice_cards` node. Since ui-sdk 1.1.0.
|
|
62
|
+
* Caps: `choices` 20. Every cap is in `NODE_LIMITS.choice_cards`.
|
|
63
|
+
*/
|
|
64
|
+
choice_cards: (props: UiNodeProps<"choice_cards">) => UiNode<"choice_cards">;
|
|
65
|
+
/**
|
|
66
|
+
* A `progress` node. Since ui-sdk 1.1.0.
|
|
67
|
+
* Caps: `steps` 100. Every cap is in `NODE_LIMITS.progress`.
|
|
68
|
+
*/
|
|
69
|
+
progress: (props: UiNodeProps<"progress">) => UiNode<"progress">;
|
|
70
|
+
/**
|
|
71
|
+
* A `chart` node. Since ui-sdk 1.1.0. Kee may draw it in a chat reply.
|
|
72
|
+
* Caps: `categories` 120, `series` 8, `title` 120, `valueSuffix` 12. Every cap is in `NODE_LIMITS.chart`.
|
|
73
|
+
*/
|
|
74
|
+
chart: (props: UiNodeProps<"chart">) => UiNode<"chart">;
|
|
75
|
+
/**
|
|
76
|
+
* A `pagination` node. Since ui-sdk 1.4.0.
|
|
77
|
+
* Caps: `param` 64. Every cap is in `NODE_LIMITS.pagination`.
|
|
78
|
+
*/
|
|
79
|
+
pagination: (props: UiNodeProps<"pagination">) => UiNode<"pagination">;
|
|
80
|
+
/**
|
|
81
|
+
* A `select` node. Since ui-sdk 1.4.0.
|
|
82
|
+
* Caps: `disabledReason` 200, `hint` 200, `label` 80, `name` 64, `options` 100, `value` 200, `values` 12. Every cap is in `NODE_LIMITS.select`.
|
|
83
|
+
*/
|
|
84
|
+
select: (props: UiNodeProps<"select">) => UiNode<"select">;
|
|
85
|
+
/**
|
|
86
|
+
* A `alert` node. Since ui-sdk 1.4.0. Kee may draw it in a chat reply.
|
|
87
|
+
* Caps: `text` 1000, `title` 120. Every cap is in `NODE_LIMITS.alert`.
|
|
88
|
+
*/
|
|
89
|
+
alert: (props: UiNodeProps<"alert">) => UiNode<"alert">;
|
|
90
|
+
/**
|
|
91
|
+
* A `details` node. Since ui-sdk 1.4.0. Kee may draw it in a chat reply.
|
|
92
|
+
* Caps: `items` 50, `title` 120. Every cap is in `NODE_LIMITS.details`.
|
|
93
|
+
*/
|
|
94
|
+
details: (props: UiNodeProps<"details">) => UiNode<"details">;
|
|
95
|
+
/**
|
|
96
|
+
* A `timestamp` node. Since ui-sdk 1.4.0.
|
|
97
|
+
* Caps: `label` 80. Every cap is in `NODE_LIMITS.timestamp`.
|
|
98
|
+
*/
|
|
99
|
+
timestamp: (props: UiNodeProps<"timestamp">) => UiNode<"timestamp">;
|
|
100
|
+
/**
|
|
101
|
+
* A `divider` node. Since ui-sdk 1.4.0. Kee may draw it in a chat reply.
|
|
102
|
+
* No size caps.
|
|
103
|
+
*/
|
|
104
|
+
divider: (props: UiNodeProps<"divider">) => UiNode<"divider">;
|
|
105
|
+
/**
|
|
106
|
+
* A `board` node. Since ui-sdk 1.4.0.
|
|
107
|
+
* Caps: `columns` 8, `empty` 80. Every cap is in `NODE_LIMITS.board`.
|
|
108
|
+
*/
|
|
109
|
+
board: (props: UiNodeProps<"board">) => UiNode<"board">;
|
|
110
|
+
/**
|
|
111
|
+
* A `link` node. Since ui-sdk 1.4.0. Kee may draw it in a chat reply.
|
|
112
|
+
* Caps: `copy` 2000, `hint` 120, `href` 2000, `label` 120. Every cap is in `NODE_LIMITS.link`.
|
|
113
|
+
*/
|
|
114
|
+
link: (props: UiNodeProps<"link">) => UiNode<"link">;
|
|
115
|
+
/**
|
|
116
|
+
* A `tags` node. Since ui-sdk 1.4.0. Kee may draw it in a chat reply.
|
|
117
|
+
* Caps: `items` 20, `label` 80. Every cap is in `NODE_LIMITS.tags`.
|
|
118
|
+
*/
|
|
119
|
+
tags: (props: UiNodeProps<"tags">) => UiNode<"tags">;
|
|
120
|
+
/**
|
|
121
|
+
* A `toggle` node. Since ui-sdk 1.4.0.
|
|
122
|
+
* Caps: `disabledReason` 200, `hint` 200, `label` 80, `name` 64. Every cap is in `NODE_LIMITS.toggle`.
|
|
123
|
+
*/
|
|
124
|
+
toggle: (props: UiNodeProps<"toggle">) => UiNode<"toggle">;
|
|
125
|
+
/**
|
|
126
|
+
* A `segmented` node. Since ui-sdk 1.4.0.
|
|
127
|
+
* Caps: `label` 80, `name` 64, `options` 6, `value` 200. Every cap is in `NODE_LIMITS.segmented`.
|
|
128
|
+
*/
|
|
129
|
+
segmented: (props: UiNodeProps<"segmented">) => UiNode<"segmented">;
|
|
130
|
+
/**
|
|
131
|
+
* A `progress_bar` node. Since ui-sdk 1.4.0. Kee may draw it in a chat reply.
|
|
132
|
+
* Caps: `label` 80, `valueLabel` 40. Every cap is in `NODE_LIMITS.progress_bar`.
|
|
133
|
+
*/
|
|
134
|
+
progress_bar: (props: UiNodeProps<"progress_bar">) => UiNode<"progress_bar">;
|
|
135
|
+
/**
|
|
136
|
+
* A `heatmap` node. Since the current release.
|
|
137
|
+
* Caps: `columns` 24, `rowHeader` 80, `rows` 50, `title` 120, `valueSuffix` 12. Every cap is in `NODE_LIMITS.heatmap`.
|
|
138
|
+
*/
|
|
139
|
+
heatmap: (props: UiNodeProps<"heatmap">) => UiNode<"heatmap">;
|
|
140
|
+
/**
|
|
141
|
+
* A `stacked_bar` node. Since the current release.
|
|
142
|
+
* Caps: `label` 80, `segments` 6, `valueLabel` 40. Every cap is in `NODE_LIMITS.stacked_bar`.
|
|
143
|
+
*/
|
|
144
|
+
stacked_bar: (props: UiNodeProps<"stacked_bar">) => UiNode<"stacked_bar">;
|
|
145
|
+
/**
|
|
146
|
+
* A `conversation` node. Since ui-sdk 1.4.0.
|
|
147
|
+
* Caps: `empty` 80, `messages` 500. Every cap is in `NODE_LIMITS.conversation`.
|
|
148
|
+
*/
|
|
149
|
+
conversation: (props: UiNodeProps<"conversation">) => UiNode<"conversation">;
|
|
150
|
+
/**
|
|
151
|
+
* A `composer` node. Since ui-sdk 1.4.0.
|
|
152
|
+
* Caps: `disabledReason` 200, `label` 80, `mode` 64, `modes` 4, `name` 64, `placeholder` 120, `value` 20000. Every cap is in `NODE_LIMITS.composer`.
|
|
153
|
+
*/
|
|
154
|
+
composer: (props: UiNodeProps<"composer">) => UiNode<"composer">;
|
|
155
|
+
/**
|
|
156
|
+
* A `calendar` node. Since ui-sdk 1.4.0.
|
|
157
|
+
* Caps: `empty` 80, `events` 500, `resources` 12, `timeZone` 64, `views` 3. Every cap is in `NODE_LIMITS.calendar`.
|
|
158
|
+
*/
|
|
159
|
+
calendar: (props: UiNodeProps<"calendar">) => UiNode<"calendar">;
|
|
160
|
+
/**
|
|
161
|
+
* A `image` node. Since ui-sdk 1.4.0.
|
|
162
|
+
* Caps: `alt` 300, `caption` 200, `src` 2048. Every cap is in `NODE_LIMITS.image`.
|
|
163
|
+
*/
|
|
164
|
+
image: (props: UiNodeProps<"image">) => UiNode<"image">;
|
|
165
|
+
/**
|
|
166
|
+
* A `video` node. Since ui-sdk 1.4.0.
|
|
167
|
+
* Caps: `caption` 200, `captions` 256, `key` 64, `poster` 2048, `src` 2048, `title` 120. Every cap is in `NODE_LIMITS.video`.
|
|
168
|
+
*/
|
|
169
|
+
video: (props: UiNodeProps<"video">) => UiNode<"video">;
|
|
170
|
+
/**
|
|
171
|
+
* A `audio` node. Since the current release.
|
|
172
|
+
* Caps: `caption` 200, `key` 64, `src` 256, `title` 120. Every cap is in `NODE_LIMITS.audio`.
|
|
173
|
+
*/
|
|
174
|
+
audio: (props: UiNodeProps<"audio">) => UiNode<"audio">;
|
|
175
|
+
/**
|
|
176
|
+
* A `document` node. Since the current release.
|
|
177
|
+
* Caps: `caption` 200, `src` 256, `title` 120. Every cap is in `NODE_LIMITS.document`.
|
|
178
|
+
*/
|
|
179
|
+
document: (props: UiNodeProps<"document">) => UiNode<"document">;
|
|
180
|
+
/**
|
|
181
|
+
* A `dataset` node. Since the current release.
|
|
182
|
+
* Caps: `dataset` 64, `empty` 80, `fields` 12, `groupBy` 64, `param` 64. Every cap is in `NODE_LIMITS.dataset`.
|
|
183
|
+
*/
|
|
184
|
+
dataset: (props: UiNodeProps<"dataset">) => UiNode<"dataset">;
|
|
185
|
+
/**
|
|
186
|
+
* A `section` node. Since ui-sdk 1.1.0. Holds child nodes. Kee may draw it in a chat reply.
|
|
187
|
+
* Caps: `badge` 24, `children` 100, `description` 300, `key` 64, `title` 120, `weights` 4. Every cap is in `NODE_LIMITS.section`.
|
|
188
|
+
*/
|
|
189
|
+
section: (props: UiNodeProps<"section">) => UiNode<"section">;
|
|
190
|
+
/**
|
|
191
|
+
* A `tabs` node. Since ui-sdk 1.1.0. Holds child nodes.
|
|
192
|
+
* Caps: `active` 64, `key` 64, `param` 64, `tabs` 10. Every cap is in `NODE_LIMITS.tabs`.
|
|
193
|
+
*/
|
|
194
|
+
tabs: (props: UiNodeProps<"tabs">) => UiNode<"tabs">;
|
|
195
|
+
};
|