@pwfabric/runtime 2.0.0 → 2.1.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/LICENSE +2 -2
- package/dist/binding/expression.js +4 -1
- package/dist/binding/expression.js.map +1 -1
- package/dist/connectors/types.d.ts +4 -2
- package/dist/connectors/types.js +4 -2
- package/dist/connectors/types.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/render/DataBoundSurfaceRenderer.d.ts +10 -0
- package/dist/render/DataBoundSurfaceRenderer.js +45 -11
- package/dist/render/DataBoundSurfaceRenderer.js.map +1 -1
- package/dist/render/data-binding-types.d.ts +13 -2
- package/dist/render/data-binding-types.js.map +1 -1
- package/dist/render/index.d.ts +3 -1
- package/dist/render/index.js +2 -1
- package/dist/render/index.js.map +1 -1
- package/dist/render/links.d.ts +50 -0
- package/dist/render/links.js +78 -0
- package/dist/render/links.js.map +1 -0
- package/dist/render/resolve-expressions.d.ts +22 -1
- package/dist/render/resolve-expressions.js +65 -5
- package/dist/render/resolve-expressions.js.map +1 -1
- package/dist/render/scoped-css.d.ts +8 -1
- package/dist/render/scoped-css.js +16 -0
- package/dist/render/scoped-css.js.map +1 -1
- package/dist/surface/expression.js +0 -1
- package/dist/surface/expression.js.map +1 -1
- package/dist/surface/output.js +1 -1
- package/dist/surface/output.js.map +1 -1
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scoped-css.js","sourceRoot":"","sources":["../../src/render/scoped-css.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAIH,4FAA4F;AAC5F,MAAM,UAAU,WAAW,CAAC,EAAU;IACpC,OAAO,EAAE,CAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAA;AACrC,CAAC;AAED,yFAAyF;AACzF,MAAM,UAAU,YAAY,CAAC,IAAY;IACvC,OAAO,IA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B,KAAK,MAAM,KAAK,IAAI,KAAK,CAAC,QAAQ;gBAAE,IAAI,CAAC,KAAK,CAAC,CAAA;QACjD,CAAC;IACH,CAAC,CAAA;IACD,KAAK,MAAM,KAAK,IAAI,MAAM;QAAE,IAAI,CAAC,KAAK,CAAC,CAAA;IACvC,OAAO,GAAG,CAAA;AACZ,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,MAAM,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,CAAU,CAAA;AAEvF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG;IACjC,YAAY;IACZ,iBAAiB;IACjB,eAAe;IACf,SAAS;IACT,cAAc;IACd,YAAY;IACZ,UAAU;CACF,CAAA;AAEV,MAAM,CAAC,MAAM,iBAAiB,GAAG;IAC/B,QAAQ;IACR,WAAW;IACX,aAAa;IACb,cAAc;IACd,YAAY;IACZ,UAAU;IACV,UAAU;IACV,GAAG,eAAe;IAClB,GAAG,mBAAmB;CACd,CAAA;AAEV,sGAAsG;AACtG,MAAM,CAAC,MAAM,YAAY,GAAG,sBAAsB,CAAA","sourcesContent":["/**\n * Shared helpers for the scoped per-block CSS collectors.\n *\n * The runtime emits several kinds of scoped `<style>` rules keyed by a block's\n * stable `data-block-id` (visibility / interaction-states / responsive-style /\n * conditional-states / group-states). They all share the SAME primitives:\n *\n * - `cssEscapeId(id)` — conservative attribute-value escaper for block ids.\n * - `camelToKebab(prop)` — CSS camelCase property name → kebab-case.\n * - `blockSelector(id)` — `[data-pw-surface] [data-block-id=\"<escaped>\"]`,\n * the scoped base selector every rule attaches to.\n * - `collectScopedCss(blocks, perBlockFn)` — walk the block tree (including\n * children) and concatenate each block's CSS as\n * produced by `perBlockFn(block)`.\n *\n * Centralising these keeps the per-collector files focused on their own\n * selector/declaration semantics and guarantees byte-identical output across\n * the family (the same escaper, the same selector string, the same walk order).\n *\n * @module @pwfabric/runtime/render/scoped-css\n */\n\nimport type { Block } from '@pwfabric/core'\n\n/** Conservative CSS attribute-selector value escaper (block ids are slugs, but be safe). */\nexport function cssEscapeId(id: string): string {\n return id.replace(/[\"\\\\]/g, '\\\\$&')\n}\n\n/** camelCase CSS property → kebab-case (e.g. `backgroundColor` → `background-color`). */\nexport function camelToKebab(prop: string): string {\n return prop.replace(/[A-Z]/g, (m) => `-${m.toLowerCase()}`)\n}\n\n/** The bare `[data-block-id=\"<escaped-id>\"]` attribute selector (no surface scope). */\nexport function blockIdAttr(id: string): string {\n return `[data-block-id=\"${cssEscapeId(id)}\"]`\n}\n\n/**\n * The surface-root selector a scoped rule hangs off.\n *\n * With a `surfaceId` this is `[data-pw-surface=\"<id>\"]` — scope carried by the\n * attribute's VALUE, not merely its presence. That matters because the embed\n * autoloader hydrates EVERY `[data-pw-surface]` element in a document, so a host\n * page can hold several surfaces at once; a value-less selector makes each\n * surface's rules apply to all of them, and the last one in document order wins.\n *\n * Without a `surfaceId` it degrades to the historical `[data-pw-surface]`, which\n * is correct for rules whose content is identical for every surface (the base\n * CSS) and for unit tests that render a single surface.\n */\nexport function surfaceScope(surfaceId?: string): string {\n return surfaceId ? `[data-pw-surface=\"${cssEscapeId(surfaceId)}\"]` : '[data-pw-surface]'\n}\n\n/**\n * The scoped base selector a per-block rule attaches to:\n * `[data-pw-surface=\"<sid>\"] [data-block-id=\"<escaped-id>\"]`. Scoped under the\n * surface root so a rule can never leak to the host page — and, when the surface\n * id is supplied, never to a SIBLING surface that happens to reuse a block id.\n */\nexport function blockSelector(id: string, surfaceId?: string): string {\n return `${surfaceScope(surfaceId)} ${blockIdAttr(id)}`\n}\n\n/**\n * Walk a surface block tree (depth-first, children after their parent — the\n * order all collectors used) and concatenate the per-block CSS produced by\n * `perBlockFn(block)`. Blocks for which `perBlockFn` returns '' contribute zero\n * bytes, so existing surfaces stay byte-identical.\n */\nexport function collectScopedCss(\n blocks: readonly Block[],\n perBlockFn: (block: Block) => string\n): string {\n let css = ''\n const walk = (block: Block): void => {\n css += perBlockFn(block)\n if (block.children) {\n for (const child of block.children) walk(child)\n }\n }\n for (const block of blocks) walk(block)\n return css\n}\n\n/**\n * Style keys HOISTED to the wrapper — kept here so the renderer and the\n * responsive CSS generator see the SAME list.\n *\n * This list is a RULE: these keys apply to the outer wrapper, not the\n * component root. For flat values the renderer hoists them; for responsive\n * values the value leaves the inline style entirely and lands in CSS, so the\n * same rule has to be expressed on the selector side — otherwise the rule\n * matches both the wrapper and the root and the value is applied TWICE\n * (`marginTop:'-40%'` would move content by both -335px and -256px).\n */\nexport const FLEX_HOIST_KEYS = ['flex', 'flexGrow', 'flexShrink', 'flexBasis'] as const\n\n/**\n * Where a block sits in its parent's grid. The grid item is the WRAPPER the\n * renderer draws (it carries the `layout.span` column too); on the component\n * root, one level down, `gridColumn: {xs: 'span 12', md: 'span 4'}` placed\n * nothing — the root is the wrapper's only child, not a grid item.\n */\nexport const GRID_PLACEMENT_KEYS = [\n 'gridColumn',\n 'gridColumnStart',\n 'gridColumnEnd',\n 'gridRow',\n 'gridRowStart',\n 'gridRowEnd',\n 'gridArea',\n] as const\n\nexport const LAYOUT_HOIST_KEYS = [\n 'margin',\n 'marginTop',\n 'marginRight',\n 'marginBottom',\n 'marginLeft',\n 'maxWidth',\n 'minWidth',\n ...FLEX_HOIST_KEYS,\n ...GRID_PLACEMENT_KEYS,\n] as const\n\n/** The attribute that tells the wrapper from the component root (the same marker states-css uses). */\nexport const WRAPPER_ATTR = '[data-block-wrapper]'\n"]}
|
|
@@ -145,7 +145,6 @@ export const DEFAULT_HELPERS = {
|
|
|
145
145
|
upper: (s) => String(s).toUpperCase(),
|
|
146
146
|
lower: (s) => String(s).toLowerCase(),
|
|
147
147
|
trim: (s) => String(s).trim(),
|
|
148
|
-
length: (s) => (Array.isArray(s) ? s.length : String(s).length),
|
|
149
148
|
concat: (...args) => args.map(String).join(''),
|
|
150
149
|
// Number helpers
|
|
151
150
|
// `round` comes from the binding set: `round(n)` as before, `round(n, 2)`
|
|
@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GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAC9E,OAAO,EAAE,CAAC,CAAU,EAAE,EAAE,CACtB,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC;IACnF,UAAU,EAAE,CAAC,CAAU,EAAE,EAAE,CACzB,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC;IAEpF,eAAe;IACf,MAAM,EAAE,CAAC,CAAU,EAAE,EAAE,CAAC,OAAO,CAAC;IAChC,OAAO,EAAE,CAAC,CAAU,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;IACzC,QAAQ,EAAE,CAAC,CAAU,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ;IAC/C,QAAQ,EAAE,CAAC,CAAU,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ;IAC/C,MAAM,EAAE,CAAC,CAAU,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,SAAS;IAE9C,qBAAqB;IACrB,IAAI,EAAE,CAAC,CAAU,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC;CACxC,CAAA;AAED;;GAEG;AACH,MAAM,UAAU,uBAAuB,CACrC,IAA6B,EAC7B,iBAAmE;IAEnE,OAAO;QACL,IAAI;QACJ,OAAO,EAAE,EAAE,GAAG,eAAe,EAAE,GAAG,iBAAiB,EAAE;KACtD,CAAA;AACH,CAAC","sourcesContent":["/**\n * Expression Evaluator\n *\n * Safe expression evaluation for surface rendering\n *\n * Expressions are used in conditionals, loops, and data bindings.\n * This evaluator provides a safe, sandboxed way to evaluate expressions.\n *\n * @module @pwfabric/runtime/surface/expression\n */\n\n// ============================================================\n// EXPRESSION TYPES\n// ============================================================\n\n/**\n * Expression result\n */\nimport { DEFAULT_BINDING_HELPERS, evaluateWithScope, readProperty } from '../binding/expression.js'\n\nexport type ExpressionResult<T = unknown> =\n | { readonly success: true; readonly value: T }\n | { readonly success: false; readonly error: string }\n\n/**\n * Expression context - the scope for evaluation\n */\nexport interface ExpressionContext {\n readonly data: Record<string, unknown>\n readonly helpers?: Record<string, (...args: unknown[]) => unknown>\n}\n\n// ============================================================\n// PATH RESOLUTION\n// ============================================================\n\n/**\n * Resolve a dotted path in an object\n * e.g., \"user.profile.name\" -> obj.user.profile.name\n */\nexport function resolvePath(obj: unknown, path: string): unknown {\n if (!path || typeof obj !== 'object' || obj === null) {\n return undefined\n }\n\n const parts = path.split('.')\n let current: unknown = obj\n\n for (const part of parts) {\n if (current === null || current === undefined) {\n return undefined\n }\n\n // Handle array indexing: items[0]\n const arrayMatch = part.match(/^(\\w+)\\[(\\d+)\\]$/)\n if (arrayMatch) {\n const prop = arrayMatch[1]\n const index = arrayMatch[2]\n if (prop && index) {\n current = readProperty(current, prop)\n if (Array.isArray(current)) {\n current = current[parseInt(index, 10)]\n } else {\n return undefined\n }\n }\n } else if (typeof current === 'object') {\n // A list of ONE record is read through — see readProperty.\n current = readProperty(current, part)\n } else {\n return undefined\n }\n }\n\n return current\n}\n\n// ============================================================\n// EXPRESSION EVALUATOR\n// ============================================================\n\n/**\n * Evaluate an expression (the text between `{{` and `}}`).\n *\n * THE SAME PARSER AS THE BINDING EVALUATOR (binding/expression.ts). This used\n * to be a regex/`split` evaluator of its own, and the two drifted: no\n * parentheses, no arithmetic, helper arguments split on every comma — so\n * `{{a == 'x' && (b || c)}}` failed, and a failed `visibleWhen` rendered its\n * raw template, which counts as visible.\n *\n * Supported: paths (`user.name`, `items[0]`, `items.0`, hyphenated source ids\n * `hero-stats.0.text`), literals, `! - +`, `* / %`, `+ -`, comparisons,\n * `&& || ??` (short-circuit), `?:`, parentheses, array literals, helper calls\n * (`join(xs, ', ')`) and a safe list of string / array / number methods\n * (`name.toLowerCase().includes(q)`).\n *\n * Names resolve against `context.data`; a call resolves against\n * `context.helpers`. In this mode a `-` between two name characters is part of\n * the name (source ids are hyphenated) — write subtraction as `a - b`.\n *\n * A syntax error, an unknown helper or an unsupported method is a FAILURE\n * (`success: false`), never a partial value.\n */\nexport function evaluate(expression: string, context: ExpressionContext): ExpressionResult {\n try {\n const data = context.data\n const helpers = context.helpers\n const value = evaluateWithScope(\n expression,\n {\n lookup: (name) => data[name],\n helper: (name) =>\n helpers && Object.prototype.hasOwnProperty.call(helpers, name) ? helpers[name] : undefined,\n },\n { hyphenNames: true }\n )\n return { success: true, value }\n } catch (e) {\n return { success: false, error: `Unable to evaluate expression: ${expression.trim()} — ${e instanceof Error ? e.message : String(e)}` }\n }\n}\n\n// ============================================================\n// INTERPOLATION\n// ============================================================\n\n/**\n * Interpolate expressions within a template string\n * e.g., \"Hello, {{user.name}}!\" -> \"Hello, John!\"\n */\nexport function interpolate(\n template: string,\n context: ExpressionContext\n): ExpressionResult<string> {\n const pattern = /\\{\\{(.+?)\\}\\}/g\n let result = template\n let match: RegExpExecArray | null\n\n while ((match = pattern.exec(template)) !== null) {\n const placeholder = match[0]\n const expression = match[1]\n if (!expression) continue\n const evalResult = evaluate(expression.trim(), context)\n if (!evalResult.success) {\n return evalResult as ExpressionResult<string>\n }\n // CALLBACK replacer, NOT a string replacement. When its second argument is\n // a string, `String.replace` treats `$&`, `$\\``, `$'`, `$$` as REPLACEMENT\n // PATTERNS: `\"$$5\"` in the data would print as `\"$5\"`, and `\"a$&b\"` would\n // bring back the placeholder itself. The sibling evaluator\n // (binding/expression.ts) uses a callback too.\n const replacement = String(evalResult.value ?? '')\n result = result.replace(placeholder, () => replacement)\n }\n\n return { success: true, value: result }\n}\n\n// ============================================================\n// BUILT-IN HELPERS\n// ============================================================\n\n/**\n * Default helper functions available in expressions\n */\nexport const DEFAULT_HELPERS: Record<string, (...args: unknown[]) => unknown> = {\n // The binding evaluator's helper set comes first. Two evaluators exist —\n // binding/expression (bindings, computed values) and this one (block props\n // through resolveBlockExpressions) — and their helper lists drifted:\n // `sumBy`, `groupCount`, `min`, `max`, `currency`, `date`… live in the\n // binding set, and without it a KPI tile authored as\n // `{{sumBy(customers,'mrr')}}` would render the raw template. One name, one meaning: the surface-specific entries below\n // override where both define the same name.\n ...DEFAULT_BINDING_HELPERS,\n // String helpers\n upper: (s: unknown) => String(s).toUpperCase(),\n lower: (s: unknown) => String(s).toLowerCase(),\n trim: (s: unknown) => String(s).trim(),\n length: (s: unknown) => (Array.isArray(s) ? s.length : String(s).length),\n concat: (...args: unknown[]) => args.map(String).join(''),\n\n // Number helpers\n // `round` comes from the binding set: `round(n)` as before, `round(n, 2)`\n // to two decimals (this override used to drop the second argument).\n floor: (n: unknown) => Math.floor(Number(n)),\n ceil: (n: unknown) => Math.ceil(Number(n)),\n abs: (n: unknown) => Math.abs(Number(n)),\n\n // Array helpers\n first: (arr: unknown) => (Array.isArray(arr) ? arr[0] : undefined),\n last: (arr: unknown) => (Array.isArray(arr) ? arr[arr.length - 1] : undefined),\n isEmpty: (v: unknown) =>\n v === null || v === undefined || v === '' || (Array.isArray(v) && v.length === 0),\n isNotEmpty: (v: unknown) =>\n v !== null && v !== undefined && v !== '' && !(Array.isArray(v) && v.length === 0),\n\n // Type helpers\n typeof: (v: unknown) => typeof v,\n isArray: (v: unknown) => Array.isArray(v),\n isString: (v: unknown) => typeof v === 'string',\n isNumber: (v: unknown) => typeof v === 'number',\n isBool: (v: unknown) => typeof v === 'boolean',\n\n // Formatting helpers\n json: (v: unknown) => JSON.stringify(v),\n}\n\n/**\n * Create an expression context with default helpers\n */\nexport function createExpressionContext(\n data: Record<string, unknown>,\n additionalHelpers?: Record<string, (...args: unknown[]) => unknown>\n): ExpressionContext {\n return {\n data,\n helpers: { ...DEFAULT_HELPERS, ...additionalHelpers },\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AAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC;IACnF,UAAU,EAAE,CAAC,CAAU,EAAE,EAAE,CACzB,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC;IAEpF,eAAe;IACf,MAAM,EAAE,CAAC,CAAU,EAAE,EAAE,CAAC,OAAO,CAAC;IAChC,OAAO,EAAE,CAAC,CAAU,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;IACzC,QAAQ,EAAE,CAAC,CAAU,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ;IAC/C,QAAQ,EAAE,CAAC,CAAU,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ;IAC/C,MAAM,EAAE,CAAC,CAAU,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,SAAS;IAE9C,qBAAqB;IACrB,IAAI,EAAE,CAAC,CAAU,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC;CACxC,CAAA;AAED;;GAEG;AACH,MAAM,UAAU,uBAAuB,CACrC,IAA6B,EAC7B,iBAAmE;IAEnE,OAAO;QACL,IAAI;QACJ,OAAO,EAAE,EAAE,GAAG,eAAe,EAAE,GAAG,iBAAiB,EAAE;KACtD,CAAA;AACH,CAAC","sourcesContent":["/**\n * Expression Evaluator\n *\n * Safe expression evaluation for surface rendering\n *\n * Expressions are used in conditionals, loops, and data bindings.\n * This evaluator provides a safe, sandboxed way to evaluate expressions.\n *\n * @module @pwfabric/runtime/surface/expression\n */\n\n// ============================================================\n// EXPRESSION TYPES\n// ============================================================\n\n/**\n * Expression result\n */\nimport { DEFAULT_BINDING_HELPERS, evaluateWithScope, readProperty } from '../binding/expression.js'\n\nexport type ExpressionResult<T = unknown> =\n | { readonly success: true; readonly value: T }\n | { readonly success: false; readonly error: string }\n\n/**\n * Expression context - the scope for evaluation\n */\nexport interface ExpressionContext {\n readonly data: Record<string, unknown>\n readonly helpers?: Record<string, (...args: unknown[]) => unknown>\n}\n\n// ============================================================\n// PATH RESOLUTION\n// ============================================================\n\n/**\n * Resolve a dotted path in an object\n * e.g., \"user.profile.name\" -> obj.user.profile.name\n */\nexport function resolvePath(obj: unknown, path: string): unknown {\n if (!path || typeof obj !== 'object' || obj === null) {\n return undefined\n }\n\n const parts = path.split('.')\n let current: unknown = obj\n\n for (const part of parts) {\n if (current === null || current === undefined) {\n return undefined\n }\n\n // Handle array indexing: items[0]\n const arrayMatch = part.match(/^(\\w+)\\[(\\d+)\\]$/)\n if (arrayMatch) {\n const prop = arrayMatch[1]\n const index = arrayMatch[2]\n if (prop && index) {\n current = readProperty(current, prop)\n if (Array.isArray(current)) {\n current = current[parseInt(index, 10)]\n } else {\n return undefined\n }\n }\n } else if (typeof current === 'object') {\n // A list of ONE record is read through — see readProperty.\n current = readProperty(current, part)\n } else {\n return undefined\n }\n }\n\n return current\n}\n\n// ============================================================\n// EXPRESSION EVALUATOR\n// ============================================================\n\n/**\n * Evaluate an expression (the text between `{{` and `}}`).\n *\n * THE SAME PARSER AS THE BINDING EVALUATOR (binding/expression.ts). This used\n * to be a regex/`split` evaluator of its own, and the two drifted: no\n * parentheses, no arithmetic, helper arguments split on every comma — so\n * `{{a == 'x' && (b || c)}}` failed, and a failed `visibleWhen` rendered its\n * raw template, which counts as visible.\n *\n * Supported: paths (`user.name`, `items[0]`, `items.0`, hyphenated source ids\n * `hero-stats.0.text`), literals, `! - +`, `* / %`, `+ -`, comparisons,\n * `&& || ??` (short-circuit), `?:`, parentheses, array literals, helper calls\n * (`join(xs, ', ')`) and a safe list of string / array / number methods\n * (`name.toLowerCase().includes(q)`).\n *\n * Names resolve against `context.data`; a call resolves against\n * `context.helpers`. In this mode a `-` between two name characters is part of\n * the name (source ids are hyphenated) — write subtraction as `a - b`.\n *\n * A syntax error, an unknown helper or an unsupported method is a FAILURE\n * (`success: false`), never a partial value.\n */\nexport function evaluate(expression: string, context: ExpressionContext): ExpressionResult {\n try {\n const data = context.data\n const helpers = context.helpers\n const value = evaluateWithScope(\n expression,\n {\n lookup: (name) => data[name],\n helper: (name) =>\n helpers && Object.prototype.hasOwnProperty.call(helpers, name) ? helpers[name] : undefined,\n },\n { hyphenNames: true }\n )\n return { success: true, value }\n } catch (e) {\n return { success: false, error: `Unable to evaluate expression: ${expression.trim()} — ${e instanceof Error ? e.message : String(e)}` }\n }\n}\n\n// ============================================================\n// INTERPOLATION\n// ============================================================\n\n/**\n * Interpolate expressions within a template string\n * e.g., \"Hello, {{user.name}}!\" -> \"Hello, John!\"\n */\nexport function interpolate(\n template: string,\n context: ExpressionContext\n): ExpressionResult<string> {\n const pattern = /\\{\\{(.+?)\\}\\}/g\n let result = template\n let match: RegExpExecArray | null\n\n while ((match = pattern.exec(template)) !== null) {\n const placeholder = match[0]\n const expression = match[1]\n if (!expression) continue\n const evalResult = evaluate(expression.trim(), context)\n if (!evalResult.success) {\n return evalResult as ExpressionResult<string>\n }\n // CALLBACK replacer, NOT a string replacement. When its second argument is\n // a string, `String.replace` treats `$&`, `$\\``, `$'`, `$$` as REPLACEMENT\n // PATTERNS: `\"$$5\"` in the data would print as `\"$5\"`, and `\"a$&b\"` would\n // bring back the placeholder itself. The sibling evaluator\n // (binding/expression.ts) uses a callback too.\n const replacement = String(evalResult.value ?? '')\n result = result.replace(placeholder, () => replacement)\n }\n\n return { success: true, value: result }\n}\n\n// ============================================================\n// BUILT-IN HELPERS\n// ============================================================\n\n/**\n * Default helper functions available in expressions\n */\nexport const DEFAULT_HELPERS: Record<string, (...args: unknown[]) => unknown> = {\n // The binding evaluator's helper set comes first. Two evaluators exist —\n // binding/expression (bindings, computed values) and this one (block props\n // through resolveBlockExpressions) — and their helper lists drifted:\n // `sumBy`, `groupCount`, `min`, `max`, `currency`, `date`… live in the\n // binding set, and without it a KPI tile authored as\n // `{{sumBy(customers,'mrr')}}` would render the raw template. One name, one meaning: the surface-specific entries below\n // override where both define the same name.\n ...DEFAULT_BINDING_HELPERS,\n // String helpers\n upper: (s: unknown) => String(s).toUpperCase(),\n lower: (s: unknown) => String(s).toLowerCase(),\n trim: (s: unknown) => String(s).trim(),\n concat: (...args: unknown[]) => args.map(String).join(''),\n\n // Number helpers\n // `round` comes from the binding set: `round(n)` as before, `round(n, 2)`\n // to two decimals (this override used to drop the second argument).\n floor: (n: unknown) => Math.floor(Number(n)),\n ceil: (n: unknown) => Math.ceil(Number(n)),\n abs: (n: unknown) => Math.abs(Number(n)),\n\n // Array helpers\n first: (arr: unknown) => (Array.isArray(arr) ? arr[0] : undefined),\n last: (arr: unknown) => (Array.isArray(arr) ? arr[arr.length - 1] : undefined),\n isEmpty: (v: unknown) =>\n v === null || v === undefined || v === '' || (Array.isArray(v) && v.length === 0),\n isNotEmpty: (v: unknown) =>\n v !== null && v !== undefined && v !== '' && !(Array.isArray(v) && v.length === 0),\n\n // Type helpers\n typeof: (v: unknown) => typeof v,\n isArray: (v: unknown) => Array.isArray(v),\n isString: (v: unknown) => typeof v === 'string',\n isNumber: (v: unknown) => typeof v === 'number',\n isBool: (v: unknown) => typeof v === 'boolean',\n\n // Formatting helpers\n json: (v: unknown) => JSON.stringify(v),\n}\n\n/**\n * Create an expression context with default helpers\n */\nexport function createExpressionContext(\n data: Record<string, unknown>,\n additionalHelpers?: Record<string, (...args: unknown[]) => unknown>\n): ExpressionContext {\n return {\n data,\n helpers: { ...DEFAULT_HELPERS, ...additionalHelpers },\n }\n}\n"]}
|
package/dist/surface/output.js
CHANGED
|
@@ -17,7 +17,7 @@ const webOutputDataSchema = z.object({
|
|
|
17
17
|
head: z.string().optional(),
|
|
18
18
|
});
|
|
19
19
|
const apiOutputDataSchema = z.object({
|
|
20
|
-
json: z.unknown(),
|
|
20
|
+
json: z.unknown().optional(),
|
|
21
21
|
status: z.number().int().min(100).max(599),
|
|
22
22
|
});
|
|
23
23
|
const cliOutputDataSchema = z.object({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"output.js","sourceRoot":"","sources":["../../src/surface/output.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAsCvB,+DAA+D;AAC/D,cAAc;AACd,+DAA+D;AAE/D,MAAM,mBAAmB,GAAG,CAAC,CAAC,MAAM,CAAC;IACnC,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;IAChB,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC1B,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;CAC5B,CAAC,CAAA;AAEF,MAAM,mBAAmB,GAAG,CAAC,CAAC,MAAM,CAAC;IACnC,IAAI,EAAE,CAAC,CAAC,OAAO,EAAE;
|
|
1
|
+
{"version":3,"file":"output.js","sourceRoot":"","sources":["../../src/surface/output.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAsCvB,+DAA+D;AAC/D,cAAc;AACd,+DAA+D;AAE/D,MAAM,mBAAmB,GAAG,CAAC,CAAC,MAAM,CAAC;IACnC,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;IAChB,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC1B,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;CAC5B,CAAC,CAAA;AAEF,MAAM,mBAAmB,GAAG,CAAC,CAAC,MAAM,CAAC;IACnC,IAAI,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE;IAC5B,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CA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AAE,CAAA;AAClC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,OAK/B;IACC,MAAM,UAAU,GAAG,mBAAmB,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IAE9D,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;QACxB,OAAO;YACL,OAAO,EAAE,KAAK;YACd,MAAM,EAAE,UAAU,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,OAAO,EAAE,CAAC;SACjG,CAAA;IACH,CAAC;IAED,MAAM,MAAM,GAAmB;QAC7B,IAAI,EAAE,KAAK;QACX,IAAI,EAAE,UAAU,CAAC,IAAI;QACrB,QAAQ,EAAE,OAAO,CAAC,QAAQ;QAC1B,GAAG,CAAC,OAAO,CAAC,MAAM,KAAK,SAAS,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC;QAC/D,GAAG,CAAC,OAAO,CAAC,MAAM,KAAK,SAAS,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC;KAChE,CAAA;IAED,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,CAAA;AAClC,CAAC","sourcesContent":["/**\n * Typed Output Factories\n *\n * Typed wrappers for BaseSurfaceOutput.data.\n * These do NOT modify @pwfabric/contracts. They extend the contract types\n * with narrowed data fields and provide factory functions that validate.\n *\n * @module @pwfabric/runtime/surface/output\n */\n\nimport { z } from 'zod'\nimport type { WebSurfaceOutput, ApiSurfaceOutput, CliSurfaceOutput } from '@pwfabric/contracts'\n\n// ============================================================\n// TYPED OUTPUT INTERFACES\n// ============================================================\n\n/**\n * Web output with narrowed data type\n */\nexport interface TypedWebOutput extends WebSurfaceOutput {\n readonly data: {\n readonly html: string\n readonly css?: string\n readonly head?: string\n }\n}\n\n/**\n * API output with narrowed data type\n */\nexport interface TypedApiOutput extends ApiSurfaceOutput {\n readonly data: {\n readonly json: unknown\n readonly status: number\n }\n}\n\n/**\n * CLI output with narrowed data type\n */\nexport interface TypedCliOutput extends CliSurfaceOutput {\n readonly data: {\n readonly formatted: string\n readonly raw?: unknown\n }\n}\n\n// ============================================================\n// ZOD SCHEMAS\n// ============================================================\n\nconst webOutputDataSchema = z.object({\n html: z.string(),\n css: z.string().optional(),\n head: z.string().optional(),\n})\n\nconst apiOutputDataSchema = z.object({\n json: z.unknown().optional(),\n status: z.number().int().min(100).max(599),\n})\n\nconst cliOutputDataSchema = z.object({\n formatted: z.string(),\n raw: z.unknown().optional(),\n})\n\n// ============================================================\n// VALIDATION RESULT\n// ============================================================\n\n/**\n * Result of creating a typed output\n */\nexport type OutputFactoryResult<T> =\n | { readonly success: true; readonly output: T }\n | { readonly success: false; readonly errors: readonly string[] }\n\n// ============================================================\n// FACTORY FUNCTIONS\n// ============================================================\n\n/**\n * Create a validated TypedWebOutput.\n *\n * Validates the data shape and returns a typed output or errors.\n */\nexport function createWebOutput(options: {\n readonly data: { readonly html: string; readonly css?: string; readonly head?: string }\n readonly component?: unknown\n readonly html?: string\n}): OutputFactoryResult<TypedWebOutput> {\n const dataResult = webOutputDataSchema.safeParse(options.data)\n\n if (!dataResult.success) {\n return {\n success: false,\n errors: dataResult.error.issues.map((i) => `data.${i.path.map(String).join('.')}: ${i.message}`),\n }\n }\n\n const data: TypedWebOutput['data'] = {\n html: dataResult.data.html,\n ...(dataResult.data.css !== undefined ? { css: dataResult.data.css } : {}),\n ...(dataResult.data.head !== undefined ? { head: dataResult.data.head } : {}),\n }\n\n const output: TypedWebOutput = {\n type: 'web',\n data,\n ...(options.component !== undefined ? { component: options.component } : {}),\n ...(options.html !== undefined ? { html: options.html } : {}),\n }\n\n return { success: true, output }\n}\n\n/**\n * Create a validated TypedApiOutput.\n *\n * Validates the data shape and returns a typed output or errors.\n */\nexport function createApiOutput(options: {\n readonly data: { readonly json: unknown; readonly status: number }\n readonly status: number\n readonly headers?: Record<string, string>\n readonly body?: unknown\n}): OutputFactoryResult<TypedApiOutput> {\n const dataResult = apiOutputDataSchema.safeParse(options.data)\n\n if (!dataResult.success) {\n return {\n success: false,\n errors: dataResult.error.issues.map((i) => `data.${i.path.map(String).join('.')}: ${i.message}`),\n }\n }\n\n const data: TypedApiOutput['data'] = {\n json: dataResult.data.json,\n status: dataResult.data.status,\n }\n\n const output: TypedApiOutput = {\n type: 'api',\n data,\n status: options.status,\n body: options.body ?? null,\n ...(options.headers ? { headers: options.headers } : {}),\n }\n\n return { success: true, output }\n}\n\n/**\n * Create a validated TypedCliOutput.\n *\n * Validates the data shape and returns a typed output or errors.\n */\nexport function createCliOutput(options: {\n readonly data: { readonly formatted: string; readonly raw?: unknown }\n readonly exitCode: number\n readonly stdout?: string\n readonly stderr?: string\n}): OutputFactoryResult<TypedCliOutput> {\n const dataResult = cliOutputDataSchema.safeParse(options.data)\n\n if (!dataResult.success) {\n return {\n success: false,\n errors: dataResult.error.issues.map((i) => `data.${i.path.map(String).join('.')}: ${i.message}`),\n }\n }\n\n const output: TypedCliOutput = {\n type: 'cli',\n data: dataResult.data,\n exitCode: options.exitCode,\n ...(options.stdout !== undefined && { stdout: options.stdout }),\n ...(options.stderr !== undefined && { stderr: options.stderr }),\n }\n\n return { success: true, output }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pwfabric/runtime",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "PWFabric runtime and execution engine",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "BUSL-1.1",
|
|
@@ -69,8 +69,8 @@
|
|
|
69
69
|
"node": ">=22"
|
|
70
70
|
},
|
|
71
71
|
"dependencies": {
|
|
72
|
-
"@pwfabric/contracts": "2.
|
|
73
|
-
"@pwfabric/core": "2.
|
|
72
|
+
"@pwfabric/contracts": "2.1.0",
|
|
73
|
+
"@pwfabric/core": "2.1.0"
|
|
74
74
|
},
|
|
75
75
|
"peerDependencies": {
|
|
76
76
|
"react": "^18.0.0 || ^19.0.0",
|