@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.
package/README.md CHANGED
@@ -21,7 +21,10 @@ The package root exports nothing; each entrypoint is for a different side of a D
21
21
  | Entrypoint | For | Holds |
22
22
  |---|---|---|
23
23
  | `@holistics/anfra-sdk/app` | a Data App definition, in its frame | the **app runtime** (`createApp`, queries, controls, cross-filters, results and state), and the **frame bootstrap** that provisions it from its host |
24
- | `@holistics/anfra-sdk/host` | the page hosting a Data App | provisioning its frame, mounting it sandboxed, and answering its calls over the **bridge** |
24
+ | `@holistics/anfra-sdk/host` | the page hosting a Data App | the **host runtime**: the plugins and middleware a page installs, provisioning each frame, mounting it sandboxed, and answering its calls over the **bridge**; `definePlugin` for a plugin of your own |
25
+ | `@holistics/anfra-sdk/plugins` | the page hosting a Data App | anfra's own **plugins**: `core` (queries), `export`, `lineage`, `render`, `inspect` |
26
+ | `@holistics/anfra-sdk/build` | a plugin's build, in Node | `buildFrameHalf`: a plugin's frame half as the script a host provisions |
27
+ | `@holistics/anfra-sdk/testing` | a plugin's tests | `testFrame`: a frame half run on the real kernel against a stand-in host |
25
28
  | `@holistics/anfra-sdk/api` | the host's page, scripts, tests | a client for the core API, generated from anfra's spec, and a **Backend** over it |
26
29
  | `@holistics/anfra-sdk/common` | all of the above | the contract they share: types, error classes, the bridge's messages |
27
30
 
@@ -44,10 +47,15 @@ app.subscribe(() => render(byRegion.result));
44
47
  await app.execute();
45
48
  ```
46
49
 
47
- A reader can download a query's whole result, with the inputs its result on screen was run with:
50
+ Everything beyond queries comes from a **plugin** the host installed, which adds its API under its
51
+ own name (`query.export`, `Anfra.render`, `Anfra.lineage`). Where the host didn't install it, the
52
+ member isn't there: check `Anfra.plugins.has(name)` before using an optional one.
53
+
54
+ A reader can download a query's whole result, with the inputs its result on screen was run with
55
+ (the `export` plugin):
48
56
 
49
57
  ```js
