mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-09-07 18:26:17 +00:00
feat(sdk): variable declaration read apis + browser-safe variables entry (#2046)
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user