@holistics/anfra-sdk 0.10.2 → 0.11.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.
@@ -0,0 +1,6 @@
1
+ // package.json
2
+ var version = "0.11.0";
3
+
4
+ export {
5
+ version
6
+ };
@@ -1,6 +1,6 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
1
+ import {
2
+ __publicField
3
+ } from "./chunk-PKBMQBKP.js";
4
4
 
5
5
  // src/common/errors.ts
6
6
  var DataAppError = class extends Error {
@@ -33,7 +33,6 @@ var TransportError = class extends DataAppError {
33
33
  };
34
34
 
35
35
  export {
36
- __publicField,
37
36
  DataAppError,
38
37
  ValidationError,
39
38
  QueryError,
@@ -0,0 +1,7 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+
5
+ export {
6
+ __publicField
7
+ };
@@ -0,0 +1,24 @@
1
+ // src/host/plugin.ts
2
+ var NAME = /^[a-z][a-z0-9-]*$/;
3
+ var RESERVED_NAMES = /* @__PURE__ */ new Set(["anfra", "plugins"]);
4
+ var MEMBER = /^[A-Za-z][A-Za-z0-9]*$/;
5
+ function checkPlugin(plugin) {
6
+ if (typeof plugin?.name !== "string" || !NAME.test(plugin.name)) {
7
+ throw new Error(`A plugin's name is lowercase letters, digits and dashes, starting with a letter: '${String(plugin?.name)}' is not.`);
8
+ }
9
+ if (RESERVED_NAMES.has(plugin.name)) throw new Error(`'${plugin.name}' is reserved for the SDK: name the plugin something else.`);
10
+ for (const [kind, names] of [["call", Object.keys(plugin.host?.calls ?? {})], ["event", Object.keys(plugin.host?.events ?? {})]]) {
11
+ for (const name of names) {
12
+ if (!MEMBER.test(name)) throw new Error(`Plugin '${plugin.name}': '${name}' is not ${kind === "call" ? "a call" : "an event"} name (letters and digits).`);
13
+ }
14
+ }
15
+ }
16
+ function definePlugin(plugin) {
17
+ checkPlugin(plugin);
18
+ return plugin;
19
+ }
20
+
21
+ export {
22
+ checkPlugin,
23
+ definePlugin
24
+ };
package/dist/common.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export { D as DataAppError, P as PermissionError, Q as QueryError, T as TransportError, V as ValidationError } from './errors-CKvqVaqU.js';
2
- export { A as Aggregation, a as AppDeclaration, b as AppJson, c as ArrowType, B as Backend, d as BackendCondition, e as BackendDateDrill, f as BackendExportRequest, g as BackendFilter, h as BackendLineageRequest, i as BackendQueryRequest, j as BackendQueryResult, k as BackendSort, C as ColumnMeta, l as Condition, m as ConditionValue, n as ControlMappingJson, o as CrossFilterJson, D as DatasetDescriptor, p as DateDrillDeclaration, q as DateGrain, r as DateTransformation, E as EntityKind, s as EntityRef, t as EntityUsage, u as ExecuteOptions, v as ExecuteSummary, w as ExportFormat, x as ExportFormatOptions, y as ExportOptions, z as ExportResult, F as FILTER_AGGREGATIONS, G as FieldLineage, H as FieldSuggestionsRequest, I as FilterAggregation, J as FilterDeclaration, K as FilterValueType, L as InspectedApp, M as InspectedControl, N as InspectedError, O as InspectedQuery, P as InspectedSelection, Q as InspectedStructure, R as LineageCard, S as LineageDiagram, T as LineageNode, U as LineageOptions, V as LineageResult, W as LineageTarget, X as Listener, Y as LocateKind, Z as LocateTarget, _ as MappingJson, $ as Operator, a0 as QueryDebug, a1 as QueryDeclaration, a2 as QueryInput, a3 as QueryResult, a4 as QuerySort, a5 as QueryState, a6 as RenderFormat, a7 as RenderRequest, a8 as Rendering, a9 as Row, aa as SdkEnvironment, ab as SdkFeatures, ac as Selection, ad as SelectionCondition, ae as SemanticType, af as StructureKind, ag as StructureNode, ah as Unsubscribe, ai as User } from './types-DznT0m7F.js';
3
- export { B as BridgeError, a as BridgeMethod, F as FrameMessage, H as HostMessage, P as PROVISION_ELEMENT_ID, b as Provision } from './bridge-DUKdREoK.js';
2
+ export { A as Aggregation, a as AppDeclaration, b as AppJson, c as ArrowType, B as Backend, d as BackendCondition, e as BackendDateDrill, f as BackendExportRequest, g as BackendFilter, h as BackendLineageRequest, i as BackendQueryRequest, j as BackendQueryResult, k as BackendSort, C as ColumnMeta, l as Condition, m as ConditionValue, n as ControlMappingJson, o as CrossFilterJson, D as DatasetDescriptor, p as DateDrillDeclaration, q as DateGrain, r as DateTransformation, E as EntityKind, s as EntityRef, t as EntityUsage, u as ExecuteOptions, v as ExecuteSummary, w as ExportFormat, x as ExportFormatOptions, y as ExportOptions, z as ExportResult, F as FILTER_AGGREGATIONS, G as FieldLineage, H as FieldSuggestionsRequest, I as FilterAggregation, J as FilterDeclaration, K as FilterValueType, L as InspectedApp, M as InspectedControl, N as InspectedError, O as InspectedQuery, P as InspectedSelection, Q as InspectedStructure, R as LineageCard, S as LineageDiagram, T as LineageNode, U as LineageOptions, V as LineageResult, W as LineageTarget, X as Listener, Y as LocateKind, Z as LocateTarget, _ as MappingJson, $ as Operator, a0 as QueryDebug, a1 as QueryDeclaration, a2 as QueryInput, a3 as QueryResult, a4 as QuerySort, a5 as QueryState, a6 as RenderFormat, a7 as RenderRequest, a8 as Rendering, a9 as Row, aa as SdkEnvironment, ab as SdkFeatures, ac as Selection, ad as SelectionCondition, ae as SemanticType, af as StructureKind, ag as StructureNode, ah as Unsubscribe, ai as User } from './types-BTyDyld7.js';
3
+ export { B as BridgeError, a as BridgeMessage, F as FrameMessage, H as HostMessage, P as PROVISION_ELEMENT_ID, b as PluginInfo, c as Provision } from './bridge-Byq62yS8.js';
package/dist/common.js CHANGED
@@ -1,6 +1,7 @@
1
+ import "./chunk-52LXBYQM.js";
1
2
  import {
2
3
  FILTER_AGGREGATIONS
3
- } from "./chunk-H4XI4B7G.js";
4
+ } from "./chunk-XIUFHRZE.js";
4
5
  import {
5
6
  PROVISION_ELEMENT_ID
6
7
  } from "./chunk-33B2D4U5.js";
@@ -10,7 +11,8 @@ import {
10
11
  QueryError,
11
12
  TransportError,
12
13
  ValidationError
13
- } from "./chunk-KBXITYQK.js";
14
+ } from "./chunk-KCTLO3CO.js";
15
+ import "./chunk-PKBMQBKP.js";
14
16
  export {
15
17
  DataAppError,
16
18
  FILTER_AGGREGATIONS,
package/dist/host.d.ts CHANGED
@@ -1,46 +1,17 @@
1
- import { L as InspectedApp, Q as InspectedStructure, Z as LocateTarget, a7 as RenderRequest, a8 as Rendering, B as Backend, D as DatasetDescriptor, ai as User } from './types-DznT0m7F.js';
2
- import { b as Provision } from './bridge-DUKdREoK.js';
1
+ import { b as PluginInfo, c as Provision } from './bridge-Byq62yS8.js';
2
+ import { D as DatasetDescriptor, ai as User } from './types-BTyDyld7.js';
3
+ import { C as CallOptions, P as Plugin, M as Middleware } from './plugin-C5cihcQD.js';
4
+ export { a as CallContext, b as CallDefinition, c as CallHandler, d as Caller, E as EventCheck, F as FrameHalf, H as HandlerCallOptions, e as HandlerContext, f as HostHalf, g as MountContext, h as definePlugin } from './plugin-C5cihcQD.js';
3
5
 
4
- interface BridgeHandle {
5
- /** Ask the frame to post (or stop posting) inspection snapshots; survives the frame reloading. */
6
- setInspecting: (open: boolean) => void;
7
- /** Show a node of the last snapshot's structure on the page, or nothing (null). */
8
- highlight: (node: string | null) => void;
9
- /**
10
- * Scroll a node, or an entity wherever it is drawn, into view and hold the highlight on it. Fire
11
- * and forget: the frame answers nothing, and does nothing for what is not on the page.
12
- */
13
- locate: (target: LocateTarget) => void;
14
- /** Start or stop pick mode in the frame: pointing outlines marked elements, a click reports one. */
15
- setPicking: (on: boolean) => void;
16
- /**
17
- * Render the app, or one block or container, once its running queries settle, and save the file
18
- * for the reader. Rejects with a `ValidationError` when the frame can't, or rendering is off.
19
- */
20
- render: (request: RenderRequest) => Promise<Rendering>;
21
- stop: () => void;
22
- }
23
- interface BridgeOptions {
24
- /** Whether the frame may render: off unless true, and off, its asks are refused and `render` rejects. */
25
- render?: boolean;
26
- /** The renderer's code (`anfra-sdk:renderer`), sent to the frame on its first rendering. */
27
- renderer?: string;
28
- }
29
- interface BridgeCallbacks {
30
- /** The frame's inspection snapshot: its apps and its structure. */
31
- onInspect?: (apps: InspectedApp[], structure: InspectedStructure) => void;
32
- /** In pick mode, the reader chose a node (its key), or left the mode (null). */
33
- onPick?: (node: string | null) => void;
6
+ interface HostOptions {
7
+ /** The plugins this page's Data Apps get. `core` is required: the app model runs its queries with it. */
8
+ plugins: readonly Plugin[];
9
+ /** Middleware around calls, beside what plugins bring. */
10
+ middleware?: readonly Middleware[];
11
+ /** The frame script; the one built into this package unless given (tests). */
12
+ frameScript?: string;
34
13
  }
35
- /**
36
- * The bridge, the host's side: answer a Data App frame's Backend calls with `backend`, abort one on
37
- * the frame's cancel, and relay its inspection snapshots, until `stop`. Only messages from this
38
- * frame are served (a sandboxed frame has an opaque origin, so its window is what identifies it),
39
- * and only the Backend's own methods: nothing else of the API is reachable from a frame.
40
- */
41
- declare function serveBridge(frame: HTMLIFrameElement, backend: Backend, callbacks?: BridgeCallbacks, options?: BridgeOptions): BridgeHandle;
42
-
43
- interface MountOptions extends BridgeCallbacks {
14
+ interface MountOptions {
44
15
  /** Where the frame goes: it is appended to this element. */
45
16
  container: HTMLElement;
46
17
  /** The Data App definition: its HTML, as its author wrote it. */
@@ -49,45 +20,63 @@ interface MountOptions extends BridgeCallbacks {
49
20
  baseHref: string;
50
21
  datasets: Record<string, DatasetDescriptor>;
51
22
  user: User;
52
- /** What answers the frame's queries: `coreApiBackend` from `@holistics/anfra-sdk/api`, on any host. */
53
- backend: Backend;
54
- /** The frame script; the one built into this package unless given (tests). */
55
- frameScript?: string;
56
- /**
57
- * Whether the frame may render itself as a PNG or PDF (`Anfra.render()`, `render()` here). Off
58
- * unless turned on: rendering has the host save a file the frame made, so a host opts in to it
59
- * (docs/designs/data-apps.md, Renderings).
60
- */
61
- render?: boolean;
62
- /** The renderer's script; the one built into this package unless given (tests). */
63
- renderer?: string;
64
23
  title?: string;
65
24
  }
66
- interface MountedDataApp extends Pick<BridgeHandle, 'setInspecting' | 'highlight' | 'locate' | 'setPicking' | 'render'> {
25
+ interface MountedDataApp {
67
26
  frame: HTMLIFrameElement;
27
+ /**
28
+ * Make a call about this frame: a host half's (through the pipeline), or, when no host half
29
+ * answers the name, a frame half's, in the frame.
30
+ */
31
+ call: <T = unknown>(name: string, request?: unknown, options?: CallOptions) => Promise<T>;
32
+ /** Send the frame an event. */
33
+ emit: (name: string, payload?: unknown) => void;
34
+ /** Listen to the frame's events, by full name. Returns the function that stops listening. */
35
+ on: (name: string, listener: (payload: any) => void) => () => void;
68
36
  /** Stop answering the frame, and remove it. */
69
37
  unmount: () => void;
70
38
  }
71
- /**
72
- * Mount a Data App: a sandboxed frame (scripts only, an opaque origin, so its own requests reach no
73
- * API) whose document is the definition with the SDK provisioned ahead of it, answered over the
74
- * bridge by `backend`. The bridge is served before the document loads, so no call is missed.
75
- */
76
- declare function mountDataApp(options: MountOptions): MountedDataApp;
39
+ interface Host {
40
+ /** The installed plugins, in install order. */
41
+ readonly plugins: readonly PluginInfo[];
42
+ /** Make a call as the host page, about no frame. */
43
+ call: <T = unknown>(name: string, request?: unknown, options?: CallOptions) => Promise<T>;
44
+ /**
45
+ * Mount a Data App: a sandboxed frame (scripts only, an opaque origin, so its own requests reach
46
+ * no API) whose document is the definition with the SDK and the plugins' frame halves provisioned
47
+ * ahead of it. The bridge is served before the document loads, so no call is missed.
48
+ */
49
+ mount: (options: MountOptions) => MountedDataApp;
50
+ }
51
+ declare function createHost(options: HostOptions): Host;
77
52
 
78
53
  interface ProvisionOptions {
79
54
  /** Where the definition's relative URLs resolve: the directory it was read from. */
80
55
  baseHref: string;
81
56
  provision: Provision;
82
- /** The frame script (`@holistics/anfra-sdk/app`'s IIFE), run before any of the definition's own. */
57
+ /** The frame script (`@holistics/anfra-sdk/app`'s kernel, as `__anfraKernel`), run before any of the definition's own. */
83
58
  frameScript: string;
59
+ /** The installed plugins' frame setups (each assigning `__anfraPlugin`), run after the kernel, in this order. */
60
+ setups?: readonly {
61
+ name: string;
62
+ setup: string;
63
+ }[];
84
64
  }
65
+ /**
66
+ * The one script a frame starts with: the kernel, then each plugin's setup, then the bootstrap,
67
+ * all inside one function so that neither the kernel nor a setup is left on the frame's window,
68
+ * where the definition would find it.
69
+ */
70
+ declare function composeFrameScript(frameScript: string, setups?: readonly {
71
+ name: string;
72
+ setup: string;
73
+ }[]): string;
85
74
  /**
86
75
  * A Data App's frame document: its definition, with its base URL, the provision data and the frame
87
- * script added at the top of its <head> (after <meta charset> when there is one, so the encoding is
76
+ * script (the kernel and the plugins' setups) added at the top of its <head> (after <meta charset> when there is one, so the encoding is
88
77
  * still declared first), so `Anfra` exists before the definition's first script runs. The
89
78
  * definition itself never mentions the SDK.
90
79
  */
91
80
  declare function provisionDocument(definition: string, options: ProvisionOptions): string;
92
81
 
93
- export { type BridgeCallbacks, type BridgeHandle, type BridgeOptions, type MountOptions, type MountedDataApp, type ProvisionOptions, mountDataApp, provisionDocument, serveBridge };
82
+ export { CallOptions, type Host, type HostOptions, Middleware, type MountOptions, type MountedDataApp, Plugin, type ProvisionOptions, composeFrameScript, createHost, provisionDocument };