50
- if (Anfra.features.export && Anfra.user.permissions.canExportData) {
58
+ if (Anfra.plugins.has('export') && Anfra.user.permissions.canExportData) {
51
59
  button.onclick = () => byRegion.export().catch((err) => showError(err.message));
52
60
  }
53
61
  ```
@@ -55,22 +63,24 @@ if (Anfra.features.export && Anfra.user.permissions.canExportData) {
55
63
  `export()` resolves once the file is made and the host has started saving it, with its link and
56
64
  `rowCount`. The file is a CSV named after the app and the query unless you pass `filename`.
57
65
 
58
- A reader can download a picture of the app, or of one block or container, as they see it:
66
+ A reader can download a picture of the app, or of one block or container, as they see it (the
67
+ `render` plugin):
59
68
 
60
69
  ```js
61
70
  await Anfra.render({ block: 'trend', format: 'png' }); // or 'pdf'; no node: the whole app
62
71
  ```
63
72
 
64
- It works only where the host turned rendering on (`mountDataApp({ render: true })`; `anfra serve`
65
- does). Check `Anfra.features.render` and `Anfra.user.permissions.canExportData` before offering it, and
73
+ It works only where the host installed `render` (`anfra serve` does). Check
74
+ `Anfra.plugins.has('render')` and `Anfra.user.permissions.canExportData` before offering it, and
66
75
  mark what shouldn't be drawn (the download button itself) `data-anfra-render="skip"`. Serve images
67
76
  and web fonts with CORS: an image without it is left blank (listed in the result's `omitted`), a
68
77
  font without it falls back.
69
78
 
70
79
  `Anfra.datasets` describes what there is to query. For TypeScript, `@holistics/anfra-sdk/ambient` declares the
71
- global.
80
+ global. A plugin's members are typed as optional: check the member itself (`if (Anfra.render)`,
81
+ `query.export?.()`), which narrows it.
72
82
 
73
- `Anfra.lineage({ parent: 'orders', child: 'amount' })` traces fields through the repo's catalog, each named
83
+ `Anfra.lineage({ parent: 'orders', child: 'amount' })` (the `lineage` plugin) traces fields through the repo's catalog, each named
74
84
  by its parent and its name in it: what each is built from, as a diagram to draw (`results[i].diagram`), or
75
85
  why it could not be (`results[i].error`). It reads the catalog the host last ingested (`anfra ingest`).
76
86
 
@@ -97,33 +107,83 @@ inspects, and never thrown over: a wrong name or a block inside a block is repor
97
107
 
98
108
  ```ts
99
109
  import { coreApiBackend, coreClient, loadDatasets } from '@holistics/anfra-sdk/api';
100
- import { mountDataApp } from '@holistics/anfra-sdk/host';
110
+ import { createHost } from '@holistics/anfra-sdk/host';
111
+ import {
112
+ corePlugin, exportPlugin, inspectPlugin, lineagePlugin, renderPlugin,
113
+ } from '@holistics/anfra-sdk/plugins';
101
114
 
102
115
  const client = coreClient('/api'); // the core API, on whichever server serves it
103
116
  const { datasets } = await loadDatasets(client); // every dataset, through core.show
117
+ const backend = coreApiBackend(client, { datasets }); // queries, suggestions, exports and lineage, as core ops
118
+
119
+ const host = createHost({ // one per page
120
+ plugins: [
121
+ corePlugin(backend), // required: runs the queries
122
+ exportPlugin(backend), // query.export()
123
+ lineagePlugin(backend), // Anfra.lineage()
124
+ renderPlugin(), // Anfra.render(): the host saves PNGs and PDFs the frame makes
125
+ inspectPlugin(), // what an inspect panel needs
126
+ ],
127
+ });
104
128
 
105
- const mounted = mountDataApp({
129
+ const mounted = host.mount({
106
130
  container: document.querySelector('#data-app')!,
107
131
  definition: await (await fetch('/appserve/files/sales/overview.html')).text(),
108
132
  baseHref: '/appserve/files/sales/', // where its relative URLs resolve
109
133
  datasets,
110
134
  user,
111
- backend: coreApiBackend(client, { datasets }), // queries, field suggestions and exports, as core ops; lineage as core.lineage
112
- render: true, // let it save PNGs and PDFs of itself (off by default)
113
135
  });
114
- // mounted.setInspecting(true) to receive inspection snapshots; mounted.render({ format: 'pdf' }) to
115
- // save a picture of the app (needs render: true); mounted.unmount() when done.
136
+ await mounted.call('render.draw', { format: 'pdf' }); // save a picture of the app
137
+ mounted.on('inspect.snapshot', ({ apps, structure }) => …);
138
+ await mounted.call('inspect.watch', true); // start the snapshots
139
+ mounted.unmount(); // when done
116
140
  ```
117
141
 
118
- `mountDataApp` puts the definition in an iframe with `sandbox="allow-scripts"`, as its `srcdoc`,
119
- with the frame bootstrap and the provisioned data ahead of it. The bootstrap's Backend forwards each call to the host over postMessage; the host answers it with the Backend it was given. An export's file is saved from the host's page, by following its link: the frame can't download. A Backend without `exportQuery` makes a host that can't export, and its frames see `Anfra.features.export` false; likewise a Backend without `lineage`, and `Anfra.features.lineage`.
142
+ `host.mount` puts the definition in an iframe with `sandbox="allow-scripts"`, as its `srcdoc`, with
143
+ the frame's kernel, the provisioned data and each plugin's frame setup ahead of it. Every call the
144
+ frame makes, and every call a plugin makes of another, goes through the host's one pipeline to the
145
+ plugin that answers it. An export's file is saved from the host's page, by following its link: the
146
+ frame can't download.
147
+
148
+ ### Middleware
149
+
150
+ Middleware wraps calls by name (`*`, `core.*`, `core.submitQuery`), in order of `stage`, then name:
151
+
152
+ ```ts
153
+ createHost({
154
+ plugins,
155
+ middleware: [{
156
+ name: 'log',
157
+ match: '*',
158
+ handle: async (request, call, next) => {
159
+ const started = Date.now();
160
+ try { return await next(request); } finally { console.log(call.name, call.caller, Date.now() - started); }
161
+ },
162
+ }],
163
+ });
164
+ ```
165
+
166
+ It may change the request, answer in the plugin's place (not calling `next`), refuse (throwing), or
167
+ change the answer. `call.caller` says who made the call: the frame, the host, or another plugin;
168
+ `call.origin` who started it, through any plugins between (`call.chain`). Count what the frame
169
+ started by `origin`: a query a plugin runs for the frame's request has the plugin as its `caller`.
170
+ A handler forwarding a call or request the frame chose makes it with `{ asOrigin: true }`, so it
171
+ is refused if the frame couldn't make it itself.
172
+
173
+ ### A plugin of your own
174
+
175
+ A plugin adds a feature to the Data Apps on your host, such as a call from the frame to your
176
+ server. Make one with `definePlugin`, build its frame half with `@holistics/anfra-sdk/build`, and
177
+ test it with `@holistics/anfra-sdk/testing`. The guide, with a worked example, is
178
+ [docs/usage/sdk-plugins.md](../../docs/usage/sdk-plugins.md); the design is
179
+ [docs/designs/sdk-plugins.md](../../docs/designs/sdk-plugins.md).
120
180
 
121
181
  ## The boundary
122
182
 
123
183
  A Data App definition can come from any author, often an agent, which may not think of security while building an analysis. So the SDK guards it for its author, and makes the safe way the easy one:
124
184
 
125
185
  - Its frame has an opaque origin. Its own requests to the API are cross-origin, which `anfra serve` refuses, and carry no credentials on a platform that has them.
126
- - The bridge answers only the Backend's methods, `submitQuery`, `fieldSuggestions`, `exportQuery` and the optional `lineage`, and the two a rendering needs, the renderer's code and saving its file. Nothing else of the API is reachable from a frame.
186
+ - The frame reaches only the calls its host's plugins let it make (`frame: true`); every other call is the host's. Nothing else of the API is reachable from a frame.
127
187
  - `@holistics/anfra-sdk/app` imports only `common`: the script injected into a frame carries no API client. A test on the built frame script holds this.
128
188
 
129
189
  ## Development
@@ -131,7 +191,7 @@ A Data App definition can come from any author, often an agent, which may not th
131
191
  From the repository root, after `pnpm install`:
132
192
 
133
193
  ```sh
134
- pnpm build:sdk # dist/{common,app,host,api}.js, with the frame script built into host
194
+ pnpm build:sdk # dist/{common,app,host,plugins,api,build,testing}.js, with the frame script built into host and the plugins' into plugins
135
195
  pnpm dev:sdk # the same, rebuilt on change
136
196
  pnpm test:sdk # vitest
137
197
  ```
package/dist/api.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Client } from 'openapi-fetch';
2
- import { aj as paths, D as DatasetDescriptor, B as Backend, f as BackendExportRequest, ak as components, i as BackendQueryRequest } from './types-DznT0m7F.js';
2
+ import { aj as paths, D as DatasetDescriptor, B as Backend, f as BackendExportRequest, ak as components, i as BackendQueryRequest } from './types-BTyDyld7.js';
3
3
  import { D as DataAppError } from './errors-CKvqVaqU.js';
4
4
 
5
5
  /** The core API, typed from anfra core's spec (`api/openapi.yaml`, generated into `schema.d.ts`). */
package/dist/api.js CHANGED
@@ -2,7 +2,8 @@ import {
2
2
  PermissionError,
3
3
  QueryError,
4
4
  TransportError
5
- } from "./chunk-KBXITYQK.js";
5
+ } from "./chunk-KCTLO3CO.js";
6
+ import "./chunk-PKBMQBKP.js";
6
7
 
7
8
  // src/api/client.ts
8
9
  import createClient from "openapi-fetch";