@holistics/anfra-sdk 0.16.0 → 0.18.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.
Files changed (38) hide show
  1. package/README.md +2 -1
  2. package/dist/ambient.d.ts +2 -1
  3. package/dist/api.d.ts +11 -5
  4. package/dist/api.js +40 -30
  5. package/dist/app.d.ts +5 -5
  6. package/dist/app.js +22 -12
  7. package/dist/{bootstrap-hLhpijIb.d.ts → bootstrap-q1J7GUxa.d.ts} +55 -18
  8. package/dist/build.d.ts +2 -2
  9. package/dist/{chunk-THYCARLT.js → chunk-2O2WVQXO.js} +102 -30
  10. package/dist/chunk-32566ZNE.js +1278 -0
  11. package/dist/{chunk-S6XLP6U4.js → chunk-3ZWUCECU.js} +45 -20
  12. package/dist/{chunk-KFD6CHLI.js → chunk-ACKFOANJ.js} +2 -0
  13. package/dist/{chunk-SKSKC747.js → chunk-B6URV5AO.js} +1 -1
  14. package/dist/{chunk-CYZQ7ZTJ.js → chunk-C26OVN2S.js} +6 -1
  15. package/dist/{chunk-PTNYQOEY.js → chunk-NS7HJHNK.js} +1 -1
  16. package/dist/chunk-SWNVCTT6.js +161 -0
  17. package/dist/common.d.ts +3 -3
  18. package/dist/common.js +19 -9
  19. package/dist/{data-BGX3V42T.js → data-TDUQTG3P.js} +8 -6
  20. package/dist/{declaration-ZL5B676C.js → declaration-RJFU4ZLJ.js} +2 -2
  21. package/dist/errors-IhdhKO-6.d.ts +119 -0
  22. package/dist/host.d.ts +2 -2
  23. package/dist/host.js +17 -17
  24. package/dist/{menu-BFLb7dVq.d.ts → menu-CRAFaQ8s.d.ts} +21 -5
  25. package/dist/{overview-7YU7JW6Q.js → overview-AZCX6SY6.js} +23 -23
  26. package/dist/{plugin-B7SpJLBm.d.ts → plugin-BdLabbah.d.ts} +2 -2
  27. package/dist/plugins.d.ts +11 -2
  28. package/dist/plugins.js +38 -21
  29. package/dist/{queries-QFN4DI25.js → queries-5TTPOB7R.js} +32 -30
  30. package/dist/{structure-Z2QK5MGH.js → structure-MTLCRTJ4.js} +11 -9
  31. package/dist/{tab-LSITVMKV.js → tab-XL2HS5W4.js} +20 -18
  32. package/dist/testing.d.ts +1 -1
  33. package/dist/testing.js +4 -4
  34. package/dist/{types-CKytl8ds.d.ts → types-C_i7V0DA.d.ts} +46 -14
  35. package/package.json +2 -1
  36. package/dist/chunk-F6B3EGFV.js +0 -750
  37. package/dist/chunk-KCTLO3CO.js +0 -41
  38. package/dist/errors-CKvqVaqU.d.ts +0 -30
@@ -6,7 +6,7 @@ import {
6
6
  eventShortcut,
7
7
  isTyping,
8
8
  normalizeShortcut
9
- } from "./chunk-S6XLP6U4.js";
9
+ } from "./chunk-3ZWUCECU.js";
10
10
  import {
11
11
  DEFAULT_TOKENS,
12
12
  PROVISION_ELEMENT_ID,
@@ -15,14 +15,18 @@ import {
15
15
  } from "./chunk-RXPNL7UR.js";
16
16
  import {
17
17
  version
18
- } from "./chunk-SKSKC747.js";
18
+ } from "./chunk-B6URV5AO.js";
19
19
  import {
20
20
  DataAppError,
21
- PermissionError,
22
- QueryError,
23
- TransportError,
24
- ValidationError
25
- } from "./chunk-KCTLO3CO.js";
21
+ ValidationError,
22
+ callFailed,
23
+ connectionFailed,
24
+ forbidden,
25
+ fromHttpStatus,
26
+ hasErrorCode,
27
+ invalidRequest,
28
+ isDataAppError
29
+ } from "./chunk-SWNVCTT6.js";
26
30
  import {
27
31
  __publicField
28
32
  } from "./chunk-PKBMQBKP.js";
