feat(sdk): variable declaration read apis + browser-safe variables entry (#2046)

This commit is contained in:
James Russo
2026-07-09 13:15:21 -07:00
committed by GitHub
parent 030fded71d
commit fcbd4cb0f6
10 changed files with 340 additions and 47 deletions
+18
View File
@@ -7,6 +7,12 @@
* (engine/apply-patches.ts) can never disagree on the model's shape.
*/
// Browser-safe subpath — the core/parsers root entries pull Node-only modules
// and would break browser bundles that include the SDK (e.g. Studio).
import { parseCompositionVariables } from "@hyperframes/core/variables";
import type { CompositionVariable } from "@hyperframes/core/variables";
// Exported so the SDK index can re-export it (kept from #2098's surface).
export type VariableDecl = { id: string; default?: unknown; [key: string]: unknown };
function getHtmlEl(document: Document): Element | null {
@@ -33,6 +39,18 @@ function indexOfId(arr: VariableDecl[], id: string): number {
return arr.findIndex((v) => typeof v === "object" && v !== null && v.id === id);
}
/**
* Read the typed variable declarations from `data-composition-variables`.
* Delegates to the canonical parser (same filter the render pipeline uses),
* so malformed entries are dropped rather than surfaced. Returns `[]` when
* the document has no root element or no declarations.
*/
export function readVariableDeclarations(document: Document): CompositionVariable[] {
const htmlEl = getHtmlEl(document);
if (!htmlEl) return [];
return parseCompositionVariables(htmlEl);
}
/**
* Read the current `default` value for a variable id. Returns undefined when
* the attribute is absent, the JSON is invalid, or no entry matches the id.