@mistriwork/engine 1.0.0-rc.80 → 1.0.0-rc.82

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.
@@ -246,8 +246,9 @@ export function withHead(html, head) {
246
246
  beforeScripts('<meta name="mistri-shell" content="workbench">');
247
247
  if (head.scheme)
248
248
  beforeScripts(`<meta name="mistri-scheme" content="${escapeHtml(head.scheme)}">`);
249
+ /* Before the scripts too: the first one keys its cached startup colours by the look these grounds say. */
249
250
  if (head.grounds)
250
- out = out.replace(/<\/head>/i, () => ` ${groundsMeta(head.grounds)}\n </head>`);
251
+ beforeScripts(groundsMeta(head.grounds).replace(/\n /g, '\n '));
251
252
  if (head.observe)
252
253
  out = out.replace(/<\/head>/i, () => ` <meta name="mistri-observe" content="${escapeHtml(head.observe)}">\n </head>`);
253
254
  if (head.density)
@@ -64,7 +64,7 @@ export async function exportFile(asked) {
64
64
  const amountsOf = (tableId) => asked.amounts?.(tableId);
65
65
  /* An App that grants by role says who exports which table (appExportable); the file is refused where the Export menu is not offered. */
66
66
  if (!may('')) {
67
- answer(403, { error: 'Export is not available to this person.' });
67
+ answer(403, refusal('forbidden', ['Export is not available to this person.']));
68
68
  return undefined;
69
69
  }
70
70
  /* Counted until the response closes, whichever way it ends: a refusal, the file, or a client that left. */
@@ -84,11 +84,11 @@ export async function exportFile(asked) {
84
84
  });
85
85
  const format = body.format ?? 'csv';
86
86
  if (typeof format !== 'string' || !Object.hasOwn(FORMATS, format)) {
87
- answer(400, { error: 'format must be "csv", "json" or "ndjson".' });
87
+ answer(400, refusal('invalid_request', ['format must be "csv", "json" or "ndjson".']));
88
88
  return undefined;
89
89
  }
90
90
  if (body.columns !== undefined && (!Array.isArray(body.columns) || body.columns.some((column) => typeof column !== 'string'))) {
91
- answer(400, { error: 'columns must be a list of column names.' });
91
+ answer(400, refusal('invalid_request', ['columns must be a list of column names.']));
92
92
  return undefined;
93
93
  }
94
94
  const text = String(body.q ?? '');
@@ -133,13 +133,16 @@ export async function exportFile(asked) {
133
133
  found — the same answer whether it exists or not, so a refusal tells nothing — and any other question that cannot
134
134
  be read is a bad request. Either way `errors` says why, as `/api/run`'s does. */
135
135
  if (!parsed.query || parsed.errors.length) {
136
- answer(servesTable(schema, text) ? 400 : 404, { errors: parsed.errors });
136
+ const code = servesTable(schema, text) ? 'invalid_request' : 'not_found';
137
+ /* Each error keeps where it is in the question (`at`), which the query bar underlines. */
138
+ const said = refusal(code, parsed.errors.map((error) => error.message));
139
+ answer(code === 'not_found' ? 404 : 400, { ...said, errors: parsed.errors.map((error) => ({ code, ...error })) });
137
140
  return undefined;
138
141
  }
139
142
  const query = parsed.query;
140
143
  const table = findTable(schema, query.table);
141
144
  if (!table) {
142
- answer(404, { error: `Unknown table "${query.table}"` });
145
+ answer(404, refusal('not_found', [`Unknown table "${query.table}"`]));
143
146
  return undefined;
144
147
  }
145
148
  if (!may(table.id))
@@ -181,11 +184,17 @@ function servesTable(schema, text) {
181
184
  return !first || findTable(schema, first) !== undefined || schema.tables.some((table) => table.name === first);
182
185
  }
183
186
  function refuseTable(answer, tableId) {
184
- answer(403, {
185
- error: `Export of "${tableId}" is not available to this person.`,
186
- });
187
+ answer(403, refusal('forbidden', [`Export of "${tableId}" is not available to this person.`]));
187
188
  return undefined;
188
189
  }
190
+ /**
191
+ * A reply that is not the file, in the body the App API's other refusals use: a snake_case `code` a program branches
192
+ * on, `error` for a client that reads one sentence, and `errors` for the Export menu, which shows the first. A field
193
+ * report read a refusal's body for its code and found none, and took the status alone for the reason.
194
+ */
195
+ function refusal(code, messages) {
196
+ return { code, error: messages[0] ?? '', errors: messages.map((message) => ({ code, message })) };
197
+ }
189
198
  /** Stands for a table a view reaches that cannot be named (a many-valued condition it cannot resolve): never exportable. */
190
199
  const UNKNOWN_TABLE = '\u0000unknown';
191
200
  /** Every table a view reads from: its base, each table along a reference path in its columns, filter and order, and the rows its aggregates and `any` conditions read. */
@@ -33,6 +33,15 @@ export function toleratesLag(path, body) {
33
33
  if (!LAG_TOLERANT.has(path))
34
34
  return false;
35
35
  const view = body.view && typeof body.view === 'object' ? body.view : undefined;
36
+ /* A batch carries a free-form page's views (`items[].view`): one that asks for one row goes where it would alone. */
37
+ if (path === '/api/breakdowns' && Array.isArray(body.items)) {
38
+ const one = body.items.some((item) => {
39
+ const asked = item && typeof item === 'object' ? item.view : undefined;
40
+ return !!asked && typeof asked === 'object' && !toleratesLag('/api/view/run', asked);
41
+ });
42
+ if (one)
43
+ return false;
44
+ }
36
45
  return body.limit !== 1 && view?.limit !== 1;
37
46
  }
38
47
  /** The cookie a write sets so the same browser's next reads go to the primary, in the running product's name. */
@@ -42,6 +42,8 @@ function plainCount(schema, view) {
42
42
  }
43
43
  /* What a batched list's page may say: what `/api/run` reads of a first page. */
44
44
  const RUN_FIELDS = ['q', 'limit', 'offset', 'count', 'exactCount', 'countLimit', 'lanes'];
45
+ /* What a batched view may say: what `/api/view/run` reads. */
46
+ const VIEW_FIELDS = ['view', 'text', 'query', 'where', 'count', 'limit', 'offset'];
45
47
  /** The most rows a lanes read keeps per lane: a board's page, a few times over. */
46
48
  const LANE_MOST = 100;
47
49
  /** A run's `lanes`, read: absent, refused, or the column and how many of each. */
@@ -351,6 +353,14 @@ export async function dispatchResourceReadHttp(path, body, adapter, schema, opti
351
353
  if (items.some((item) => 'run' in item && !run(item))) {
352
354
  return refusal(400, ['A batched list is `run: { q, limit, offset, count, exactCount, countLimit }`.']);
353
355
  }
356
+ /* A free-form page's reads ride the same way (the ui-sdk host's useRows, its record, its choices): an item
357
+ that is `view` is answered as `/api/view/run` answers it, from the view-run fields alone. Nineteen lists
358
+ on one page were nineteen requests, each its own admission and its own person looked up. */
359
+ const viewed = (item) => item.view && typeof item.view === 'object' && !Array.isArray(item.view) ? item.view : undefined;
360
+ if (items.some((item) => 'view' in item && !viewed(item))) {
361
+ return refusal(400, ['A batched view is `view: { view, text, query, where, count, limit, offset }`.']);
362
+ }
363
+ const only = (asked, fields) => Object.fromEntries(fields.filter((field) => asked[field] !== undefined).map((field) => [field, asked[field]]));
354
364
  const results = Array.from({ length: items.length });
355
365
  const queue = items.map((item, at) => ({ item, at }));
356
366
  /* An item's route knows its own last wave, not the batch's: the others are
@@ -361,11 +371,14 @@ export async function dispatchResourceReadHttp(path, body, adapter, schema, opti
361
371
  const one = async () => {
362
372
  for (let next = queue.shift(); next; next = queue.shift()) {
363
373
  const page = run(next.item);
374
+ const view = viewed(next.item);
364
375
  const { def, text, limit, offset } = next.item;
365
376
  try {
366
377
  results[next.at] = (await (page
367
- ? dispatchResourceReadHttp('/api/run', Object.fromEntries(RUN_FIELDS.filter((field) => page[field] !== undefined).map((field) => [field, page[field]])), adapter, schema, each)
368
- : dispatchResourceReadHttp('/api/breakdown', { def, text, limit, offset }, adapter, schema, each)));
378
+ ? dispatchResourceReadHttp('/api/run', only(page, RUN_FIELDS), adapter, schema, each)
379
+ : view
380
+ ? dispatchResourceReadHttp('/api/view/run', only(view, VIEW_FIELDS), adapter, schema, each)
381
+ : dispatchResourceReadHttp('/api/breakdown', { def, text, limit, offset }, adapter, schema, each)));
369
382
  }
370
383
  catch (error) {
371
384
  /* A request's own refusal (a ResourceReadError: the provider's are
@@ -61,6 +61,11 @@ export interface QueryPerson {
61
61
  * refused rather than read as text.
62
62
  */
63
63
  chosen?: Readonly<Record<string, string | number | null>>;
64
+ /**
65
+ * With `chosen`: declared choices the door was not told (an API call that named some picks and not others). Each
66
+ * reads as `chosen` itself being absent does, so the words say what to do rather than that the name is unknown.
67
+ */
68
+ chosenUnset?: readonly string[];
64
69
  }
65
70
  /** One column of a tenant's profile row, as a member may read it. */
66
71
  export type TenantValue = string | number | boolean | null;
@@ -113,10 +113,13 @@ export function chosenOperand(ctx, word, quoted) {
113
113
  return me.once
114
114
  ? { error: `${word} is a choice of one request's person, and this query is read once for every request, so it cannot ask it.` }
115
115
  : { value: word };
116
+ const personLess = {
117
+ error: `${word} is the person's App choice, and nothing here runs for a person at a desk (a job, a workflow step, an API call): take it as input, or quote it ("${word}") to mean the text.`,
118
+ };
116
119
  if (!me?.chosen)
117
- return {
118
- error: `${word} is the person's App choice, and nothing here runs for a person at a desk (a job, a workflow step, an API call): take it as input, or quote it ("${word}") to mean the text.`,
119
- };
120
+ return personLess;
121
+ if (!Object.hasOwn(me.chosen, match[1]) && me.chosenUnset?.includes(match[1]))
122
+ return personLess;
120
123
  if (!Object.hasOwn(me.chosen, match[1]))
121
124
  return { error: `${word} names no choice: the App's choose declares ${Object.keys(me.chosen).join(', ') || 'none'}.` };
122
125
  const value = me.chosen[match[1]];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mistriwork/engine",
3
- "version": "1.0.0-rc.80",
3
+ "version": "1.0.0-rc.82",
4
4
  "description": "The engine both products stand on: the adapters (SQLite, Postgres), the catalog, the query language, keyset pages, the HTTP kit and the shared rules. No React.",
5
5
  "homepage": "https://mistriwork.dev",
6
6
  "bugs": {