@withgateway/sdk 0.3.1 → 0.3.3
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 +4 -3
- package/dist/cli.js +2880 -2032
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +2982 -2135
- package/dist/cli.mjs.map +1 -1
- package/dist/files/index.d.mts +1 -1
- package/dist/files/index.d.ts +1 -1
- package/dist/files/index.js +180 -41
- package/dist/files/index.js.map +1 -1
- package/dist/files/index.mjs +172 -35
- package/dist/files/index.mjs.map +1 -1
- package/dist/{index-CBqbB_0J.d.ts → index-DUHmS74p.d.ts} +1 -1
- package/dist/{index-D-AMnAMa.d.mts → index-mJhG3vZU.d.mts} +1 -1
- package/dist/index.d.mts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +1309 -540
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1287 -532
- package/dist/index.mjs.map +1 -1
- package/dist/{media-CbcAfQvd.d.mts → media-BIApiPzS.d.mts} +21 -2
- package/dist/{media-CbcAfQvd.d.ts → media-BIApiPzS.d.ts} +21 -2
- package/dist/traces/index.d.mts +1 -1
- package/dist/traces/index.d.ts +1 -1
- package/dist/traces/index.js +177 -38
- package/dist/traces/index.js.map +1 -1
- package/dist/traces/index.mjs +169 -32
- package/dist/traces/index.mjs.map +1 -1
- package/dist/tracing/index.d.mts +2 -2
- package/dist/tracing/index.d.ts +2 -2
- package/dist/tracing/index.js +69 -13
- package/dist/tracing/index.js.map +1 -1
- package/dist/tracing/index.mjs +69 -13
- package/dist/tracing/index.mjs.map +1 -1
- package/dist/worlds/index.d.mts +249 -120
- package/dist/worlds/index.d.ts +249 -120
- package/dist/worlds/index.js +1240 -484
- package/dist/worlds/index.js.map +1 -1
- package/dist/worlds/index.mjs +1231 -476
- package/dist/worlds/index.mjs.map +1 -1
- package/package.json +1 -1
- package/runtime/gatewaysdk/environment/_bundle.py +2 -27
- package/runtime/gatewaysdk/environment/_materialize.py +49 -69
- package/runtime/gatewaysdk/environment/_world.py +122 -197
- package/runtime/gatewaysdk/environment/backing.py +137 -0
- package/runtime/gatewaysdk/environment/core.py +1 -1
- package/runtime/gatewaysdk/environment/legacy_sqlite.py +301 -0
- package/runtime/gatewaysdk/environment/reward_keys.py +25 -0
- package/runtime/gatewaysdk/environment/runtime.py +8 -13
- package/runtime/gatewaysdk/environment/schema/__init__.py +4 -4
- package/runtime/gatewaysdk/environment/schema/admin_routes.py +86 -0
- package/runtime/gatewaysdk/environment/schema/api.py +266 -514
- package/runtime/gatewaysdk/environment/schema/batch.py +42 -37
- package/runtime/gatewaysdk/environment/schema/bundle_runtime.py +17 -5
- package/runtime/gatewaysdk/environment/schema/call_log.py +291 -0
- package/runtime/gatewaysdk/environment/schema/call_redaction.py +344 -0
- package/runtime/gatewaysdk/environment/schema/compiler.py +334 -158
- package/runtime/gatewaysdk/environment/schema/conform.py +8 -12
- package/runtime/gatewaysdk/environment/schema/connector.py +27 -62
- package/runtime/gatewaysdk/environment/schema/data.py +50 -207
- package/runtime/gatewaysdk/environment/schema/database/__init__.py +24 -0
- package/runtime/gatewaysdk/environment/schema/database/audit.py +121 -0
- package/runtime/gatewaysdk/environment/schema/database/base.py +995 -0
- package/runtime/gatewaysdk/environment/schema/database/budget.py +33 -0
- package/runtime/gatewaysdk/environment/schema/database/connections.py +101 -0
- package/runtime/gatewaysdk/environment/schema/database/engines.py +8 -0
- package/runtime/gatewaysdk/environment/schema/database/guard.py +208 -0
- package/runtime/gatewaysdk/environment/schema/database/ingest.py +154 -0
- package/runtime/gatewaysdk/environment/schema/database/matching.py +58 -0
- package/runtime/gatewaysdk/environment/schema/database/provenance.py +30 -0
- package/runtime/gatewaysdk/environment/schema/database/rows.py +53 -0
- package/runtime/gatewaysdk/environment/schema/database/runtime_rows.py +39 -0
- package/runtime/gatewaysdk/environment/schema/database/sqlite.py +367 -0
- package/runtime/gatewaysdk/environment/schema/domain_migration.py +236 -0
- package/runtime/gatewaysdk/environment/schema/envelope.py +169 -0
- package/runtime/gatewaysdk/environment/schema/extract.py +6 -13
- package/runtime/gatewaysdk/environment/schema/grading.py +45 -30
- package/runtime/gatewaysdk/environment/schema/handler_errors.py +142 -12
- package/runtime/gatewaysdk/environment/schema/host.py +158 -250
- package/runtime/gatewaysdk/environment/schema/lanes.py +53 -0
- package/runtime/gatewaysdk/environment/schema/local.py +44 -71
- package/runtime/gatewaysdk/environment/schema/mapping.py +5 -25
- package/runtime/gatewaysdk/environment/schema/openapi_import.py +112 -73
- package/runtime/gatewaysdk/environment/schema/openapi_paging.py +147 -0
- package/runtime/gatewaysdk/environment/schema/openapi_payloads.py +288 -0
- package/runtime/gatewaysdk/environment/schema/openapi_routes.py +97 -102
- package/runtime/gatewaysdk/environment/schema/openapi_schemas.py +113 -30
- package/runtime/gatewaysdk/environment/schema/platform.py +123 -171
- package/runtime/gatewaysdk/environment/schema/platform_import.py +2 -3
- package/runtime/gatewaysdk/environment/schema/platform_query.py +61 -5
- package/runtime/gatewaysdk/environment/schema/primary_key.py +61 -0
- package/runtime/gatewaysdk/environment/schema/proof.py +19 -12
- package/runtime/gatewaysdk/environment/schema/runtime.py +77 -160
- package/runtime/gatewaysdk/environment/schema/served_names.py +58 -0
- package/runtime/gatewaysdk/environment/schema/session_files.py +166 -0
- package/runtime/gatewaysdk/environment/schema/skills/benchmarks-getting-started/SKILL.md +2 -2
- package/runtime/gatewaysdk/environment/schema/skills/connector-schema-authoring/SKILL.md +19 -11
- package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/ROWS.md +29 -3
- package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/SKILL.md +14 -14
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/SKILL.md +12 -12
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/cli-mcp-map.md +3 -2
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/gotchas.md +15 -0
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/openapi-import.md +50 -21
- package/runtime/gatewaysdk/environment/schema/slack.py +4 -8
- package/runtime/gatewaysdk/environment/schema/snapshot.py +385 -129
- package/runtime/gatewaysdk/environment/schema/store_contract.py +8 -14
- package/runtime/gatewaysdk/environment/schema/tasks.py +140 -55
- package/runtime/gatewaysdk/environment/schema/tools_world.py +110 -87
- package/runtime/gatewaysdk/environment/schema/ui.py +24 -5
- package/runtime/gatewaysdk/environment/schema/ui_static/app.js +100 -130
- package/runtime/gatewaysdk/environment/schema/ui_static/index.html +1 -8
- package/runtime/gatewaysdk/environment/schema/validation.py +166 -40
- package/runtime/gatewaysdk/environment/schema/workers.py +142 -57
- package/runtime/gatewaysdk/environment/schema/world_sql.py +81 -0
- package/runtime/gatewaysdk/environment/schema/world_tests.py +71 -39
- package/runtime/gatewaysdk/environment/score.py +57 -0
- package/runtime/gatewaysdk/environment/secret_values.py +36 -0
- package/runtime/gatewaysdk/environment/world.py +42 -3
- package/runtime/gatewaysdk/environment/world_session_contract.py +32 -5
- package/skills/benchmarks-getting-started/SKILL.md +2 -2
- package/skills/connector-schema-authoring/SKILL.md +19 -11
- package/skills/connector-schema-authoring/connector-worlds.md +13 -4
- package/skills/connector-schema-authoring/world-schema.md +103 -51
- package/skills/world-data-ingestion/ROWS.md +29 -3
- package/skills/world-data-ingestion/SKILL.md +14 -14
- package/skills/worlds-getting-started/SKILL.md +12 -12
- package/skills/worlds-getting-started/references/cli-mcp-map.md +3 -2
- package/skills/worlds-getting-started/references/gotchas.md +15 -0
- package/skills/worlds-getting-started/references/openapi-import.md +50 -21
- package/runtime/gatewaysdk/environment/schema/store.py +0 -1013
|
@@ -1,36 +1,15 @@
|
|
|
1
1
|
// gateway-world-ui: generated
|
|
2
|
-
//
|
|
3
|
-
// The world's browsable UI over its own routes. `./contract.json` (written by
|
|
4
|
-
// `gateway worlds schema compile` from schema/world.json + connector.toml)
|
|
5
|
-
// names every entity, its fields, keys and relationships, and the route that
|
|
6
|
-
// lists, reads, creates, updates and deletes it. Every request this page makes
|
|
7
|
-
// goes to `contract.apiPath` on its own origin: the World Host rewrites that
|
|
8
|
-
// prefix onto the session's api and admits the request by the session cookie
|
|
9
|
-
// it set on the first page load (`?token=`), `gateway worlds serve` answers it
|
|
10
|
-
// with the local pin. No key is embedded here and none is asked for.
|
|
11
|
-
//
|
|
12
|
-
// Routes: #/ every entity
|
|
13
|
-
// #/<entity> the list route (query params as filters)
|
|
14
|
-
// #/<entity>/new the create route's form
|
|
15
|
-
// #/<entity>/<key> the get route (or the row from the list)
|
|
16
|
-
// #/<entity>/<key>/edit the update route's form
|
|
17
|
-
// #/<entity>/<key>/raw the row as the api answered it
|
|
18
2
|
"use strict";
|
|
19
3
|
|
|
20
4
|
(function () {
|
|
21
|
-
// True in a browser tab; false under a plain `node app.js` (the pure
|
|
22
|
-
// helpers below are still reachable there, at `globalThis.__gatewayWorldUi`,
|
|
23
|
-
// for tests — nothing DOM-shaped runs without a document to render into).
|
|
24
5
|
const hasDom = typeof document !== "undefined" && typeof document.getElementById === "function";
|
|
25
6
|
const $view = hasDom ? document.getElementById("view") : null;
|
|
26
7
|
const $nav = hasDom ? document.getElementById("nav") : null;
|
|
8
|
+
const LIST_COLUMNS = 8;
|
|
27
9
|
let contract = null;
|
|
28
|
-
// Rows
|
|
29
|
-
// row whose entity has no by-id route is served from here.
|
|
10
|
+
// Rows from the last list page, by key: the detail of an entity with no get route reads them.
|
|
30
11
|
const seen = {};
|
|
31
12
|
|
|
32
|
-
// ------------------------------------------------------------ helpers
|
|
33
|
-
|
|
34
13
|
function esc(value) {
|
|
35
14
|
return String(value)
|
|
36
15
|
.replace(/&/g, "&")
|
|
@@ -44,11 +23,17 @@
|
|
|
44
23
|
}
|
|
45
24
|
|
|
46
25
|
function dig(row, path) {
|
|
47
|
-
// A field path is a JSON pointer without the leading slash ("owner/id")
|
|
48
|
-
// or a plain name; connector.toml writes both.
|
|
49
26
|
if (row == null) return undefined;
|
|
50
27
|
if (path in row) return row[path];
|
|
51
|
-
return path
|
|
28
|
+
return pathParts(path).reduce((cur, part) => (cur == null ? undefined : cur[part]), row);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function pathParts(path) {
|
|
32
|
+
if (!path.startsWith("/")) return path.split("/");
|
|
33
|
+
return path
|
|
34
|
+
.slice(1)
|
|
35
|
+
.split("/")
|
|
36
|
+
.map((part) => part.replace(/~1/g, "/").replace(/~0/g, "~"));
|
|
52
37
|
}
|
|
53
38
|
|
|
54
39
|
function set(target, path, value) {
|
|
@@ -94,8 +79,6 @@
|
|
|
94
79
|
return esc(String(value));
|
|
95
80
|
}
|
|
96
81
|
|
|
97
|
-
// ------------------------------------------------------------ the api
|
|
98
|
-
|
|
99
82
|
async function api(method, path, query, body) {
|
|
100
83
|
const q = new URLSearchParams();
|
|
101
84
|
for (const [k, v] of Object.entries(query || {})) if (v !== "" && v != null) q.append(k, v);
|
|
@@ -116,29 +99,28 @@
|
|
|
116
99
|
return { status: res.status, ok: res.ok, body: json, text, request: `${method} ${url}` };
|
|
117
100
|
}
|
|
118
101
|
|
|
119
|
-
function unwrap(route, entity, body) {
|
|
120
|
-
// Rows out of the vendor's envelope: the operation's `results` path, the
|
|
121
|
-
// route's own envelope key, else the world's; a bare array; a single row
|
|
122
|
-
// (it carries the primary key).
|
|
102
|
+
function unwrap(route, entity, body, api = contract.api) {
|
|
123
103
|
if (Array.isArray(body)) return { rows: body, cursor: null };
|
|
124
|
-
if (body
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
if (Array.isArray(body[k])) return { rows: body[k], cursor: body[contract.api.cursorKey] ?? null };
|
|
135
|
-
}
|
|
136
|
-
if (entity.primaryKey.every((f) => dig(body, f) !== undefined)) return { rows: [body], cursor: null, single: true };
|
|
137
|
-
}
|
|
104
|
+
if (!body || typeof body !== "object") return { rows: [], cursor: null, raw: body };
|
|
105
|
+
const results = route.results ? listedAt(body, route.results, api) : null;
|
|
106
|
+
if (results && Array.isArray(results.rows)) return results;
|
|
107
|
+
if (results && results.rows && typeof results.rows === "object") return { rows: [results.rows], cursor: null, single: true };
|
|
108
|
+
const key = route.envelope === null || route.envelope === undefined ? api.envelope : route.envelope;
|
|
109
|
+
const enveloped = key ? listedAt(body, key, api) : null;
|
|
110
|
+
if (enveloped && Array.isArray(enveloped.rows)) return enveloped;
|
|
111
|
+
const common = ["results", "items", "data", "nodes", "value", "records"].find((k) => Array.isArray(body[k]));
|
|
112
|
+
if (common) return { rows: body[common], cursor: body[api.cursorKey] ?? null };
|
|
113
|
+
if (entity.primaryKey.every((f) => dig(body, f) !== undefined)) return { rows: [body], cursor: null, single: true };
|
|
138
114
|
return { rows: [], cursor: null, raw: body };
|
|
139
115
|
}
|
|
140
116
|
|
|
141
|
-
|
|
117
|
+
function listedAt(body, dotted, api) {
|
|
118
|
+
const parts = dotted.split(".");
|
|
119
|
+
const holder = parts.length > 1 ? dig(body, parts.slice(0, -1).join("/")) : body;
|
|
120
|
+
const around = holder && typeof holder === "object" ? holder : body;
|
|
121
|
+
const total = api.hitsKey ? around[api.hitsKey] ?? body[api.hitsKey] : undefined;
|
|
122
|
+
return { rows: dig(body, parts.join("/")), cursor: around[api.cursorKey] ?? body[api.cursorKey] ?? null, total };
|
|
123
|
+
}
|
|
142
124
|
|
|
143
125
|
function parseHash() {
|
|
144
126
|
const raw = (location.hash || "#/").slice(1);
|
|
@@ -150,14 +132,7 @@
|
|
|
150
132
|
}
|
|
151
133
|
|
|
152
134
|
function link(parts, query) {
|
|
153
|
-
// Every part arrives already
|
|
154
|
-
// plain identifier (an entity or field name — never has URL-special
|
|
155
|
-
// characters), a literal such as "new"/"edit"/"raw", or a value a caller
|
|
156
|
-
// percent-encoded itself (a single field's value, or `keyOf`'s already-
|
|
157
|
-
// encoded, comma-joined composite key). Encoding here too would encode
|
|
158
|
-
// those encoded parts a second time — a "%3A" becomes "%253A", the plain
|
|
159
|
-
// "," `keyOf` joins parts with becomes "%2C" and no longer splits back
|
|
160
|
-
// apart — so a part is used exactly as given.
|
|
135
|
+
// Every part arrives encoded once already (keyOf, a caller's value): encoding again breaks the round trip.
|
|
161
136
|
const q = query ? new URLSearchParams(query).toString() : "";
|
|
162
137
|
return "#/" + parts.join("/") + (q ? "?" + q : "");
|
|
163
138
|
}
|
|
@@ -180,15 +155,11 @@
|
|
|
180
155
|
}
|
|
181
156
|
}
|
|
182
157
|
|
|
183
|
-
// ------------------------------------------------------------ views
|
|
184
|
-
|
|
185
158
|
function renderNav(active) {
|
|
186
159
|
$nav.innerHTML =
|
|
187
160
|
h`<a href="#/" class="${!active ? "active" : ""}">All entities</a>` +
|
|
188
161
|
Object.entries(contract.entities)
|
|
189
162
|
.map(([name, e]) => {
|
|
190
|
-
// A lowercase "l" marks a scoped list — it needs values before it
|
|
191
|
-
// can be fetched, unlike a plain "L" list the page can load as is.
|
|
192
163
|
const verbs = ["list", "get", "create", "update", "delete"]
|
|
193
164
|
.filter((v) => e.routes[v])
|
|
194
165
|
.map((v) => (v === "list" && e.routes.list.scoped ? "l" : v[0].toUpperCase()))
|
|
@@ -216,11 +187,15 @@
|
|
|
216
187
|
h`<div class="api">api ${esc(contract.apiPath)} · ${Object.keys(contract.entities).length} entities · ${esc(contract.world.id)}</div>`;
|
|
217
188
|
}
|
|
218
189
|
|
|
219
|
-
function columnsOf(entity) {
|
|
190
|
+
function columnsOf(entity, route) {
|
|
220
191
|
const scalars = entity.fields.filter(isScalar);
|
|
221
|
-
const
|
|
222
|
-
const
|
|
223
|
-
|
|
192
|
+
const filtered = route.params.map((param) => (route.filter || {})[param] || param);
|
|
193
|
+
const enumerated = scalars.filter((f) => f.enum).map((f) => f.name);
|
|
194
|
+
const order = new Set([...entity.primaryKey, ...filtered, ...enumerated, ...scalars.map((f) => f.name)]);
|
|
195
|
+
return [...order]
|
|
196
|
+
.map((name) => scalars.find((f) => f.name === name))
|
|
197
|
+
.filter(Boolean)
|
|
198
|
+
.slice(0, LIST_COLUMNS);
|
|
224
199
|
}
|
|
225
200
|
|
|
226
201
|
function cell(entity, field, row) {
|
|
@@ -231,6 +206,13 @@
|
|
|
231
206
|
return show(value);
|
|
232
207
|
}
|
|
233
208
|
|
|
209
|
+
function filterForm(fields, scoped, missing) {
|
|
210
|
+
if (!fields) return "";
|
|
211
|
+
const kind = scoped ? "scope" : "query";
|
|
212
|
+
const action = missing ? "Load" : "Apply";
|
|
213
|
+
return h`<form data-filters><span class="muted">${kind}</span> ${fields} <button class="btn" type="submit">${action}</button></form>`;
|
|
214
|
+
}
|
|
215
|
+
|
|
234
216
|
async function renderList(name, entity, query) {
|
|
235
217
|
$view.dataset.view = "list";
|
|
236
218
|
$view.dataset.entity = name;
|
|
@@ -241,12 +223,6 @@
|
|
|
241
223
|
$view.innerHTML = head + h`<div class="bar">${actions}</div><p class="muted">This entity declares no list route. ${routesList(entity)}</p>`;
|
|
242
224
|
return;
|
|
243
225
|
}
|
|
244
|
-
// A scoped list's path has placeholders of its own (a parent id, say) —
|
|
245
|
-
// require them up front, as a small form, before ever fetching. Their
|
|
246
|
-
// values live in the hash query under the row field each fills (`route.args`
|
|
247
|
-
// maps the placeholder to that field), the same place a plain filter's
|
|
248
|
-
// query param lives, so a parent's detail view can deep-link here with
|
|
249
|
-
// them prefilled and "Apply"/"Load" round-trips through the same query.
|
|
250
226
|
const scopeFields = route.scoped ? [...new Set(Object.values(route.args))] : [];
|
|
251
227
|
const missing = scopeFields.filter((f) => !query[f]);
|
|
252
228
|
const scopeInputs = scopeFields
|
|
@@ -255,10 +231,7 @@
|
|
|
255
231
|
const filterInputs = route.params
|
|
256
232
|
.map((p) => h`<label>${esc(p)} <input name="${esc(p)}" value="${esc(query[p] || "")}" size="12"></label>`)
|
|
257
233
|
.join("");
|
|
258
|
-
const
|
|
259
|
-
const form = formFields
|
|
260
|
-
? h`<form data-filters><span class="muted">${scopeFields.length ? "scope" : "query"}</span> ${formFields} <button class="btn" type="submit">${missing.length ? "Load" : "Apply"}</button></form>`
|
|
261
|
-
: "";
|
|
234
|
+
const form = filterForm(scopeInputs + filterInputs, scopeFields.length > 0, missing.length > 0);
|
|
262
235
|
const bindFilterForm = () => {
|
|
263
236
|
const el = $view.querySelector("form[data-filters]");
|
|
264
237
|
if (el)
|
|
@@ -279,10 +252,7 @@
|
|
|
279
252
|
}
|
|
280
253
|
$view.innerHTML = head + h`<div class="bar">${actions}${form}</div><p class="muted">Loading…</p>`;
|
|
281
254
|
bindFilterForm();
|
|
282
|
-
//
|
|
283
|
-
// them, but keep everything else in `query` (declared filter params, and
|
|
284
|
-
// a paging cursor/offset the "Next page" link may have added, which
|
|
285
|
-
// `route.params` does not name).
|
|
255
|
+
// Only the scope goes into the path: paging params the Next link adds are not in route.params.
|
|
286
256
|
const apiQuery = { ...query };
|
|
287
257
|
for (const f of scopeFields) delete apiQuery[f];
|
|
288
258
|
const path = route.scoped ? fillPath(route, query) : route.path;
|
|
@@ -290,7 +260,7 @@
|
|
|
290
260
|
const page = unwrap(route, entity, res.body);
|
|
291
261
|
seen[name] = seen[name] || {};
|
|
292
262
|
for (const row of page.rows) seen[name][keyOf(entity, row)] = row;
|
|
293
|
-
const columns = columnsOf(entity);
|
|
263
|
+
const columns = columnsOf(entity, route);
|
|
294
264
|
let table;
|
|
295
265
|
if (!res.ok) table = h`<div class="error">HTTP ${res.status}: ${esc(res.text.slice(0, 500))}</div>`;
|
|
296
266
|
else if (page.rows.length === 0 && page.raw !== undefined) table = h`<pre class="raw">${esc(JSON.stringify(page.raw, null, 2))}</pre>`;
|
|
@@ -307,8 +277,6 @@
|
|
|
307
277
|
})
|
|
308
278
|
.join("") +
|
|
309
279
|
"</tbody></table>";
|
|
310
|
-
// The next page, the way the world pages: a cursor under the api's
|
|
311
|
-
// cursor param, or the offset moved by the page size.
|
|
312
280
|
let paging = "";
|
|
313
281
|
const style = route.paging || contract.api.paging;
|
|
314
282
|
if (res.ok && style === "cursor" && page.cursor) {
|
|
@@ -329,10 +297,6 @@
|
|
|
329
297
|
}
|
|
330
298
|
|
|
331
299
|
function childScopedLists(entityName) {
|
|
332
|
-
// Other entities whose scoped list can be filled straight from a row of
|
|
333
|
-
// `entityName`: one of their list route's placeholders resolves (through
|
|
334
|
-
// `route.args`) to a field of their own that is itself the foreign key
|
|
335
|
-
// back to this entity (`field.link === entityName`).
|
|
336
300
|
const out = [];
|
|
337
301
|
for (const [childName, child] of Object.entries(contract.entities)) {
|
|
338
302
|
const route = child.routes.list;
|
|
@@ -373,8 +337,20 @@
|
|
|
373
337
|
return { row: null, res: { request: "(no route reads this entity)", status: 0 } };
|
|
374
338
|
}
|
|
375
339
|
|
|
340
|
+
async function deleteRow(name, entity, key, row) {
|
|
341
|
+
const r = entity.routes.delete;
|
|
342
|
+
if (!window.confirm(`${r.method} ${fillPath(r, row)} — delete this ${entity.title}?`)) return;
|
|
343
|
+
const out = await api(r.method, fillPath(r, row));
|
|
344
|
+
if (!out.ok) {
|
|
345
|
+
$view.insertAdjacentHTML("beforeend", h`<div class="error">${esc(out.request)} → ${out.status}\n${esc(out.text.slice(0, 500))}</div>`);
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
delete (seen[name] || {})[key];
|
|
349
|
+
location.hash = link([name], listQueryFor(entity, row));
|
|
350
|
+
}
|
|
351
|
+
|
|
376
352
|
async function renderDetail(name, entity, key, mode) {
|
|
377
|
-
$view.dataset.view =
|
|
353
|
+
$view.dataset.view = { edit: "edit", raw: "raw" }[mode] || "detail";
|
|
378
354
|
$view.dataset.entity = name;
|
|
379
355
|
$view.dataset.key = key;
|
|
380
356
|
$view.innerHTML = h`<h2>${esc(entity.title)}</h2><p class="muted">Loading…</p>`;
|
|
@@ -399,9 +375,6 @@
|
|
|
399
375
|
extra.map((k) => h`<dt>${esc(k)} <span class="type">undeclared</span></dt><dd data-field="${esc(k)}">${show(row[k])}</dd>`).join("") +
|
|
400
376
|
"</dl>";
|
|
401
377
|
}
|
|
402
|
-
// A single-field primary key is what a foreign-key link elsewhere in this
|
|
403
|
-
// page already assumes (`cell`'s field links do the same) — the value a
|
|
404
|
-
// child's scoped list wants is this row's own key.
|
|
405
378
|
const children = entity.primaryKey.length === 1 ? childScopedLists(name) : [];
|
|
406
379
|
const childBar = children.length
|
|
407
380
|
? h`<div class="bar"><span class="muted">Lists scoped to this row:</span> ${children
|
|
@@ -410,22 +383,10 @@
|
|
|
410
383
|
: "";
|
|
411
384
|
$view.innerHTML = title + bar + body + childBar + h`<div class="api" data-request>${esc(res.request)} → ${res.status}</div>`;
|
|
412
385
|
const del = $view.querySelector('[data-action="delete"]');
|
|
413
|
-
if (del)
|
|
414
|
-
del.addEventListener("click", async () => {
|
|
415
|
-
const r = entity.routes.delete;
|
|
416
|
-
if (!window.confirm(`${r.method} ${fillPath(r, row)} — delete this ${entity.title}?`)) return;
|
|
417
|
-
const out = await api(r.method, fillPath(r, row));
|
|
418
|
-
if (out.ok) {
|
|
419
|
-
delete (seen[name] || {})[key];
|
|
420
|
-
location.hash = link([name]);
|
|
421
|
-
} else $view.insertAdjacentHTML("beforeend", h`<div class="error">${esc(out.request)} → ${out.status}\n${esc(out.text.slice(0, 500))}</div>`);
|
|
422
|
-
});
|
|
386
|
+
if (del) del.addEventListener("click", () => deleteRow(name, entity, key, row));
|
|
423
387
|
}
|
|
424
388
|
|
|
425
389
|
function editableFields(entity, route, row) {
|
|
426
|
-
// What the form asks for: the row fields the route's body mapping names,
|
|
427
|
-
// else every declared field the server does not mint itself; on an
|
|
428
|
-
// update the key fields are the address, not the payload.
|
|
429
390
|
const generated = new Set(route.generated || []);
|
|
430
391
|
let fields = entity.fields.filter((f) => !generated.has(f.name));
|
|
431
392
|
if (route.body && Object.keys(route.body).length) {
|
|
@@ -436,25 +397,51 @@
|
|
|
436
397
|
return fields;
|
|
437
398
|
}
|
|
438
399
|
|
|
439
|
-
function
|
|
400
|
+
function controlId(field) {
|
|
401
|
+
return "f-" + encodeURIComponent(field.name);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function requiredFields(entity, route, row) {
|
|
405
|
+
if (row || route.handler) return new Set();
|
|
406
|
+
const filled = new Set([...(route.generated || []), ...Object.keys(route.defaults || {})]);
|
|
407
|
+
return new Set(entity.fields.filter((f) => f.required && !filled.has(f.name)).map((f) => f.name));
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function input(field, value, attrs) {
|
|
440
411
|
const types = (field.type || []).filter((t) => t !== "null");
|
|
441
412
|
const t = types[0] || "string";
|
|
442
413
|
const name = esc(field.name);
|
|
443
414
|
const current = value === undefined ? "" : value;
|
|
444
415
|
if (!isScalar(field)) {
|
|
445
|
-
return h`<textarea name="${name}" data-json placeholder="JSON">${esc(current === "" || current === null ? "" : JSON.stringify(current, null, 2))}</textarea>`;
|
|
416
|
+
return h`<textarea name="${name}"${attrs} data-json placeholder="JSON">${esc(current === "" || current === null ? "" : JSON.stringify(current, null, 2))}</textarea>`;
|
|
446
417
|
}
|
|
447
418
|
if (field.enum) {
|
|
448
419
|
const opts = ['<option value="">—</option>'].concat(field.enum.map((v) => h`<option value="${esc(String(v))}" ${String(current) === String(v) ? "selected" : ""}>${esc(String(v))}</option>`));
|
|
449
|
-
return h`<select name="${name}">${opts.join("")}</select>`;
|
|
420
|
+
return h`<select name="${name}"${attrs}>${opts.join("")}</select>`;
|
|
450
421
|
}
|
|
451
422
|
if (t === "boolean") {
|
|
452
|
-
return h`<select name="${name}" data-boolean><option value="">—</option><option value="true" ${current === true ? "selected" : ""}>true</option><option value="false" ${current === false ? "selected" : ""}>false</option></select>`;
|
|
423
|
+
return h`<select name="${name}"${attrs} data-boolean><option value="">—</option><option value="true" ${current === true ? "selected" : ""}>true</option><option value="false" ${current === false ? "selected" : ""}>false</option></select>`;
|
|
453
424
|
}
|
|
454
425
|
if (t === "integer" || t === "number") {
|
|
455
|
-
return h`<input name="${name}" type="number" ${t === "integer" ? 'step="1"' : 'step="any"'} value="${esc(current === null ? "" : current)}" data-number>`;
|
|
426
|
+
return h`<input name="${name}"${attrs} type="number" ${t === "integer" ? 'step="1"' : 'step="any"'} value="${esc(current === null ? "" : current)}" data-number>`;
|
|
456
427
|
}
|
|
457
|
-
return h`<input name="${name}" value="${esc(current === null ? "" : current)}" placeholder="${esc(field.format || "")}" size="40">`;
|
|
428
|
+
return h`<input name="${name}"${attrs} value="${esc(current === null ? "" : current)}" placeholder="${esc(field.format || "")}" size="40">`;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
function hintOf(field, id) {
|
|
432
|
+
if (!field.description && !field.link) return "";
|
|
433
|
+
const target = field.link ? h`→ <a href="${link([field.link])}">${esc(field.link)}</a> ` : "";
|
|
434
|
+
return h`<div class="hint" id="${id}">${target}${esc(field.description || "")}</div>`;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
function formField(field, value, required) {
|
|
438
|
+
const id = esc(controlId(field));
|
|
439
|
+
const hint = hintOf(field, `${id}:hint`);
|
|
440
|
+
const described = hint ? `${id}:type ${id}:hint` : `${id}:type`;
|
|
441
|
+
const attrs = ` id="${id}" aria-describedby="${described}"${required ? ' aria-required="true"' : ""}`;
|
|
442
|
+
const star = required ? '<span class="req" aria-hidden="true"> *</span>' : "";
|
|
443
|
+
const type = esc(typeOf(field)) + (field.format ? ":" + esc(field.format) : "");
|
|
444
|
+
return h`<div class="field"><div class="name"><label for="${id}">${esc(field.name)}</label>${star} <span class="type" id="${id}:type">${type}</span></div><div>${input(field, value, attrs)}</div>${hint}</div>`;
|
|
458
445
|
}
|
|
459
446
|
|
|
460
447
|
function readForm(form, fields) {
|
|
@@ -463,7 +450,7 @@
|
|
|
463
450
|
for (const f of fields) {
|
|
464
451
|
const raw = data.get(f.name);
|
|
465
452
|
if (raw === null || raw === "") continue;
|
|
466
|
-
const el =
|
|
453
|
+
const el = document.getElementById(controlId(f));
|
|
467
454
|
let value = raw;
|
|
468
455
|
if (el.dataset.json !== undefined) value = JSON.parse(raw);
|
|
469
456
|
else if (el.dataset.boolean !== undefined) value = raw === "true";
|
|
@@ -474,9 +461,6 @@
|
|
|
474
461
|
}
|
|
475
462
|
|
|
476
463
|
function listQueryFor(entity, row) {
|
|
477
|
-
// The list view that shows this row: its scope values and the filter
|
|
478
|
-
// params the row's own fields fill (`filter = { channel = "channel_id" }`),
|
|
479
|
-
// so a list that needs a parent comes back scoped to the row's parent.
|
|
480
464
|
const list = entity.routes.list;
|
|
481
465
|
if (!list) return undefined;
|
|
482
466
|
const query = {};
|
|
@@ -492,9 +476,6 @@
|
|
|
492
476
|
}
|
|
493
477
|
|
|
494
478
|
function payloadOf(route, values) {
|
|
495
|
-
// A body mapping (`body = { id = "id", value = "attributes/value" }`) is
|
|
496
|
-
// row field <- body path: the form's row fields go back out under the
|
|
497
|
-
// body paths the vendor documents. No mapping: the row as-is.
|
|
498
479
|
if (!route.body || !Object.keys(route.body).length) return values;
|
|
499
480
|
const out = {};
|
|
500
481
|
for (const [field, path] of Object.entries(route.body)) if (values[field] !== undefined) set(out, path, values[field]);
|
|
@@ -507,16 +488,10 @@
|
|
|
507
488
|
$view.dataset.entity = name;
|
|
508
489
|
if (!route) return renderError(`${entity.title} declares no ${row ? "update" : "create"} route`);
|
|
509
490
|
const fields = editableFields(entity, route, row);
|
|
491
|
+
const required = requiredFields(entity, route, row);
|
|
510
492
|
const key = row ? keyOf(entity, row) : null;
|
|
511
493
|
const title = row ? h`<h2><a href="${link([name])}">${esc(entity.title)}</a> / <a href="${link([name, key])}">${esc(decodeURIComponent(key))}</a> / edit</h2>` : h`<h2><a href="${link([name])}">${esc(entity.title)}</a> / new</h2>`;
|
|
512
|
-
const rows = fields
|
|
513
|
-
.map(
|
|
514
|
-
(f) =>
|
|
515
|
-
h`<div class="field"><label for="f-${esc(f.name)}">${esc(f.name)}${f.required && !row ? ' <span class="req">*</span>' : ""} <span class="type">${esc(typeOf(f))}${f.format ? ":" + esc(f.format) : ""}</span></label><div>${input(f, row ? dig(row, f.name) : undefined)}</div>${
|
|
516
|
-
f.description || f.link ? h`<div class="hint">${f.link ? h`→ <a href="${link([f.link])}">${esc(f.link)}</a> ` : ""}${esc(f.description || "")}</div>` : ""
|
|
517
|
-
}</div>`,
|
|
518
|
-
)
|
|
519
|
-
.join("");
|
|
494
|
+
const rows = fields.map((f) => formField(f, row ? dig(row, f.name) : undefined, required.has(f.name))).join("");
|
|
520
495
|
$view.innerHTML =
|
|
521
496
|
title +
|
|
522
497
|
h`<form data-form="${row ? "update" : "create"}">${rows}<div class="bar"><button class="btn" type="submit" data-action="submit">${row ? "Save" : "Create"}</button> <a href="${link(row ? [name, key] : [name])}">Cancel</a></div></form>` +
|
|
@@ -548,16 +523,13 @@
|
|
|
548
523
|
location.hash = link([name, keyOf(entity, created)]);
|
|
549
524
|
} else if (row || created) location.hash = link([name], listQueryFor(entity, { ...values, ...(row || {}), ...(created || {}) }));
|
|
550
525
|
else {
|
|
551
|
-
// A 200
|
|
552
|
-
// (Slack's `{ok: false, error}`): keep the form and show the answer.
|
|
526
|
+
// A 200 with no row is the vendor's own refusal (Slack's {ok: false, error}): keep the form.
|
|
553
527
|
form.querySelectorAll(".error").forEach((e) => e.remove());
|
|
554
528
|
form.insertAdjacentHTML("beforeend", h`<div class="error" data-error>HTTP ${res.status}, no row created\n${esc(res.text.slice(0, 800))}</div>`);
|
|
555
529
|
}
|
|
556
530
|
});
|
|
557
531
|
}
|
|
558
532
|
|
|
559
|
-
// ------------------------------------------------------------ boot
|
|
560
|
-
|
|
561
533
|
async function boot() {
|
|
562
534
|
const res = await fetch("./contract.json", { credentials: "same-origin" });
|
|
563
535
|
if (!res.ok) return renderError(`contract.json → ${res.status}`);
|
|
@@ -569,10 +541,8 @@
|
|
|
569
541
|
await route();
|
|
570
542
|
}
|
|
571
543
|
|
|
572
|
-
//
|
|
573
|
-
|
|
574
|
-
// page itself uses, not a reimplementation kept in sync by hand.
|
|
575
|
-
globalThis.__gatewayWorldUi = { esc, dig, keyOf, keyValues, fillPath, link, listQueryFor };
|
|
544
|
+
// For the node tests (tests/environment/test_schema_ui_app_js.py).
|
|
545
|
+
globalThis.__gatewayWorldUi = { esc, dig, keyOf, keyValues, fillPath, link, listQueryFor, unwrap };
|
|
576
546
|
|
|
577
547
|
if (hasDom) {
|
|
578
548
|
window.__worldUi = { get contract() { return contract; }, api, unwrap };
|
|
@@ -1,12 +1,5 @@
|
|
|
1
1
|
<!doctype html>
|
|
2
2
|
<!-- gateway-world-ui: generated -->
|
|
3
|
-
<!--
|
|
4
|
-
The world's browsable UI, generated from its contract by
|
|
5
|
-
`gateway worlds schema compile` (ui.py). Every view is one of the world's own
|
|
6
|
-
routes: the list view is the entity's GET list route, the detail view its GET
|
|
7
|
-
by id, the forms its POST/PATCH/DELETE. Set `[ui] generated = false` in
|
|
8
|
-
connector.toml to hand-write this directory instead; compile then leaves it.
|
|
9
|
-
-->
|
|
10
3
|
<html lang="en">
|
|
11
4
|
<head>
|
|
12
5
|
<meta charset="utf-8">
|
|
@@ -44,7 +37,7 @@
|
|
|
44
37
|
dt { color: var(--muted); }
|
|
45
38
|
dd { margin: 0; word-break: break-word; }
|
|
46
39
|
.field { display: grid; grid-template-columns: 200px 1fr; gap: 4px 12px; align-items: start; margin: 6px 0; }
|
|
47
|
-
.field
|
|
40
|
+
.field .name { padding-top: 5px; }
|
|
48
41
|
.field .hint { grid-column: 2; color: var(--muted); font-size: 12px; }
|
|
49
42
|
.req { color: var(--bad); }
|
|
50
43
|
.type { color: var(--muted); font-size: 11px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
|