@lightdash/query-sdk 1.11.0 → 1.12.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.
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "1.11.0";
1
+ export declare const SDK_VERSION = "1.12.0";
@@ -1,2 +1,2 @@
1
1
  // Generated by scripts/generateSdkVersion.mjs (prebuild) — do not edit.
2
- export const SDK_VERSION = '1.11.0';
2
+ export const SDK_VERSION = '1.12.0';
package/dist/index.d.ts CHANGED
@@ -13,6 +13,6 @@ export type { InspectAvailableMessage, InspectSelectedMessage, } from './inspect
13
13
  export { exportToSheets } from './exportToSheets';
14
14
  export type { ExportToSheetsOptions, ExportToSheetsResult, } from './exportToSheets';
15
15
  export { VizContextProvider, useVizContext, getFormatted, getRaw, } from './vizContext';
16
- export type { VizContext, VizContextCell, VizContextRow, DataAppVizContextMessage, VizContextRequestMessage, } from './vizContext';
16
+ export type { VizContext, VizContextCell, VizContextOptionValue, VizContextRow, DataAppVizContextMessage, VizContextRequestMessage, } from './vizContext';
17
17
  export { useUrlState } from './urlState';
18
18
  export type { SdkUrlStateChangeMessage, UrlStateMap } from './urlState';
package/dist/index.js CHANGED
@@ -16,7 +16,7 @@ export { createPostMessageTransport } from './postMessageTransport';
16
16
  export { SDK_FEATURES, SDK_FEATURE_KEYS, SDK_MANIFEST_MESSAGE_TYPE, } from './features';
17
17
  // Google Sheets export (data apps)
18
18
  export { exportToSheets } from './exportToSheets';
19
- // Data app viz render context (host-pushed rows + field mapping)
19
+ // Data app viz render context (host-pushed rows + field mapping + config options)
20
20
  export { VizContextProvider, useVizContext, getFormatted, getRaw, } from './vizContext';
21
21
  // Shareable URL state (seeded from and written back to the host page URL)
22
22
  export { useUrlState } from './urlState';
@@ -23,15 +23,23 @@ export type VizContextCell = {
23
23
  };
24
24
  /** A result row keyed by query field id. */
25
25
  export type VizContextRow = Record<string, VizContextCell | undefined>;
26
+ /**
27
+ * A config option value. Its shape follows the option's declared type:
28
+ * `boolean` → boolean, `number` → number, `select`/`text`/`color` → string.
29
+ */
30
+ export type VizContextOptionValue = boolean | number | string;
26
31
  /**
27
32
  * Pushed by the host into the iframe. `fieldMapping` maps each field name the
28
33
  * renderer declared to the query field id it resolves to; `rows` are the
29
- * host-fetched result rows keyed by field id.
34
+ * host-fetched result rows keyed by field id; `options` holds the current
35
+ * value of each config option the renderer declared.
30
36
  */
31
37
  export type DataAppVizContextMessage = {
32
38
  type: 'lightdash:sdk:data-app-viz-context';
33
39
  fieldMapping: Record<string, string>;
34
40
  rows: VizContextRow[];
41
+ /** Absent when the installed host predates config-option delivery. */
42
+ options?: Record<string, VizContextOptionValue>;
35
43
  };
36
44
  /** Posted by the iframe on mount so the host pushes the current context. */