@@ -45,7 +49,13 @@ var Observable = class {
45
49
  * entity's changes through one subscription. Subclasses call the protected `notify` instead.
46
50
  */
47
51
  notifyChange() {
48
- this.listeners.forEach((listener) => listener());
52
+ this.listeners.forEach((listener) => {
53
+ try {
54
+ listener();
55
+ } catch (err) {
56
+ console.error("anfra: a subscribe() listener threw; the queries carry on.", err);
57
+ }
58
+ });
49
59
  this.parent?.notifyChange();
50
60
  }
51
61
  notify() {
@@ -189,11 +199,12 @@ var Query = class extends Observable {
189
199
  * Opens the menu on these rows of this query: the items plugins and the host offer for a point
190
200
  * of data, such as "View underlying data". Call it from your chart's own right-click handler,
191
201
  * with the event and the rows under the pointer, as for `select`; `fields` narrows the dimensions
192
- * that take part. Whether a menu opens: when one does, the event's default (the browser's menu)
193
- * is prevented, and when none would (no plugin offers one), nothing happens.
202
+ * that take part, and `measure` names the measure column the point shows (a chart's series), for
203
+ * items that are about one measure. Whether a menu opens: when one does, the event's default (the
204
+ * browser's menu) is prevented, and when none would (no plugin offers one), nothing happens.
194
205
  */
195
206
  menu(event, rows, options = {}) {
196
- return this.app.menu(this, event, Array.isArray(rows) ? rows : [rows], options.fields);
207
+ return this.app.menu(this, event, Array.isArray(rows) ? rows : [rows], options.fields, options.measure);
197
208
  }
198
209
  /**
199
210
  * Cross-filters this query's targets by the rows the reader picked.
@@ -719,8 +730,25 @@ function installContentUi(kernel) {
719
730
  }
720
731
  if (!applies) continue;
721
732
  const key = `frame:${item.full}:${target.kind}`;
722
- entries.push({ id: key, label: item.label });
723
- run.set(key, () => item.run(target));
733
+ if (!item.entries) {
734
+ entries.push({ id: key, label: item.label });
735
+ run.set(key, () => item.run(target));
736
+ continue;
737
+ }
738
+ let own = [];
739
+ try {
740
+ own = item.entries(target) ?? [];
741
+ } catch {
742
+ own = [];
743
+ }
744
+ own.forEach((entry, index) => {
745
+ if (typeof entry?.label !== "string") return;
746
+ const disabled = typeof entry.disabled === "string" && entry.disabled ? entry.disabled : void 0;
747
+ if (!disabled && typeof entry.run !== "function") return;
748
+ const entryKey = `${key}:${index}`;
749
+ entries.push({ id: entryKey, label: entry.label, ...disabled ? { disabled } : {} });
750
+ if (!disabled) run.set(entryKey, entry.run);
751
+ });
724
752
  }
725
753
  return { entries, run };
726
754
  }
@@ -1442,9 +1470,12 @@ function superseded(query, controller) {
1442
1470
  function inspectError(error) {
1443
1471
  return {
1444
1472
  name: error.name,
1473
+ code: error.code,
1474
+ scope: error.scope,
1445
1475
  message: error.message,
1446
1476
  ...error.entity ? { entity: error.entity } : {},
1447
1477
  ...typeof error.status === "number" ? { status: error.status } : {},
1478
+ ...error.requestId ? { requestId: error.requestId } : {},
1448
1479
  ...error.stack ? { stack: error.stack } : {}
1449
1480
  };
1450
1481
  }
@@ -1493,17 +1524,16 @@ function selectionKey(selection) {
1493
1524
  expression: selection.expression
1494
1525
  });
1495
1526
  }
1496
- function toDataAppError(err, entity) {
1497
- if (err instanceof DataAppError) return err;
1498
- return new QueryError(err?.message ?? "The query failed.", entity, err);
1499
- }
1500
- function toExportError(err, entity) {
1501
- if (err instanceof DataAppError && err.entity) return err;
1502
- const message = err?.message || "The export failed.";
1503
- if (err instanceof PermissionError) return new PermissionError(message, entity);
1504
- if (err instanceof TransportError) return new TransportError(message, err.status, entity);
1505
- if (err instanceof ValidationError) return new ValidationError(message, entity);
1506
- return new QueryError(message, entity, err);
1527
+ function toDataAppError(err, entity, failed = "The query failed.") {
1528
+ if (isDataAppError(err)) return err.about(entity);
1529
+ console.error(`anfra: '${entity}' failed; the app is told only that it failed.`, err);
1530
+ return new DataAppError({
1531
+ code: "call_failed",
1532
+ scope: "server",
1533
+ message: failed,
1534
+ entity,
1535
+ cause: err
1536
+ });
1507
1537
  }
1508
1538
  var App = class extends Observable {
1509
1539
  constructor(declaration, datasets, backend, features, extensions) {
@@ -1732,10 +1762,24 @@ var App = class extends Observable {
1732
1762
  });
1733
1763
  return byField;
1734
1764
  }
1735
- menu(query, event, rows, fields) {
1765
+ menu(query, event, rows, fields, measure) {
1736
1766
  if (this._queries.get(query.name) !== query) {
1737
1767
  throw new ValidationError(`Query '${query.name}' belongs to a different app.`, query.name);
1738
1768
  }
1769
+ if (measure !== void 0) {
1770
+ const columns = query.result?.columns ?? [];
1771
+ const column = columns.find((candidate) => candidate.name === measure);
1772
+ if (!column?.isMeasure) {
1773
+ throw new ValidationError(
1774
+ column ? `Query '${query.name}' can't open a menu on '${measure}' as its measure: it is a dimension.` : withSuggestion(
1775
+ `Query '${query.name}' has no measure '${measure}' in its last result.`,
1776
+ measure,
1777
+ columns.filter((candidate) => candidate.isMeasure).map((candidate) => candidate.name)
1778
+ ),
1779
+ query.name
1780
+ );
1781
+ }
1782
+ }
1739
1783
  const derived = deriveSelection(query, rows, fields, this.drillTransformations(query));
1740
1784
  return openRowsMenu(event, {
1741
1785
  kind: "rows",
@@ -1743,10 +1787,29 @@ var App = class extends Observable {
1743
1787
  query: query.name,
1744
1788
  rows: [...rows],
1745
1789
  fields: derived?.fields ? [...derived.fields] : [],
1790
+ ...measure !== void 0 ? { measure } : {},
1746
1791
  conditions: derived?.conditions ? [...derived.conditions] : [],
1747
1792
  ...derived?.expression ? { expression: derived.expression } : {}
1748
1793
  });
1749
1794
  }
1795
+ /**
1796
+ * @internal Backs the kernel's `queryRequest(query)`: what the query runs with now, from its applied
1797
+ * state, as `fetchPage` and `exportQuery` build theirs.
1798
+ */
1799
+ requestFor(query) {
1800
+ if (this._queries.get(query.name) !== query) {
1801
+ throw new ValidationError(`Query '${query.name}' belongs to a different app.`, query.name);
1802
+ }
1803
+ return buildQuery(
1804
+ query,
1805
+ this.mappingsTo(query.name),
1806
+ {
1807
+ conditions: this.selectionConditionsFor(query.name, "applied"),
1808
+ expression: this.selectionExpressionFor(query.name, "applied")
1809
+ },
1810
+ { timezone: this._timezone }
1811
+ );
1812
+ }
1750
1813
  select(query, rows, fields) {
1751
1814
  if (this._queries.get(query.name) !== query) {
1752
1815
  throw new ValidationError(`Query '${query.name}' belongs to a different app.`, query.name);
@@ -1884,7 +1947,7 @@ var App = class extends Observable {
1884
1947
  return await send(request, options.signal ?? new AbortController().signal);
1885
1948
  } catch (err) {
1886
1949
  if (isAbort(err)) throw err;
1887
- throw toExportError(err, query.name);
1950
+ throw toDataAppError(err, query.name, "The export failed.");
1888
1951
  }
1889
1952
  }
1890
1953
  /* ----------------------------------------------------------------
@@ -2175,9 +2238,13 @@ var BODY_GLOBAL = "__anfraBodyExports";
2175
2238
  var KERNEL_ERRORS = {
2176
2239
  DataAppError,
2177
2240
  ValidationError,
2178
- QueryError,
2179
- PermissionError,
2180
- TransportError
2241
+ invalidRequest,
2242
+ forbidden,
2243
+ connectionFailed,
2244
+ callFailed,
2245
+ fromHttpStatus,
2246
+ hasErrorCode,
2247
+ isDataAppError
2181
2248
  };
2182
2249
  function runBody(win, code) {
2183
2250
  const target = win;
@@ -2205,6 +2272,11 @@ function createKernel(plugin, shared, entities) {
2205
2272
  return overlayFor(win.document);
2206
2273
  },
2207
2274
  structure: () => buildStructure(win.document, sdk.apps),
2275
+ queryRequest(query) {
2276
+ const app = sdk.apps.find((candidate) => candidate.queries[query?.name] === query);
2277
+ if (!app) throw new ValidationError(`Query '${String(query?.name)}' belongs to no app.`, query?.name);
2278
+ return app.requestFor(query);
2279
+ },
2208
2280
  call: (name, request, signal) => endpoint.call(name, request ?? null, signal),
2209
2281
  emit: (name, payload) => endpoint.emit(`${plugin}.${name}`, payload ?? null),
2210
2282
  handle(name, handler) {
@@ -2396,7 +2468,7 @@ function bootstrap(win, setups = []) {
2396
2468
  const endpoint = createEndpoint(post, {
2397
2469
  answer: async (name, request, signal) => {
2398
2470
  const handler = handlers.get(name);
2399
- if (!handler) throw new TransportError(`Nothing in this frame answers '${name}'.`);
2471
+ if (!handler) throw invalidRequest(`Nothing in this frame answers '${name}'.`);
2400
2472
  return handler(request, signal);
2401
2473
  },
2402
2474
  event: (name, payload) => {