37
45
  export type VizContextRequestMessage = {
@@ -46,13 +54,23 @@ export type VizContext = {
46
54
  fieldMapping: Record<string, string>;
47
55
  /** Host-fetched result rows, keyed by query field id. */
48
56
  rows: VizContextRow[];
57
+ /** Config option name → current value (the user's choice, else the declared default). */
58
+ options: Record<string, VizContextOptionValue>;
49
59
  /** False until the first context arrives — render a placeholder while false. */
50
60
  ready: boolean;
51
61
  };
52
- type VizContextState = {
62
+ type VizContextValue = {
53
63
  fieldMapping: Record<string, string>;
54
64
  rows: VizContextRow[];
55
- } | null;
65
+ options: Record<string, VizContextOptionValue>;
66
+ };
67
+ type VizContextState = VizContextValue | null;
68
+ /**
69
+ * Normalises an inbound host message into provider state. The payload crosses a
70
+ * postMessage boundary so every key is treated as untrusted; `options` is also
71
+ * absent from hosts predating it, and falls back to `{}`.
72
+ */
73
+ export declare function toVizContextState(message: DataAppVizContextMessage): VizContextValue;
56
74
  declare const NO_PROVIDER: unique symbol;
57
75
  /**
58
76
  * Owns the single listener + handshake for a data app viz. Mount it in the
@@ -67,7 +85,8 @@ export declare function VizContextProvider({ children }: {
67
85
  * one is mounted (the scaffold default); otherwise self-subscribes so the hook
68
86
  * still works standalone. Re-renders whenever the host pushes (on load, on
69
87
  * mapping change, on query change). Resolve a declared field to its bound cell
70
- * with `fieldMapping[name]` then `getFormatted`/`getRaw`.
88
+ * with `fieldMapping[name]` then `getFormatted`/`getRaw`; read a declared
89
+ * config option with `options[name]`.
71
90
  */
72
91
  export declare function useVizContext(): VizContext;
73
92
  export {};
@@ -28,6 +28,29 @@ export const getRaw = (row, fieldId) => {
28
28
  return null;
29
29
  return row[fieldId]?.value?.raw ?? null;
30
30
  };
31
+ const isVizContextOptionValue = (value) => typeof value === 'string' ||
32
+ typeof value === 'boolean' ||
33
+ (typeof value === 'number' && Number.isFinite(value));
34
+ const normalizeOptions = (options) => {
35
+ if (typeof options !== 'object' ||
36
+ options === null ||
37
+ Array.isArray(options)) {
38
+ return {};
39
+ }
40
+ return Object.fromEntries(Object.entries(options).filter((entry) => isVizContextOptionValue(entry[1])));
41
+ };
42
+ /**
43
+ * Normalises an inbound host message into provider state. The payload crosses a
44
+ * postMessage boundary so every key is treated as untrusted; `options` is also
45
+ * absent from hosts predating it, and falls back to `{}`.
46
+ */
47
+ export function toVizContextState(message) {
48
+ return {
49
+ fieldMapping: message.fieldMapping ?? {},
50
+ rows: Array.isArray(message.rows) ? message.rows : [],
51
+ options: normalizeOptions(message.options),
52
+ };
53
+ }
31
54
  // Distinguishes "no provider mounted" from "provider present, no context yet".
32
55
  const NO_PROVIDER = Symbol('viz-context/no-provider');
33
56
  const VizContextContext = createContext(NO_PROVIDER);
@@ -45,10 +68,7 @@ function useVizContextSubscription(enabled) {
45
68
  const data = event.data;
46
69
  if (!data || data.type !== DATA_APP_VIZ_CONTEXT_MESSAGE)
47
70
  return;
48
- setContext({
49
- fieldMapping: data.fieldMapping ?? {},
50
- rows: Array.isArray(data.rows) ? data.rows : [],
51
- });
71
+ setContext(toVizContextState(data));
52
72
  };
53
73
  window.addEventListener('message', handleMessage);
54
74
  // Ask the host to push the current context. Sent from a mount effect —
@@ -77,7 +97,8 @@ export function VizContextProvider({ children }) {
77
97
  * one is mounted (the scaffold default); otherwise self-subscribes so the hook
78
98
  * still works standalone. Re-renders whenever the host pushes (on load, on
79
99
  * mapping change, on query change). Resolve a declared field to its bound cell
80
- * with `fieldMapping[name]` then `getFormatted`/`getRaw`.
100
+ * with `fieldMapping[name]` then `getFormatted`/`getRaw`; read a declared
101
+ * config option with `options[name]`.
81
102
  */
82
103
  export function useVizContext() {
83
104
  const fromProvider = useContext(VizContextContext);
@@ -91,6 +112,7 @@ export function useVizContext() {
91
112
  return {
92
113
  fieldMapping: context?.fieldMapping ?? {},
93
114
  rows: context?.rows ?? [],
115
+ options: context?.options ?? {},
94
116
  ready: context !== null,
95
117
  };
96
118
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lightdash/query-sdk",
3
- "version": "1.11.0",
3
+ "version": "1.12.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "SDK for building custom data apps against the Lightdash semantic layer",
@@ -35,7 +35,7 @@
35
35
  "typescript": "npm:@typescript/typescript6@6.0.1",
36
36
  "typescript-7": "npm:typescript@7.0.1-rc",
37
37
  "vitest": "4.1.6",
38
- "@lightdash/common": "1.11.0"
38
+ "@lightdash/common": "1.12.0"
39
39
  },
40
40
  "scripts": {
41
41
  "prebuild": "node ./scripts/generateSdkVersion.mjs",