typebars 1.0.3 → 1.0.5

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.
Files changed (131) hide show
  1. package/README.md +1 -2
  2. package/dist/{analyzer.d.ts → cjs/analyzer.d.ts} +1 -1
  3. package/dist/cjs/analyzer.js +2 -0
  4. package/dist/cjs/analyzer.js.map +1 -0
  5. package/dist/{compiled-template.d.ts → cjs/compiled-template.d.ts} +2 -2
  6. package/dist/cjs/compiled-template.js +2 -0
  7. package/dist/cjs/compiled-template.js.map +1 -0
  8. package/dist/{errors.d.ts → cjs/errors.d.ts} +1 -1
  9. package/dist/cjs/errors.js +3 -0
  10. package/dist/cjs/errors.js.map +1 -0
  11. package/dist/{executor.d.ts → cjs/executor.d.ts} +2 -2
  12. package/dist/cjs/executor.js +2 -0
  13. package/dist/cjs/executor.js.map +1 -0
  14. package/dist/{helpers → cjs/helpers}/helper-factory.d.ts +1 -1
  15. package/dist/cjs/helpers/helper-factory.js +2 -0
  16. package/dist/cjs/helpers/helper-factory.js.map +1 -0
  17. package/dist/cjs/helpers/index.d.ts +4 -0
  18. package/dist/cjs/helpers/index.js +2 -0
  19. package/dist/cjs/helpers/index.js.map +1 -0
  20. package/dist/{helpers → cjs/helpers}/logical-helpers.d.ts +2 -2
  21. package/dist/cjs/helpers/logical-helpers.js +2 -0
  22. package/dist/cjs/helpers/logical-helpers.js.map +1 -0
  23. package/dist/{helpers → cjs/helpers}/math-helpers.d.ts +2 -2
  24. package/dist/cjs/helpers/math-helpers.js +2 -0
  25. package/dist/cjs/helpers/math-helpers.js.map +1 -0
  26. package/dist/cjs/helpers/utils.js +2 -0
  27. package/dist/cjs/helpers/utils.js.map +1 -0
  28. package/dist/cjs/index.d.ts +3 -0
  29. package/dist/cjs/index.js +2 -0
  30. package/dist/cjs/index.js.map +1 -0
  31. package/dist/cjs/package.json +3 -0
  32. package/dist/cjs/parser.js +2 -0
  33. package/dist/cjs/parser.js.map +1 -0
  34. package/dist/cjs/schema-resolver.js +2 -0
  35. package/dist/cjs/schema-resolver.js.map +1 -0
  36. package/dist/{typebars.d.ts → cjs/typebars.d.ts} +2 -2
  37. package/dist/cjs/typebars.js +2 -0
  38. package/dist/cjs/typebars.js.map +1 -0
  39. package/dist/cjs/types.js +2 -0
  40. package/dist/cjs/types.js.map +1 -0
  41. package/dist/{utils.d.ts → cjs/utils.d.ts} +1 -1
  42. package/dist/cjs/utils.js +2 -0
  43. package/dist/cjs/utils.js.map +1 -0
  44. package/dist/esm/analyzer.d.ts +59 -0
  45. package/dist/esm/analyzer.js +2 -0
  46. package/dist/esm/analyzer.js.map +1 -0
  47. package/dist/esm/compiled-template.d.ts +130 -0
  48. package/dist/esm/compiled-template.js +2 -0
  49. package/dist/esm/compiled-template.js.map +1 -0
  50. package/dist/esm/errors.d.ts +105 -0
  51. package/dist/esm/errors.js +3 -0
  52. package/dist/esm/errors.js.map +1 -0
  53. package/dist/esm/executor.d.ts +55 -0
  54. package/dist/esm/executor.js +2 -0
  55. package/dist/esm/executor.js.map +1 -0
  56. package/dist/esm/helpers/helper-factory.d.ts +56 -0
  57. package/dist/esm/helpers/helper-factory.js +2 -0
  58. package/dist/esm/helpers/helper-factory.js.map +1 -0
  59. package/dist/esm/helpers/index.d.ts +4 -0
  60. package/dist/esm/helpers/index.js +2 -0
  61. package/dist/esm/helpers/index.js.map +1 -0
  62. package/dist/esm/helpers/logical-helpers.d.ts +15 -0
  63. package/dist/esm/helpers/logical-helpers.js +2 -0
  64. package/dist/esm/helpers/logical-helpers.js.map +1 -0
  65. package/dist/esm/helpers/math-helpers.d.ts +13 -0
  66. package/dist/esm/helpers/math-helpers.js +2 -0
  67. package/dist/esm/helpers/math-helpers.js.map +1 -0
  68. package/dist/esm/helpers/utils.d.ts +19 -0
  69. package/dist/esm/helpers/utils.js +2 -0
  70. package/dist/esm/helpers/utils.js.map +1 -0
  71. package/dist/esm/index.d.ts +3 -0
  72. package/dist/esm/index.js +2 -0
  73. package/dist/esm/index.js.map +1 -0
  74. package/dist/esm/parser.d.ts +146 -0
  75. package/dist/esm/parser.js +2 -0
  76. package/dist/esm/parser.js.map +1 -0
  77. package/dist/esm/schema-resolver.d.ts +88 -0
  78. package/dist/esm/schema-resolver.js +2 -0
  79. package/dist/esm/schema-resolver.js.map +1 -0
  80. package/dist/esm/typebars.d.ts +130 -0
  81. package/dist/esm/typebars.js +2 -0
  82. package/dist/esm/typebars.js.map +1 -0
  83. package/dist/esm/types.d.ts +334 -0
  84. package/dist/esm/types.js +2 -0
  85. package/dist/esm/types.js.map +1 -0
  86. package/dist/esm/utils.d.ts +113 -0
  87. package/dist/esm/utils.js +2 -0
  88. package/dist/esm/utils.js.map +1 -0
  89. package/package.json +29 -6
  90. package/dist/analyzer.js +0 -4
  91. package/dist/analyzer.js.map +0 -9
  92. package/dist/chunk-5znsrn10.js +0 -5
  93. package/dist/chunk-5znsrn10.js.map +0 -10
  94. package/dist/chunk-867xywnk.js +0 -5
  95. package/dist/chunk-867xywnk.js.map +0 -10
  96. package/dist/chunk-8xza8tca.js +0 -5
  97. package/dist/chunk-8xza8tca.js.map +0 -10
  98. package/dist/chunk-9jxzj2h4.js +0 -7
  99. package/dist/chunk-9jxzj2h4.js.map +0 -10
  100. package/dist/chunk-dpffacsy.js +0 -4
  101. package/dist/chunk-dpffacsy.js.map +0 -10
  102. package/dist/chunk-gayk9ew1.js +0 -7
  103. package/dist/chunk-gayk9ew1.js.map +0 -10
  104. package/dist/chunk-s96k41p3.js +0 -5
  105. package/dist/chunk-s96k41p3.js.map +0 -10
  106. package/dist/chunk-wvnn9g55.js +0 -5
  107. package/dist/chunk-wvnn9g55.js.map +0 -10
  108. package/dist/compiled-template.js +0 -4
  109. package/dist/compiled-template.js.map +0 -9
  110. package/dist/errors.js +0 -4
  111. package/dist/errors.js.map +0 -9
  112. package/dist/executor.js +0 -4
  113. package/dist/executor.js.map +0 -9
  114. package/dist/helpers/index.d.ts +0 -4
  115. package/dist/index.d.ts +0 -3
  116. package/dist/index.js +0 -4
  117. package/dist/index.js.map +0 -9
  118. package/dist/parser.js +0 -4
  119. package/dist/parser.js.map +0 -9
  120. package/dist/schema-resolver.js +0 -4
  121. package/dist/schema-resolver.js.map +0 -9
  122. package/dist/typebars.js +0 -4
  123. package/dist/typebars.js.map +0 -14
  124. package/dist/types.js +0 -4
  125. package/dist/types.js.map +0 -9
  126. package/dist/utils.js +0 -4
  127. package/dist/utils.js.map +0 -9
  128. /package/dist/{helpers → cjs/helpers}/utils.d.ts +0 -0
  129. /package/dist/{parser.d.ts → cjs/parser.d.ts} +0 -0
  130. /package/dist/{schema-resolver.d.ts → cjs/schema-resolver.d.ts} +0 -0
  131. /package/dist/{types.d.ts → cjs/types.d.ts} +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/schema-resolver.ts"],"sourcesContent":["import type { JSONSchema7 } from \"json-schema\";\nimport { UnsupportedSchemaError } from \"./errors.ts\";\nimport { deepEqual } from \"./utils.ts\";\n\n// ─── JSON Schema Resolver ────────────────────────────────────────────────────\n// Utility for navigating a JSON Schema Draft v7 by following a property path\n// (e.g. [\"user\", \"address\", \"city\"]).\n//\n// Handles:\n// - `$ref` resolution (internal references #/definitions/...)\n// - Navigation through `properties`\n// - Navigation through `items` (array elements)\n// - Combinators `allOf`, `anyOf`, `oneOf` (searches each branch)\n// - `additionalProperties` when the property is not explicitly declared\n//\n// Rejects:\n// - Conditional schemas (`if/then/else`) — non-resolvable without runtime data\n\n// ─── Conditional Schema Detection ────────────────────────────────────────────\n// JSON Schema Draft v7 introduced `if/then/else` conditional schemas.\n// These are fundamentally non-resolvable during static analysis because\n// they depend on runtime data values. Rather than silently ignoring them\n// (which would produce incorrect results — missing properties, wrong types),\n// we fail fast with a clear error pointing to the exact location in the schema.\n\n/**\n * Recursively validates that a JSON Schema does not contain `if/then/else`\n * conditional keywords. Throws an `UnsupportedSchemaError` if any are found.\n *\n * This check traverses the entire schema tree, including:\n * - `properties` values\n * - `additionalProperties` (when it's a schema)\n * - `items` (single schema or tuple)\n * - `allOf`, `anyOf`, `oneOf` branches\n * - `not`\n * - `definitions` / `$defs` values\n *\n * A `Set<object>` is used to track visited schemas and prevent infinite loops\n * from circular structures.\n *\n * @param schema - The JSON Schema to validate\n * @param path - The current JSON pointer path (for error reporting)\n * @param visited - Set of already-visited schema objects (cycle protection)\n *\n * @throws {UnsupportedSchemaError} if `if`, `then`, or `else` is found\n *\n * @example\n * ```\n * // Throws UnsupportedSchemaError:\n * assertNoConditionalSchema({\n * type: \"object\",\n * if: { properties: { kind: { const: \"a\" } } },\n * then: { properties: { a: { type: \"string\" } } },\n * });\n *\n * // OK — no conditional keywords:\n * assertNoConditionalSchema({\n * type: \"object\",\n * properties: { name: { type: \"string\" } },\n * });\n * ```\n */\nexport function assertNoConditionalSchema(\n\tschema: JSONSchema7,\n\tpath = \"\",\n\tvisited: Set<object> = new Set(),\n): void {\n\t// Cycle protection — avoid infinite loops on circular schema structures\n\tif (visited.has(schema)) return;\n\tvisited.add(schema);\n\n\t// ── Detect if/then/else at the current level ─────────────────────────\n\tif (schema.if !== undefined) {\n\t\tthrow new UnsupportedSchemaError(\"if/then/else\", path || \"/\");\n\t}\n\t// `then` or `else` without `if` is unusual but still unsupported\n\tif (schema.then !== undefined) {\n\t\tthrow new UnsupportedSchemaError(\"if/then/else\", path || \"/\");\n\t}\n\tif (schema.else !== undefined) {\n\t\tthrow new UnsupportedSchemaError(\"if/then/else\", path || \"/\");\n\t}\n\n\t// ── Recurse into properties ──────────────────────────────────────────\n\tif (schema.properties) {\n\t\tfor (const [key, prop] of Object.entries(schema.properties)) {\n\t\t\tif (prop && typeof prop !== \"boolean\") {\n\t\t\t\tassertNoConditionalSchema(prop, `${path}/properties/${key}`, visited);\n\t\t\t}\n\t\t}\n\t}\n\n\t// ── Recurse into additionalProperties ────────────────────────────────\n\tif (\n\t\tschema.additionalProperties &&\n\t\ttypeof schema.additionalProperties === \"object\"\n\t) {\n\t\tassertNoConditionalSchema(\n\t\t\tschema.additionalProperties,\n\t\t\t`${path}/additionalProperties`,\n\t\t\tvisited,\n\t\t);\n\t}\n\n\t// ── Recurse into items ───────────────────────────────────────────────\n\tif (schema.items) {\n\t\tif (Array.isArray(schema.items)) {\n\t\t\tfor (let i = 0; i < schema.items.length; i++) {\n\t\t\t\tconst item = schema.items[i];\n\t\t\t\tif (item && typeof item !== \"boolean\") {\n\t\t\t\t\tassertNoConditionalSchema(item, `${path}/items/${i}`, visited);\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (typeof schema.items !== \"boolean\") {\n\t\t\tassertNoConditionalSchema(schema.items, `${path}/items`, visited);\n\t\t}\n\t}\n\n\t// ── Recurse into combinators ─────────────────────────────────────────\n\tfor (const keyword of [\"allOf\", \"anyOf\", \"oneOf\"] as const) {\n\t\tconst branches = schema[keyword];\n\t\tif (branches) {\n\t\t\tfor (let i = 0; i < branches.length; i++) {\n\t\t\t\tconst branch = branches[i];\n\t\t\t\tif (branch && typeof branch !== \"boolean\") {\n\t\t\t\t\tassertNoConditionalSchema(branch, `${path}/${keyword}/${i}`, visited);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t// ── Recurse into not ─────────────────────────────────────────────────\n\tif (schema.not && typeof schema.not !== \"boolean\") {\n\t\tassertNoConditionalSchema(schema.not, `${path}/not`, visited);\n\t}\n\n\t// ── Recurse into definitions / $defs ─────────────────────────────────\n\tfor (const defsKey of [\"definitions\", \"$defs\"] as const) {\n\t\tconst defs = schema[defsKey];\n\t\tif (defs) {\n\t\t\tfor (const [name, def] of Object.entries(defs)) {\n\t\t\t\tif (def && typeof def !== \"boolean\") {\n\t\t\t\t\tassertNoConditionalSchema(def, `${path}/${defsKey}/${name}`, visited);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\n// ─── $ref Resolution ─────────────────────────────────────────────────────────\n// Only supports internal references in the format `#/definitions/Foo`\n// or `#/$defs/Foo` (JSON Schema Draft 2019+). Remote $refs (URLs) are\n// not supported — that is outside the scope of a template engine.\n\n/**\n * Recursively resolves `$ref` in a schema using the root schema as the\n * source of definitions.\n */\nexport function resolveRef(\n\tschema: JSONSchema7,\n\troot: JSONSchema7,\n): JSONSchema7 {\n\tif (!schema.$ref) return schema;\n\n\tconst ref = schema.$ref;\n\n\t// Expected format: #/definitions/Name or #/$defs/Name\n\tconst match = ref.match(/^#\\/(definitions|\\$defs)\\/(.+)$/);\n\tif (!match) {\n\t\tthrow new Error(\n\t\t\t`Unsupported $ref format: \"${ref}\". Only internal #/definitions/ references are supported.`,\n\t\t);\n\t}\n\n\tconst defsKey = match[1] as \"definitions\" | \"$defs\";\n\tconst name = match[2] ?? \"\";\n\n\tconst defs = defsKey === \"definitions\" ? root.definitions : root.$defs;\n\n\tif (!defs || !(name in defs)) {\n\t\tthrow new Error(\n\t\t\t`Cannot resolve $ref \"${ref}\": definition \"${name}\" not found.`,\n\t\t);\n\t}\n\n\t// Recursive resolution in case the definition itself contains a $ref\n\tconst def = defs[name];\n\tif (!def || typeof def === \"boolean\") {\n\t\tthrow new Error(\n\t\t\t`Cannot resolve $ref \"${ref}\": definition \"${name}\" not found.`,\n\t\t);\n\t}\n\treturn resolveRef(def, root);\n}\n\n// ─── Single-Segment Path Navigation ─────────────────────────────────────────\n\n/**\n * Resolves a single path segment (a property name) within a schema.\n * Returns the corresponding sub-schema, or `undefined` if the path is invalid.\n *\n * @param schema - The current schema (already resolved, no $ref)\n * @param segment - The property name to resolve\n * @param root - The root schema (for resolving any internal $refs)\n */\nfunction resolveSegment(\n\tschema: JSONSchema7,\n\tsegment: string,\n\troot: JSONSchema7,\n): JSONSchema7 | undefined {\n\tconst resolved = resolveRef(schema, root);\n\n\t// 1. Explicit properties\n\tif (resolved.properties && segment in resolved.properties) {\n\t\tconst prop = resolved.properties[segment];\n\t\tif (prop && typeof prop !== \"boolean\") return resolveRef(prop, root);\n\t\tif (prop === true) return {};\n\t}\n\n\t// 2. additionalProperties (when the property is not declared)\n\tif (\n\t\tresolved.additionalProperties !== undefined &&\n\t\tresolved.additionalProperties !== false\n\t) {\n\t\tif (resolved.additionalProperties === true) {\n\t\t\t// additionalProperties: true → type is unknown\n\t\t\treturn {};\n\t\t}\n\t\treturn resolveRef(resolved.additionalProperties, root);\n\t}\n\n\t// 3. Intrinsic array properties (e.g. `.length`)\n\tconst schemaType = resolved.type;\n\tconst isArray =\n\t\tschemaType === \"array\" ||\n\t\t(Array.isArray(schemaType) && schemaType.includes(\"array\"));\n\n\tif (isArray && segment === \"length\") {\n\t\treturn { type: \"integer\" };\n\t}\n\n\t// 4. Combinators — search within each branch\n\tconst combinatorResult = resolveInCombinators(resolved, segment, root);\n\tif (combinatorResult) return combinatorResult;\n\n\treturn undefined;\n}\n\n/**\n * Searches for a segment within `allOf`, `anyOf`, `oneOf` branches.\n * Returns the first matching sub-schema, or `undefined`.\n * For `allOf`, found results are merged into a single `allOf`.\n */\nfunction resolveInCombinators(\n\tschema: JSONSchema7,\n\tsegment: string,\n\troot: JSONSchema7,\n): JSONSchema7 | undefined {\n\t// allOf: the property can be defined in any branch, and all constraints\n\t// apply simultaneously.\n\tif (schema.allOf) {\n\t\tconst matches = schema.allOf\n\t\t\t.filter((b): b is JSONSchema7 => typeof b !== \"boolean\")\n\t\t\t.map((branch) => resolveSegment(branch, segment, root))\n\t\t\t.filter((s): s is JSONSchema7 => s !== undefined);\n\n\t\tif (matches.length === 1) return matches[0] as JSONSchema7;\n\t\tif (matches.length > 1) return { allOf: matches };\n\t}\n\n\t// anyOf / oneOf: the property can come from any branch.\n\tfor (const key of [\"anyOf\", \"oneOf\"] as const) {\n\t\tif (!schema[key]) continue;\n\t\tconst matches = schema[key]\n\t\t\t.filter((b): b is JSONSchema7 => typeof b !== \"boolean\")\n\t\t\t.map((branch) => resolveSegment(branch, segment, root))\n\t\t\t.filter((s): s is JSONSchema7 => s !== undefined);\n\n\t\tif (matches.length === 1) return matches[0] as JSONSchema7;\n\t\tif (matches.length > 1) return { [key]: matches };\n\t}\n\n\treturn undefined;\n}\n\n// ─── Public API ──────────────────────────────────────────────────────────────\n\n/**\n * Resolves a full path (e.g. [\"user\", \"address\", \"city\"]) within a JSON\n * Schema and returns the corresponding sub-schema.\n *\n * @param schema - The root schema describing the template context\n * @param path - Array of segments (property names)\n * @returns The sub-schema at the end of the path, or `undefined` if the path\n * cannot be resolved.\n *\n * @example\n * ```\n * const schema = {\n * type: \"object\",\n * properties: {\n * user: {\n * type: \"object\",\n * properties: {\n * name: { type: \"string\" }\n * }\n * }\n * }\n * };\n * resolveSchemaPath(schema, [\"user\", \"name\"]);\n * // → { type: \"string\" }\n * ```\n */\nexport function resolveSchemaPath(\n\tschema: JSONSchema7,\n\tpath: string[],\n): JSONSchema7 | undefined {\n\tif (path.length === 0) return resolveRef(schema, schema);\n\n\tlet current: JSONSchema7 = resolveRef(schema, schema);\n\tconst root = schema;\n\n\tfor (const segment of path) {\n\t\tconst next = resolveSegment(current, segment, root);\n\t\tif (next === undefined) return undefined;\n\t\tcurrent = next;\n\t}\n\n\treturn current;\n}\n\n/**\n * Resolves the item schema of an array.\n * If the schema is not of type `array` or has no `items`, returns `undefined`.\n *\n * @param schema - The array schema\n * @param root - The root schema (for resolving $refs)\n */\nexport function resolveArrayItems(\n\tschema: JSONSchema7,\n\troot: JSONSchema7,\n): JSONSchema7 | undefined {\n\tconst resolved = resolveRef(schema, root);\n\n\t// Verify that it's actually an array\n\tconst schemaType = resolved.type;\n\tconst isArray =\n\t\tschemaType === \"array\" ||\n\t\t(Array.isArray(schemaType) && schemaType.includes(\"array\"));\n\n\tif (!isArray && resolved.items === undefined) {\n\t\treturn undefined;\n\t}\n\n\tif (resolved.items === undefined) {\n\t\t// array without items → element type is unknown\n\t\treturn {};\n\t}\n\n\t// items can be a boolean (true = anything, false = nothing)\n\tif (typeof resolved.items === \"boolean\") {\n\t\treturn {};\n\t}\n\n\t// items can be a single schema or a tuple (array of schemas).\n\t// For template loops, we handle the single-schema case.\n\tif (Array.isArray(resolved.items)) {\n\t\t// Tuple: create a oneOf of all possible types\n\t\tconst schemas = resolved.items\n\t\t\t.filter((item): item is JSONSchema7 => typeof item !== \"boolean\")\n\t\t\t.map((item) => resolveRef(item, root));\n\t\tif (schemas.length === 0) return {};\n\t\treturn { oneOf: schemas };\n\t}\n\n\treturn resolveRef(resolved.items, root);\n}\n\n/**\n * Simplifies an output schema to avoid unnecessarily complex constructs\n * (e.g. `oneOf` with a single element, duplicates, etc.).\n *\n * Uses `deepEqual` for deduplication — more robust and performant than\n * `JSON.stringify` (independent of key order, no intermediate string\n * allocations).\n */\nexport function simplifySchema(schema: JSONSchema7): JSONSchema7 {\n\t// oneOf / anyOf with a single element → unwrap\n\tfor (const key of [\"oneOf\", \"anyOf\"] as const) {\n\t\tconst arr = schema[key];\n\t\tif (arr && arr.length === 1) {\n\t\t\tconst first = arr[0];\n\t\t\tif (first !== undefined && typeof first !== \"boolean\")\n\t\t\t\treturn simplifySchema(first);\n\t\t}\n\t}\n\n\t// allOf with a single element → unwrap\n\tif (schema.allOf && schema.allOf.length === 1) {\n\t\tconst first = schema.allOf[0];\n\t\tif (first !== undefined && typeof first !== \"boolean\")\n\t\t\treturn simplifySchema(first);\n\t}\n\n\t// Deduplicate identical entries in oneOf/anyOf\n\tfor (const key of [\"oneOf\", \"anyOf\"] as const) {\n\t\tconst arr = schema[key];\n\t\tif (arr && arr.length > 1) {\n\t\t\tconst unique: JSONSchema7[] = [];\n\t\t\tfor (const entry of arr) {\n\t\t\t\tif (typeof entry === \"boolean\") continue;\n\t\t\t\t// Use deepEqual instead of JSON.stringify for structural\n\t\t\t\t// comparison — more robust (key order independent) and\n\t\t\t\t// more performant (no string allocations).\n\t\t\t\tconst isDuplicate = unique.some((existing) =>\n\t\t\t\t\tdeepEqual(existing, entry),\n\t\t\t\t);\n\t\t\t\tif (!isDuplicate) {\n\t\t\t\t\tunique.push(simplifySchema(entry));\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (unique.length === 1) return unique[0] as JSONSchema7;\n\t\t\treturn { ...schema, [key]: unique };\n\t\t}\n\t}\n\n\treturn schema;\n}\n"],"names":["UnsupportedSchemaError","deepEqual","assertNoConditionalSchema","schema","path","visited","Set","has","add","if","undefined","then","else","properties","key","prop","Object","entries","additionalProperties","items","Array","isArray","i","length","item","keyword","branches","branch","not","defsKey","defs","name","def","resolveRef","root","$ref","ref","match","Error","definitions","$defs","resolveSegment","segment","resolved","schemaType","type","includes","combinatorResult","resolveInCombinators","allOf","matches","filter","b","map","s","resolveSchemaPath","current","next","resolveArrayItems","schemas","oneOf","simplifySchema","arr","first","unique","entry","isDuplicate","some","existing","push"],"mappings":"AACA,OAASA,sBAAsB,KAAQ,aAAc,AACrD,QAASC,SAAS,KAAQ,YAAa,AA4DvC,QAAO,SAASC,0BACfC,MAAmB,CACnBC,KAAO,EAAE,CACTC,QAAuB,IAAIC,GAAK,EAGhC,GAAID,QAAQE,GAAG,CAACJ,QAAS,OACzBE,QAAQG,GAAG,CAACL,QAGZ,GAAIA,OAAOM,EAAE,GAAKC,UAAW,CAC5B,MAAM,IAAIV,uBAAuB,eAAgBI,MAAQ,IAC1D,CAEA,GAAID,OAAOQ,IAAI,GAAKD,UAAW,CAC9B,MAAM,IAAIV,uBAAuB,eAAgBI,MAAQ,IAC1D,CACA,GAAID,OAAOS,IAAI,GAAKF,UAAW,CAC9B,MAAM,IAAIV,uBAAuB,eAAgBI,MAAQ,IAC1D,CAGA,GAAID,OAAOU,UAAU,CAAE,CACtB,IAAK,KAAM,CAACC,IAAKC,KAAK,GAAIC,OAAOC,OAAO,CAACd,OAAOU,UAAU,EAAG,CAC5D,GAAIE,MAAQ,OAAOA,OAAS,UAAW,CACtCb,0BAA0Ba,KAAM,CAAC,EAAEX,KAAK,YAAY,EAAEU,IAAI,CAAC,CAAET,QAC9D,CACD,CACD,CAGA,GACCF,OAAOe,oBAAoB,EAC3B,OAAOf,OAAOe,oBAAoB,GAAK,SACtC,CACDhB,0BACCC,OAAOe,oBAAoB,CAC3B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1B,WAAWU,SAASxB,KAAK,CAAEe,KACnC,CAUA,OAAO,SAAS2B,eAAe1D,MAAmB,EAEjD,IAAK,MAAMW,MAAO,CAAC,QAAS,QAAQ,CAAW,CAC9C,MAAMgD,IAAM3D,MAAM,CAACW,IAAI,CACvB,GAAIgD,KAAOA,IAAIvC,MAAM,GAAK,EAAG,CAC5B,MAAMwC,MAAQD,GAAG,CAAC,EAAE,CACpB,GAAIC,QAAUrD,WAAa,OAAOqD,QAAU,UAC3C,OAAOF,eAAeE,MACxB,CACD,CAGA,GAAI5D,OAAO8C,KAAK,EAAI9C,OAAO8C,KAAK,CAAC1B,MAAM,GAAK,EAAG,CAC9C,MAAMwC,MAAQ5D,OAAO8C,KAAK,CAAC,EAAE,CAC7B,GAAIc,QAAUrD,WAAa,OAAOqD,QAAU,UAC3C,OAAOF,eAAeE,MACxB,CAGA,IAAK,MAAMjD,MAAO,CAAC,QAAS,QAAQ,CAAW,CAC9C,MAAMgD,IAAM3D,MAAM,CAACW,IAAI,CACvB,GAAIgD,KAAOA,IAAIvC,MAAM,CAAG,EAAG,CAC1B,MAAMyC,OAAwB,EAAE,CAChC,IAAK,MAAMC,SAASH,IAAK,CACxB,GAAI,OAAOG,QAAU,UAAW,SAIhC,MAAMC,YAAcF,OAAOG,IAAI,CAAC,AAACC,UAChCnE,UAAUmE,SAAUH,QAErB,GAAI,CAACC,YAAa,CACjBF,OAAOK,IAAI,CAACR,eAAeI,OAC5B,CACD,CACA,GAAID,OAAOzC,MAAM,GAAK,EAAG,OAAOyC,MAAM,CAAC,EAAE,CACzC,MAAO,CAAE,GAAG7D,MAAM,CAAE,CAACW,IAAI,CAAEkD,MAAO,CACnC,CACD,CAEA,OAAO7D,MACR"}
@@ -0,0 +1,130 @@
1
+ import type { JSONSchema7 } from "json-schema";
2
+ import { CompiledTemplate } from "./compiled-template.js";
3
+ import type { AnalysisResult, AnalyzeAndExecuteOptions, ExecuteOptions, HelperDefinition, TemplateEngineOptions, TemplateInput, ValidationResult } from "./types.js";
4
+ export declare class Typebars {
5
+ /** Isolated Handlebars environment — each engine has its own helpers */
6
+ private readonly hbs;
7
+ /** LRU cache of parsed ASTs (avoids re-parsing) */
8
+ private readonly astCache;
9
+ /** LRU cache of compiled Handlebars templates (avoids recompilation) */
10
+ private readonly compilationCache;
11
+ /** Custom helpers registered on this instance */
12
+ private readonly helpers;
13
+ constructor(options?: TemplateEngineOptions);
14
+ /**
15
+ * Compiles a template and returns a `CompiledTemplate` ready to be
16
+ * executed or analyzed without re-parsing.
17
+ *
18
+ * Accepts a `TemplateInput`: string, number, boolean, null, or object.
19
+ * For objects, each property is compiled recursively.
20
+ *
21
+ * @param template - The template to compile
22
+ * @returns A reusable `CompiledTemplate`
23
+ */
24
+ compile(template: TemplateInput): CompiledTemplate;
25
+ /**
26
+ * Statically analyzes a template against a JSON Schema v7 describing
27
+ * the available context.
28
+ *
29
+ * Accepts a `TemplateInput`: string, number, boolean, null, or object.
30
+ * For objects, each property is analyzed recursively and the
31
+ * `outputSchema` reflects the object structure with resolved types.
32
+ *
33
+ * @param template - The template to analyze
34
+ * @param inputSchema - JSON Schema v7 describing the available variables
35
+ * @param identifierSchemas - (optional) Schemas by identifier `{ [id]: JSONSchema7 }`
36
+ */
37
+ analyze(template: TemplateInput, inputSchema: JSONSchema7, identifierSchemas?: Record<number, JSONSchema7>): AnalysisResult;
38
+ /**
39
+ * Validates a template against a schema without returning the output type.
40
+ *
41
+ * This is an API shortcut for `analyze()` that only returns `valid` and
42
+ * `diagnostics`, without `outputSchema`. The full analysis (including type
43
+ * inference) is executed internally — this method provides no performance
44
+ * gain, only a simplified API.
45
+ *
46
+ * @param template - The template to validate
47
+ * @param inputSchema - JSON Schema v7 describing the available variables
48
+ * @param identifierSchemas - (optional) Schemas by identifier
49
+ */
50
+ validate(template: TemplateInput, inputSchema: JSONSchema7, identifierSchemas?: Record<number, JSONSchema7>): ValidationResult;
51
+ /**
52
+ * Checks only that the template syntax is valid (parsing).
53
+ * Does not require a schema — useful for quick feedback in an editor.
54
+ *
55
+ * For objects, recursively checks each property.
56
+ *
57
+ * @param template - The template to validate
58
+ * @returns `true` if the template is syntactically correct
59
+ */
60
+ isValidSyntax(template: TemplateInput): boolean;
61
+ /**
62
+ * Executes a template with the provided data.
63
+ *
64
+ * Accepts a `TemplateInput`: string, number, boolean, null, or object.
65
+ * For objects, each property is executed recursively and an object with
66
+ * resolved values is returned.
67
+ *
68
+ * If a `schema` is provided in options, static analysis is performed
69
+ * before execution. A `TemplateAnalysisError` is thrown on errors.
70
+ *
71
+ * @param template - The template to execute
72
+ * @param data - The context data for rendering
73
+ * @param options - Execution options (schema, identifierData, identifierSchemas)
74
+ * @returns The execution result
75
+ */
76
+ execute(template: TemplateInput, data: Record<string, unknown>, options?: ExecuteOptions): unknown;
77
+ /**
78
+ * Analyzes a template and, if valid, executes it with the provided data.
79
+ * Returns both the analysis result and the executed value.
80
+ *
81
+ * For objects, each property is analyzed and executed recursively.
82
+ * The entire object is considered invalid if at least one property is.
83
+ *
84
+ * @param template - The template
85
+ * @param inputSchema - JSON Schema v7 describing the available variables
86
+ * @param data - The context data for rendering
87
+ * @param options - (optional) Options for template identifiers
88
+ * @returns An object `{ analysis, value }` where `value` is `undefined`
89
+ * if analysis failed.
90
+ */
91
+ analyzeAndExecute(template: TemplateInput, inputSchema: JSONSchema7, data: Record<string, unknown>, options?: AnalyzeAndExecuteOptions): {
92
+ analysis: AnalysisResult;
93
+ value: unknown;
94
+ };
95
+ /**
96
+ * Registers a custom helper on this engine instance.
97
+ *
98
+ * The helper is available for both execution (via Handlebars) and
99
+ * static analysis (via its declared `returnType`).
100
+ *
101
+ * @param name - Helper name (e.g. `"uppercase"`)
102
+ * @param definition - Helper definition (implementation + return type)
103
+ * @returns `this` to allow chaining
104
+ */
105
+ registerHelper(name: string, definition: HelperDefinition): this;
106
+ /**
107
+ * Removes a custom helper from this engine instance.
108
+ *
109
+ * @param name - Name of the helper to remove
110
+ * @returns `this` to allow chaining
111
+ */
112
+ unregisterHelper(name: string): this;
113
+ /**
114
+ * Checks whether a helper is registered on this instance.
115
+ *
116
+ * @param name - Helper name
117
+ * @returns `true` if the helper is registered
118
+ */
119
+ hasHelper(name: string): boolean;
120
+ /**
121
+ * Clears all internal caches (AST + compilation).
122
+ *
123
+ * Useful after a configuration change or to free memory.
124
+ */
125
+ clearCaches(): void;
126
+ /**
127
+ * Retrieves the AST of a template from the cache, or parses and caches it.
128
+ */
129
+ private getCachedAst;
130
+ }
@@ -0,0 +1,2 @@
1
+ function _define_property(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true})}else{obj[key]=value}return obj}import Handlebars from"handlebars";import{analyzeFromAst}from"./analyzer.js";import{CompiledTemplate}from"./compiled-template.js";import{TemplateAnalysisError}from"./errors.js";import{executeFromAst}from"./executor.js";import{LogicalHelpers,MathHelpers}from"./helpers/index.js";import{parse}from"./parser.js";import{inferPrimitiveSchema,isLiteralInput,isObjectInput}from"./types.js";import{aggregateObjectAnalysis,aggregateObjectAnalysisAndExecution,LRUCache}from"./utils.js";export class Typebars{compile(template){if(isObjectInput(template)){const children={};for(const[key,value]of Object.entries(template)){children[key]=this.compile(value)}return CompiledTemplate.fromObject(children,{helpers:this.helpers,hbs:this.hbs,compilationCache:this.compilationCache})}if(isLiteralInput(template)){return CompiledTemplate.fromLiteral(template,{helpers:this.helpers,hbs:this.hbs,compilationCache:this.compilationCache})}const ast=this.getCachedAst(template);const options={helpers:this.helpers,hbs:this.hbs,compilationCache:this.compilationCache};return CompiledTemplate.fromTemplate(ast,template,options)}analyze(template,inputSchema,identifierSchemas){if(isObjectInput(template)){return aggregateObjectAnalysis(Object.keys(template),key=>this.analyze(template[key],inputSchema,identifierSchemas))}if(isLiteralInput(template)){return{valid:true,diagnostics:[],outputSchema:inferPrimitiveSchema(template)}}const ast=this.getCachedAst(template);return analyzeFromAst(ast,template,inputSchema,{identifierSchemas,helpers:this.helpers})}validate(template,inputSchema,identifierSchemas){const analysis=this.analyze(template,inputSchema,identifierSchemas);return{valid:analysis.valid,diagnostics:analysis.diagnostics}}isValidSyntax(template){if(isObjectInput(template)){return Object.values(template).every(v=>this.isValidSyntax(v))}if(isLiteralInput(template))return true;try{this.getCachedAst(template);return true}catch{return false}}execute(template,data,options){if(isObjectInput(template)){const result={};for(const[key,value]of Object.entries(template)){result[key]=this.execute(value,data,options)}return result}if(isLiteralInput(template))return template;const ast=this.getCachedAst(template);if(options?.schema){const analysis=analyzeFromAst(ast,template,options.schema,{identifierSchemas:options.identifierSchemas,helpers:this.helpers});if(!analysis.valid){throw new TemplateAnalysisError(analysis.diagnostics)}}return executeFromAst(ast,template,data,{identifierData:options?.identifierData,hbs:this.hbs,compilationCache:this.compilationCache})}analyzeAndExecute(template,inputSchema,data,options){if(isObjectInput(template)){return aggregateObjectAnalysisAndExecution(Object.keys(template),key=>this.analyzeAndExecute(template[key],inputSchema,data,options))}if(isLiteralInput(template)){return{analysis:{valid:true,diagnostics:[],outputSchema:inferPrimitiveSchema(template)},value:template}}const ast=this.getCachedAst(template);const analysis=analyzeFromAst(ast,template,inputSchema,{identifierSchemas:options?.identifierSchemas,helpers:this.helpers});if(!analysis.valid){return{analysis,value:undefined}}const value=executeFromAst(ast,template,data,{identifierData:options?.identifierData,hbs:this.hbs,compilationCache:this.compilationCache});return{analysis,value}}registerHelper(name,definition){this.helpers.set(name,definition);this.hbs.registerHelper(name,definition.fn);this.compilationCache.clear();return this}unregisterHelper(name){this.helpers.delete(name);this.hbs.unregisterHelper(name);this.compilationCache.clear();return this}hasHelper(name){return this.helpers.has(name)}clearCaches(){this.astCache.clear();this.compilationCache.clear()}getCachedAst(template){let ast=this.astCache.get(template);if(!ast){ast=parse(template);this.astCache.set(template,ast)}return ast}constructor(options={}){_define_property(this,"hbs",void 0);_define_property(this,"astCache",void 0);_define_property(this,"compilationCache",void 0);_define_property(this,"helpers",new Map);this.hbs=Handlebars.create();this.astCache=new LRUCache(options.astCacheSize??256);this.compilationCache=new LRUCache(options.compilationCacheSize??256);new MathHelpers().register(this);new LogicalHelpers().register(this);if(options.helpers){for(const helper of options.helpers){const{name,...definition}=helper;this.registerHelper(name,definition)}}}}
2
+ //# sourceMappingURL=typebars.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/typebars.ts"],"sourcesContent":["import Handlebars from \"handlebars\";\nimport type { JSONSchema7 } from \"json-schema\";\nimport { analyzeFromAst } from \"./analyzer.ts\";\nimport {\n\tCompiledTemplate,\n\ttype CompiledTemplateOptions,\n} from \"./compiled-template.ts\";\nimport { TemplateAnalysisError } from \"./errors.ts\";\nimport { executeFromAst } from \"./executor.ts\";\nimport { LogicalHelpers, MathHelpers } from \"./helpers/index.ts\";\nimport { parse } from \"./parser.ts\";\nimport type {\n\tAnalysisResult,\n\tAnalyzeAndExecuteOptions,\n\tExecuteOptions,\n\tHelperDefinition,\n\tTemplateEngineOptions,\n\tTemplateInput,\n\tValidationResult,\n} from \"./types.ts\";\nimport {\n\tinferPrimitiveSchema,\n\tisLiteralInput,\n\tisObjectInput,\n} from \"./types.ts\";\nimport {\n\taggregateObjectAnalysis,\n\taggregateObjectAnalysisAndExecution,\n\tLRUCache,\n} from \"./utils\";\n\n// ─── Typebars ────────────────────────────────────────────────────────────────\n// Public entry point of the template engine. Orchestrates three phases:\n//\n// 1. **Parsing** — transforms the template string into an AST (via Handlebars)\n// 2. **Analysis** — static validation + return type inference\n// 3. **Execution** — renders the template with real data\n//\n// ─── Architecture v2 ─────────────────────────────────────────────────────────\n// - **LRU cache** for parsed ASTs and compiled Handlebars templates\n// - **Isolated Handlebars environment** per instance (custom helpers)\n// - **`compile()` pattern**: parse-once / execute-many\n// - **`validate()` method**: API shortcut without `outputSchema`\n// - **`registerHelper()`**: custom helpers with static typing\n// - **`ExecuteOptions`**: options object for `execute()`\n//\n// ─── Template Identifiers ────────────────────────────────────────────────────\n// The `{{key:N}}` syntax allows referencing variables from specific data\n// sources, identified by an integer N.\n//\n// - `identifierSchemas`: mapping `{ [id]: JSONSchema7 }` for static analysis\n// - `identifierData`: mapping `{ [id]: Record<string, unknown> }` for execution\n//\n// Usage:\n// engine.execute(\"{{meetingId:1}}\", data, { identifierData: { 1: node1Data } });\n// engine.analyze(\"{{meetingId:1}}\", schema, { 1: node1Schema });\n\n// ─── Main Class ──────────────────────────────────────────────────────────────\n\nexport class Typebars {\n\t/** Isolated Handlebars environment — each engine has its own helpers */\n\tprivate readonly hbs: typeof Handlebars;\n\n\t/** LRU cache of parsed ASTs (avoids re-parsing) */\n\tprivate readonly astCache: LRUCache<string, hbs.AST.Program>;\n\n\t/** LRU cache of compiled Handlebars templates (avoids recompilation) */\n\tprivate readonly compilationCache: LRUCache<\n\t\tstring,\n\t\tHandlebarsTemplateDelegate\n\t>;\n\n\t/** Custom helpers registered on this instance */\n\tprivate readonly helpers = new Map<string, HelperDefinition>();\n\n\tconstructor(options: TemplateEngineOptions = {}) {\n\t\tthis.hbs = Handlebars.create();\n\t\tthis.astCache = new LRUCache(options.astCacheSize ?? 256);\n\t\tthis.compilationCache = new LRUCache(options.compilationCacheSize ?? 256);\n\n\t\t// ── Built-in helpers ─────────────────────────────────────────────\n\t\tnew MathHelpers().register(this);\n\t\tnew LogicalHelpers().register(this);\n\n\t\t// ── Custom helpers via options ───────────────────────────────────\n\t\tif (options.helpers) {\n\t\t\tfor (const helper of options.helpers) {\n\t\t\t\tconst { name, ...definition } = helper;\n\t\t\t\tthis.registerHelper(name, definition);\n\t\t\t}\n\t\t}\n\t}\n\n\t// ─── Compilation ───────────────────────────────────────────────────────\n\n\t/**\n\t * Compiles a template and returns a `CompiledTemplate` ready to be\n\t * executed or analyzed without re-parsing.\n\t *\n\t * Accepts a `TemplateInput`: string, number, boolean, null, or object.\n\t * For objects, each property is compiled recursively.\n\t *\n\t * @param template - The template to compile\n\t * @returns A reusable `CompiledTemplate`\n\t */\n\tcompile(template: TemplateInput): CompiledTemplate {\n\t\tif (isObjectInput(template)) {\n\t\t\tconst children: Record<string, CompiledTemplate> = {};\n\t\t\tfor (const [key, value] of Object.entries(template)) {\n\t\t\t\tchildren[key] = this.compile(value);\n\t\t\t}\n\t\t\treturn CompiledTemplate.fromObject(children, {\n\t\t\t\thelpers: this.helpers,\n\t\t\t\thbs: this.hbs,\n\t\t\t\tcompilationCache: this.compilationCache,\n\t\t\t});\n\t\t}\n\t\tif (isLiteralInput(template)) {\n\t\t\treturn CompiledTemplate.fromLiteral(template, {\n\t\t\t\thelpers: this.helpers,\n\t\t\t\thbs: this.hbs,\n\t\t\t\tcompilationCache: this.compilationCache,\n\t\t\t});\n\t\t}\n\t\tconst ast = this.getCachedAst(template);\n\t\tconst options: CompiledTemplateOptions = {\n\t\t\thelpers: this.helpers,\n\t\t\thbs: this.hbs,\n\t\t\tcompilationCache: this.compilationCache,\n\t\t};\n\t\treturn CompiledTemplate.fromTemplate(ast, template, options);\n\t}\n\n\t// ─── Static Analysis ─────────────────────────────────────────────────────\n\n\t/**\n\t * Statically analyzes a template against a JSON Schema v7 describing\n\t * the available context.\n\t *\n\t * Accepts a `TemplateInput`: string, number, boolean, null, or object.\n\t * For objects, each property is analyzed recursively and the\n\t * `outputSchema` reflects the object structure with resolved types.\n\t *\n\t * @param template - The template to analyze\n\t * @param inputSchema - JSON Schema v7 describing the available variables\n\t * @param identifierSchemas - (optional) Schemas by identifier `{ [id]: JSONSchema7 }`\n\t */\n\tanalyze(\n\t\ttemplate: TemplateInput,\n\t\tinputSchema: JSONSchema7,\n\t\tidentifierSchemas?: Record<number, JSONSchema7>,\n\t): AnalysisResult {\n\t\tif (isObjectInput(template)) {\n\t\t\treturn aggregateObjectAnalysis(Object.keys(template), (key) =>\n\t\t\t\tthis.analyze(\n\t\t\t\t\ttemplate[key] as TemplateInput,\n\t\t\t\t\tinputSchema,\n\t\t\t\t\tidentifierSchemas,\n\t\t\t\t),\n\t\t\t);\n\t\t}\n\t\tif (isLiteralInput(template)) {\n\t\t\treturn {\n\t\t\t\tvalid: true,\n\t\t\t\tdiagnostics: [],\n\t\t\t\toutputSchema: inferPrimitiveSchema(template),\n\t\t\t};\n\t\t}\n\t\tconst ast = this.getCachedAst(template);\n\t\treturn analyzeFromAst(ast, template, inputSchema, {\n\t\t\tidentifierSchemas,\n\t\t\thelpers: this.helpers,\n\t\t});\n\t}\n\n\t// ─── Validation ──────────────────────────────────────────────────────────\n\n\t/**\n\t * Validates a template against a schema without returning the output type.\n\t *\n\t * This is an API shortcut for `analyze()` that only returns `valid` and\n\t * `diagnostics`, without `outputSchema`. The full analysis (including type\n\t * inference) is executed internally — this method provides no performance\n\t * gain, only a simplified API.\n\t *\n\t * @param template - The template to validate\n\t * @param inputSchema - JSON Schema v7 describing the available variables\n\t * @param identifierSchemas - (optional) Schemas by identifier\n\t */\n\tvalidate(\n\t\ttemplate: TemplateInput,\n\t\tinputSchema: JSONSchema7,\n\t\tidentifierSchemas?: Record<number, JSONSchema7>,\n\t): ValidationResult {\n\t\tconst analysis = this.analyze(template, inputSchema, identifierSchemas);\n\t\treturn {\n\t\t\tvalid: analysis.valid,\n\t\t\tdiagnostics: analysis.diagnostics,\n\t\t};\n\t}\n\n\t// ─── Syntax Validation ───────────────────────────────────────────────────\n\n\t/**\n\t * Checks only that the template syntax is valid (parsing).\n\t * Does not require a schema — useful for quick feedback in an editor.\n\t *\n\t * For objects, recursively checks each property.\n\t *\n\t * @param template - The template to validate\n\t * @returns `true` if the template is syntactically correct\n\t */\n\tisValidSyntax(template: TemplateInput): boolean {\n\t\tif (isObjectInput(template)) {\n\t\t\treturn Object.values(template).every((v) => this.isValidSyntax(v));\n\t\t}\n\t\tif (isLiteralInput(template)) return true;\n\t\ttry {\n\t\t\tthis.getCachedAst(template);\n\t\t\treturn true;\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t// ─── Execution ───────────────────────────────────────────────────────────\n\n\t/**\n\t * Executes a template with the provided data.\n\t *\n\t * Accepts a `TemplateInput`: string, number, boolean, null, or object.\n\t * For objects, each property is executed recursively and an object with\n\t * resolved values is returned.\n\t *\n\t * If a `schema` is provided in options, static analysis is performed\n\t * before execution. A `TemplateAnalysisError` is thrown on errors.\n\t *\n\t * @param template - The template to execute\n\t * @param data - The context data for rendering\n\t * @param options - Execution options (schema, identifierData, identifierSchemas)\n\t * @returns The execution result\n\t */\n\texecute(\n\t\ttemplate: TemplateInput,\n\t\tdata: Record<string, unknown>,\n\t\toptions?: ExecuteOptions,\n\t): unknown {\n\t\t// ── Object template → recursive execution ────────────────────────────\n\t\tif (isObjectInput(template)) {\n\t\t\tconst result: Record<string, unknown> = {};\n\t\t\tfor (const [key, value] of Object.entries(template)) {\n\t\t\t\tresult[key] = this.execute(value, data, options);\n\t\t\t}\n\t\t\treturn result;\n\t\t}\n\n\t\t// ── Passthrough for literal values ────────────────────────────────────\n\t\tif (isLiteralInput(template)) return template;\n\n\t\t// ── Parse once ───────────────────────────────────────────────────────\n\t\tconst ast = this.getCachedAst(template);\n\n\t\t// ── Pre-execution static validation ──────────────────────────────────\n\t\tif (options?.schema) {\n\t\t\tconst analysis = analyzeFromAst(ast, template, options.schema, {\n\t\t\t\tidentifierSchemas: options.identifierSchemas,\n\t\t\t\thelpers: this.helpers,\n\t\t\t});\n\t\t\tif (!analysis.valid) {\n\t\t\t\tthrow new TemplateAnalysisError(analysis.diagnostics);\n\t\t\t}\n\t\t}\n\n\t\t// ── Execution ────────────────────────────────────────────────────────\n\t\treturn executeFromAst(ast, template, data, {\n\t\t\tidentifierData: options?.identifierData,\n\t\t\thbs: this.hbs,\n\t\t\tcompilationCache: this.compilationCache,\n\t\t});\n\t}\n\n\t// ─── Combined Shortcuts ──────────────────────────────────────────────────\n\n\t/**\n\t * Analyzes a template and, if valid, executes it with the provided data.\n\t * Returns both the analysis result and the executed value.\n\t *\n\t * For objects, each property is analyzed and executed recursively.\n\t * The entire object is considered invalid if at least one property is.\n\t *\n\t * @param template - The template\n\t * @param inputSchema - JSON Schema v7 describing the available variables\n\t * @param data - The context data for rendering\n\t * @param options - (optional) Options for template identifiers\n\t * @returns An object `{ analysis, value }` where `value` is `undefined`\n\t * if analysis failed.\n\t */\n\tanalyzeAndExecute(\n\t\ttemplate: TemplateInput,\n\t\tinputSchema: JSONSchema7,\n\t\tdata: Record<string, unknown>,\n\t\toptions?: AnalyzeAndExecuteOptions,\n\t): { analysis: AnalysisResult; value: unknown } {\n\t\tif (isObjectInput(template)) {\n\t\t\treturn aggregateObjectAnalysisAndExecution(Object.keys(template), (key) =>\n\t\t\t\tthis.analyzeAndExecute(\n\t\t\t\t\ttemplate[key] as TemplateInput,\n\t\t\t\t\tinputSchema,\n\t\t\t\t\tdata,\n\t\t\t\t\toptions,\n\t\t\t\t),\n\t\t\t);\n\t\t}\n\n\t\tif (isLiteralInput(template)) {\n\t\t\treturn {\n\t\t\t\tanalysis: {\n\t\t\t\t\tvalid: true,\n\t\t\t\t\tdiagnostics: [],\n\t\t\t\t\toutputSchema: inferPrimitiveSchema(template),\n\t\t\t\t},\n\t\t\t\tvalue: template,\n\t\t\t};\n\t\t}\n\n\t\tconst ast = this.getCachedAst(template);\n\t\tconst analysis = analyzeFromAst(ast, template, inputSchema, {\n\t\t\tidentifierSchemas: options?.identifierSchemas,\n\t\t\thelpers: this.helpers,\n\t\t});\n\n\t\tif (!analysis.valid) {\n\t\t\treturn { analysis, value: undefined };\n\t\t}\n\n\t\tconst value = executeFromAst(ast, template, data, {\n\t\t\tidentifierData: options?.identifierData,\n\t\t\thbs: this.hbs,\n\t\t\tcompilationCache: this.compilationCache,\n\t\t});\n\t\treturn { analysis, value };\n\t}\n\n\t// ─── Custom Helper Management ──────────────────────────────────────────\n\n\t/**\n\t * Registers a custom helper on this engine instance.\n\t *\n\t * The helper is available for both execution (via Handlebars) and\n\t * static analysis (via its declared `returnType`).\n\t *\n\t * @param name - Helper name (e.g. `\"uppercase\"`)\n\t * @param definition - Helper definition (implementation + return type)\n\t * @returns `this` to allow chaining\n\t */\n\tregisterHelper(name: string, definition: HelperDefinition): this {\n\t\tthis.helpers.set(name, definition);\n\t\tthis.hbs.registerHelper(name, definition.fn);\n\n\t\t// Invalidate the compilation cache because helpers have changed\n\t\tthis.compilationCache.clear();\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Removes a custom helper from this engine instance.\n\t *\n\t * @param name - Name of the helper to remove\n\t * @returns `this` to allow chaining\n\t */\n\tunregisterHelper(name: string): this {\n\t\tthis.helpers.delete(name);\n\t\tthis.hbs.unregisterHelper(name);\n\n\t\t// Invalidate the compilation cache\n\t\tthis.compilationCache.clear();\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Checks whether a helper is registered on this instance.\n\t *\n\t * @param name - Helper name\n\t * @returns `true` if the helper is registered\n\t */\n\thasHelper(name: string): boolean {\n\t\treturn this.helpers.has(name);\n\t}\n\n\t// ─── Cache Management ──────────────────────────────────────────────────\n\n\t/**\n\t * Clears all internal caches (AST + compilation).\n\t *\n\t * Useful after a configuration change or to free memory.\n\t */\n\tclearCaches(): void {\n\t\tthis.astCache.clear();\n\t\tthis.compilationCache.clear();\n\t}\n\n\t// ─── Internals ─────────────────────────────────────────────────────────\n\n\t/**\n\t * Retrieves the AST of a template from the cache, or parses and caches it.\n\t */\n\tprivate getCachedAst(template: string): hbs.AST.Program {\n\t\tlet ast = this.astCache.get(template);\n\t\tif (!ast) {\n\t\t\tast = parse(template);\n\t\t\tthis.astCache.set(template, ast);\n\t\t}\n\t\treturn ast;\n\t}\n}\n"],"names":["Handlebars","analyzeFromAst","CompiledTemplate","TemplateAnalysisError","executeFromAst","LogicalHelpers","MathHelpers","parse","inferPrimitiveSchema","isLiteralInput","isObjectInput","aggregateObjectAnalysis","aggregateObjectAnalysisAndExecution","LRUCache","Typebars","compile","template","children","key","value","Object","entries","fromObject","helpers","hbs","compilationCache","fromLiteral","ast","getCachedAst","options","fromTemplate","analyze","inputSchema","identifierSchemas","keys","valid","diagnostics","outputSchema","validate","analysis","isValidSyntax","values","every","v","execute","data","result","schema","identifierData","analyzeAndExecute","undefined","registerHelper","name","definition","set","fn","clear","unregisterHelper","delete","hasHelper","has","clearCaches","astCache","get","Map","create","astCacheSize","compilationCacheSize","register","helper"],"mappings":"oLAAA,OAAOA,eAAgB,YAAa,AAEpC,QAASC,cAAc,KAAQ,eAAgB,AAC/C,QACCC,gBAAgB,KAEV,wBAAyB,AAChC,QAASC,qBAAqB,KAAQ,aAAc,AACpD,QAASC,cAAc,KAAQ,eAAgB,AAC/C,QAASC,cAAc,CAAEC,WAAW,KAAQ,oBAAqB,AACjE,QAASC,KAAK,KAAQ,aAAc,AAUpC,QACCC,oBAAoB,CACpBC,cAAc,CACdC,aAAa,KACP,YAAa,AACpB,QACCC,uBAAuB,CACvBC,mCAAmC,CACnCC,QAAQ,KACF,SAAU,AA8BjB,QAAO,MAAMC,SA8CZC,QAAQC,QAAuB,CAAoB,CAClD,GAAIN,cAAcM,UAAW,CAC5B,MAAMC,SAA6C,CAAC,EACpD,IAAK,KAAM,CAACC,IAAKC,MAAM,GAAIC,OAAOC,OAAO,CAACL,UAAW,CACpDC,QAAQ,CAACC,IAAI,CAAG,IAAI,CAACH,OAAO,CAACI,MAC9B,CACA,OAAOjB,iBAAiBoB,UAAU,CAACL,SAAU,CAC5CM,QAAS,IAAI,CAACA,OAAO,CACrBC,IAAK,IAAI,CAACA,GAAG,CACbC,iBAAkB,IAAI,CAACA,gBAAgB,AACxC,EACD,CACA,GAAIhB,eAAeO,UAAW,CAC7B,OAAOd,iBAAiBwB,WAAW,CAACV,SAAU,CAC7CO,QAAS,IAAI,CAACA,OAAO,CACrBC,IAAK,IAAI,CAACA,GAAG,CACbC,iBAAkB,IAAI,CAACA,gBAAgB,AACxC,EACD,CACA,MAAME,IAAM,IAAI,CAACC,YAAY,CAACZ,UAC9B,MAAMa,QAAmC,CACxCN,QAAS,IAAI,CAACA,OAAO,CACrBC,IAAK,IAAI,CAACA,GAAG,CACbC,iBAAkB,IAAI,CAACA,gBAAgB,AACxC,EACA,OAAOvB,iBAAiB4B,YAAY,CAACH,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A,IAAI,CAACzC,gBAAgB,CAAG,IAAIZ,SAASgB,QAAQsC,oBAAoB,EAAI,KAGrE,IAAI7D,cAAc8D,QAAQ,CAAC,IAAI,EAC/B,IAAI/D,iBAAiB+D,QAAQ,CAAC,IAAI,EAGlC,GAAIvC,QAAQN,OAAO,CAAE,CACpB,IAAK,MAAM8C,UAAUxC,QAAQN,OAAO,CAAE,CACrC,KAAM,CAAE6B,IAAI,CAAE,GAAGC,WAAY,CAAGgB,OAChC,IAAI,CAAClB,cAAc,CAACC,KAAMC,WAC3B,CACD,CACD,CAqUD"}
@@ -0,0 +1,334 @@
1
+ import type { JSONSchema7 } from "json-schema";
2
+ import type { FromSchema, JSONSchema } from "json-schema-to-ts";
3
+ /**
4
+ * Object where each property is a `TemplateInput` (recursive).
5
+ *
6
+ * Allows passing an entire structure as a template:
7
+ * ```
8
+ * engine.analyze({
9
+ * userName: "{{name}}",
10
+ * userAge: "{{age}}",
11
+ * nested: { x: "{{foo}}" },
12
+ * }, inputSchema);
13
+ * ```
14
+ */
15
+ export interface TemplateInputObject {
16
+ [key: string]: TemplateInput;
17
+ }
18
+ /**
19
+ * Input type accepted by the template engine.
20
+ *
21
+ * - `string` → standard Handlebars template (parsed and executed)
22
+ * - `number` → numeric literal (passthrough)
23
+ * - `boolean` → boolean literal (passthrough)
24
+ * - `null` → null literal (passthrough)
25
+ * - `TemplateInputObject` → object where each property is a `TemplateInput`
26
+ */
27
+ export type TemplateInput = string | number | boolean | null | TemplateInputObject;
28
+ /**
29
+ * Checks whether a value is a non-string primitive literal (number, boolean, null).
30
+ * These values are treated as passthrough by the engine.
31
+ *
32
+ * Note: objects (`TemplateInputObject`) are NOT literals.
33
+ */
34
+ export declare function isLiteralInput(input: TemplateInput): input is number | boolean | null;
35
+ /**
36
+ * Checks whether a value is a template object (`TemplateInputObject`).
37
+ * Template objects are processed recursively by the engine:
38
+ * each property is analyzed/executed individually.
39
+ */
40
+ export declare function isObjectInput(input: TemplateInput): input is TemplateInputObject;
41
+ /**
42
+ * Infers the JSON Schema of a non-string primitive value.
43
+ *
44
+ * @param value - The primitive value (number, boolean, null)
45
+ * @returns The corresponding JSON Schema
46
+ *
47
+ * @example
48
+ * ```
49
+ * inferPrimitiveSchema(42) // → { type: "number" }
50
+ * inferPrimitiveSchema(true) // → { type: "boolean" }
51
+ * inferPrimitiveSchema(null) // → { type: "null" }
52
+ * ```
53
+ */
54
+ export declare function inferPrimitiveSchema(value: number | boolean | null): JSONSchema7;
55
+ export type DiagnosticCode =
56
+ /** The referenced property does not exist in the context schema */
57
+ "UNKNOWN_PROPERTY"
58
+ /** Type mismatch (e.g. #each on a non-array) */
59
+ | "TYPE_MISMATCH"
60
+ /** A block helper is used without a required argument */
61
+ | "MISSING_ARGUMENT"
62
+ /** Unknown block helper (neither built-in nor registered) */
63
+ | "UNKNOWN_HELPER"
64
+ /** The expression cannot be statically analyzed */
65
+ | "UNANALYZABLE"
66
+ /** The {{key:N}} syntax is used but no identifierSchemas were provided */
67
+ | "MISSING_IDENTIFIER_SCHEMAS"
68
+ /** The identifier N does not exist in the provided identifierSchemas */
69
+ | "UNKNOWN_IDENTIFIER"
70
+ /** The property does not exist in the identifier's schema */
71
+ | "IDENTIFIER_PROPERTY_NOT_FOUND"
72
+ /** Syntax error in the template */
73
+ | "PARSE_ERROR";
74
+ export interface DiagnosticDetails {
75
+ /** Path of the expression that caused the error (e.g. `"user.name.foo"`) */
76
+ path?: string;
77
+ /** Name of the helper involved (for helper-related errors) */
78
+ helperName?: string;
79
+ /** What was expected (e.g. `"array"`, `"property to exist"`) */
80
+ expected?: string;
81
+ /** What was found (e.g. `"string"`, `"undefined"`) */
82
+ actual?: string;
83
+ /** Available properties in the current schema (for suggestions) */
84
+ availableProperties?: string[];
85
+ /** Template identifier number (for `{{key:N}}` errors) */
86
+ identifier?: number;
87
+ }
88
+ /** Diagnostic produced by the static analyzer */
89
+ export interface TemplateDiagnostic {
90
+ /** "error" blocks execution, "warning" is informational */
91
+ severity: "error" | "warning";
92
+ /** Machine-readable code identifying the error type */
93
+ code: DiagnosticCode;
94
+ /** Human-readable message describing the problem */
95
+ message: string;
96
+ /** Position in the template source (if available from the AST) */
97
+ loc?: {
98
+ start: {
99
+ line: number;
100
+ column: number;
101
+ };
102
+ end: {
103
+ line: number;
104
+ column: number;
105
+ };
106
+ };
107
+ /** Fragment of the template source around the error */
108
+ source?: string;
109
+ /** Structured information for debugging and frontend display */
110
+ details?: DiagnosticDetails;
111
+ }
112
+ /** Complete result of the static analysis */
113
+ export interface AnalysisResult {
114
+ /** true if no errors (warnings are tolerated) */
115
+ valid: boolean;
116
+ /** List of diagnostics (errors + warnings) */
117
+ diagnostics: TemplateDiagnostic[];
118
+ /** JSON Schema describing the template's return type */
119
+ outputSchema: JSONSchema7;
120
+ }
121
+ /** Lightweight validation result (without output type inference) */
122
+ export interface ValidationResult {
123
+ /** true if no errors (warnings are tolerated) */
124
+ valid: boolean;
125
+ /** List of diagnostics (errors + warnings) */
126
+ diagnostics: TemplateDiagnostic[];
127
+ }
128
+ export interface TemplateEngineOptions {
129
+ /**
130
+ * Capacity of the parsed AST cache. Each parsed template is cached
131
+ * to avoid costly re-parsing on repeated calls.
132
+ * @default 256
133
+ */
134
+ astCacheSize?: number;
135
+ /**
136
+ * Capacity of the compiled Handlebars template cache.
137
+ * @default 256
138
+ */
139
+ compilationCacheSize?: number;
140
+ /**
141
+ * Custom helpers to register during engine construction.
142
+ *
143
+ * Each entry describes a helper with its name, implementation,
144
+ * expected parameters, and return type.
145
+ *
146
+ * @example
147
+ * ```
148
+ * const engine = new Typebars({
149
+ * helpers: [
150
+ * {
151
+ * name: "uppercase",
152
+ * description: "Converts a string to uppercase",
153
+ * fn: (value: string) => String(value).toUpperCase(),
154
+ * params: [
155
+ * { name: "value", type: { type: "string" }, description: "The string to convert" },
156
+ * ],
157
+ * returnType: { type: "string" },
158
+ * },
159
+ * ],
160
+ * });
161
+ * ```
162
+ */
163
+ helpers?: HelperConfig[];
164
+ }
165
+ export interface ExecuteOptions {
166
+ /** JSON Schema for pre-execution static validation */
167
+ schema?: JSONSchema7;
168
+ /** Data by identifier `{ [id]: { key: value } }` */
169
+ identifierData?: Record<number, Record<string, unknown>>;
170
+ /** Schemas by identifier (for static validation with identifiers) */
171
+ identifierSchemas?: Record<number, JSONSchema7>;
172
+ }
173
+ export interface AnalyzeAndExecuteOptions {
174
+ /** Schemas by identifier `{ [id]: JSONSchema7 }` for static analysis */
175
+ identifierSchemas?: Record<number, JSONSchema7>;
176
+ /** Data by identifier `{ [id]: { key: value } }` for execution */
177
+ identifierData?: Record<number, Record<string, unknown>>;
178
+ }
179
+ /** Describes a parameter expected by a helper */
180
+ export interface HelperParam {
181
+ /** Parameter name (for documentation / introspection) */
182
+ name: string;
183
+ /**
184
+ * JSON Schema describing the expected type for this parameter.
185
+ * Used for documentation and static validation.
186
+ */
187
+ type?: JSONSchema7;
188
+ /** Human-readable description of the parameter */
189
+ description?: string;
190
+ /**
191
+ * Whether the parameter is optional.
192
+ * @default false
193
+ */
194
+ optional?: boolean;
195
+ }
196
+ /**
197
+ * Definition of a helper registerable via `registerHelper()`.
198
+ *
199
+ * Contains the runtime implementation and typing metadata
200
+ * for static analysis.
201
+ */
202
+ export interface HelperDefinition {
203
+ /**
204
+ * Runtime implementation of the helper — will be registered with Handlebars.
205
+ *
206
+ * For an inline helper `{{uppercase name}}`:
207
+ * `(value: string) => string`
208
+ *
209
+ * For a block helper `{{#repeat count}}...{{/repeat}}`:
210
+ * `function(this: any, count: number, options: Handlebars.HelperOptions) { ... }`
211
+ */
212
+ fn: (...args: any[]) => unknown;
213
+ /**
214
+ * Parameters expected by the helper (for documentation and analysis).
215
+ *
216
+ * @example
217
+ * ```
218
+ * params: [
219
+ * { name: "value", type: { type: "number" }, description: "The value to round" },
220
+ * { name: "precision", type: { type: "number" }, description: "Decimal places", optional: true },
221
+ * ]
222
+ * ```
223
+ */
224
+ params?: HelperParam[];
225
+ /**
226
+ * JSON Schema describing the helper's return type for static analysis.
227
+ * @default { type: "string" }
228
+ */
229
+ returnType?: JSONSchema7;
230
+ /** Human-readable description of the helper */
231
+ description?: string;
232
+ }
233
+ /**
234
+ * Full helper configuration for registration via the `Typebars({ helpers: [...] })`
235
+ * constructor options.
236
+ *
237
+ * Extends `HelperDefinition` with a required `name`.
238
+ *
239
+ * @example
240
+ * ```
241
+ * const config: HelperConfig = {
242
+ * name: "round",
243
+ * description: "Rounds a number to a given precision",
244
+ * fn: (value: number, precision?: number) => { ... },
245
+ * params: [
246
+ * { name: "value", type: { type: "number" } },
247
+ * { name: "precision", type: { type: "number" }, optional: true },
248
+ * ],
249
+ * returnType: { type: "number" },
250
+ * };
251
+ * ```
252
+ */
253
+ export interface HelperConfig extends HelperDefinition {
254
+ /** Name of the helper as used in templates (e.g. `"uppercase"`) */
255
+ name: string;
256
+ }
257
+ /**
258
+ * Param definition used for type inference.
259
+ * Accepts `JSONSchema` from `json-schema-to-ts` to allow `FromSchema`
260
+ * to resolve literal types.
261
+ */
262
+ type TypedHelperParam = {
263
+ readonly name: string;
264
+ readonly type?: JSONSchema;
265
+ readonly description?: string;
266
+ readonly optional?: boolean;
267
+ };
268
+ /**
269
+ * Infers the TypeScript type of a single parameter from its JSON Schema.
270
+ * - If `optional: true`, the resolved type is unioned with `undefined`.
271
+ * - If `type` is not provided, the type is `unknown`.
272
+ */
273
+ type InferParamType<P> = P extends {
274
+ readonly type: infer S extends JSONSchema;
275
+ readonly optional: true;
276
+ } ? FromSchema<S> | undefined : P extends {
277
+ readonly type: infer S extends JSONSchema;
278
+ } ? FromSchema<S> : unknown;
279
+ /**
280
+ * Maps a tuple of `TypedHelperParam` to a tuple of inferred TypeScript types,
281
+ * usable as the `fn` signature.
282
+ *
283
+ * @example
284
+ * ```
285
+ * type Args = InferArgs<readonly [
286
+ * { name: "a"; type: { type: "string" } },
287
+ * { name: "b"; type: { type: "number" }; optional: true },
288
+ * ]>;
289
+ * // => [string, number | undefined]
290
+ * ```
291
+ */
292
+ type InferArgs<P extends readonly TypedHelperParam[]> = {
293
+ [K in keyof P]: InferParamType<P[K]>;
294
+ };
295
+ /**
296
+ * Helper configuration with generic parameter inference.
297
+ * Used exclusively by `defineHelper()`.
298
+ */
299
+ interface TypedHelperConfig<P extends readonly TypedHelperParam[]> {
300
+ name: string;
301
+ description?: string;
302
+ params: P;
303
+ fn: (...args: InferArgs<P>) => unknown;
304
+ returnType?: JSONSchema;
305
+ }
306
+ /**
307
+ * Creates a `HelperConfig` with automatic type inference for `fn` arguments
308
+ * based on the JSON Schemas declared in `params`.
309
+ *
310
+ * The generic parameter `const P` preserves schema literal types
311
+ * (equivalent of `as const`), enabling `FromSchema` to resolve the
312
+ * corresponding TypeScript types.
313
+ *
314
+ * @example
315
+ * ```
316
+ * const helper = defineHelper({
317
+ * name: "concat",
318
+ * description: "Concatenates two strings",
319
+ * params: [
320
+ * { name: "a", type: { type: "string" }, description: "First string" },
321
+ * { name: "b", type: { type: "string" }, description: "Second string" },
322
+ * { name: "sep", type: { type: "string" }, description: "Separator", optional: true },
323
+ * ],
324
+ * fn: (a, b, sep) => {
325
+ * // a: string, b: string, sep: string | undefined
326
+ * const separator = sep ?? "";
327
+ * return `${a}${separator}${b}`;
328
+ * },
329
+ * returnType: { type: "string" },
330
+ * });
331
+ * ```
332
+ */
333
+ export declare function defineHelper<const P extends readonly TypedHelperParam[]>(config: TypedHelperConfig<P>): HelperConfig;
334
+ export {};
@@ -0,0 +1,2 @@
1
+ export function isLiteralInput(input){return input===null||typeof input!=="string"&&typeof input!=="object"}export function isObjectInput(input){return input!==null&&typeof input==="object"}export function inferPrimitiveSchema(value){if(value===null)return{type:"null"};if(typeof value==="boolean")return{type:"boolean"};if(typeof value==="number"){return Number.isInteger(value)?{type:"integer"}:{type:"number"}}value;return{type:"null"}}export function defineHelper(config){return config}
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/types.ts"],"sourcesContent":["import type { JSONSchema7 } from \"json-schema\";\nimport type { FromSchema, JSONSchema } from \"json-schema-to-ts\";\n\n// ─── Template Input ──────────────────────────────────────────────────────────\n// The engine accepts primitive values in addition to template strings.\n// When a non-string value is passed, it is treated as a literal passthrough:\n// analysis returns the inferred type, and execution returns the value as-is.\n\n/**\n * Object where each property is a `TemplateInput` (recursive).\n *\n * Allows passing an entire structure as a template:\n * ```\n * engine.analyze({\n * userName: \"{{name}}\",\n * userAge: \"{{age}}\",\n * nested: { x: \"{{foo}}\" },\n * }, inputSchema);\n * ```\n */\nexport interface TemplateInputObject {\n\t[key: string]: TemplateInput;\n}\n\n/**\n * Input type accepted by the template engine.\n *\n * - `string` → standard Handlebars template (parsed and executed)\n * - `number` → numeric literal (passthrough)\n * - `boolean` → boolean literal (passthrough)\n * - `null` → null literal (passthrough)\n * - `TemplateInputObject` → object where each property is a `TemplateInput`\n */\nexport type TemplateInput =\n\t| string\n\t| number\n\t| boolean\n\t| null\n\t| TemplateInputObject;\n\n/**\n * Checks whether a value is a non-string primitive literal (number, boolean, null).\n * These values are treated as passthrough by the engine.\n *\n * Note: objects (`TemplateInputObject`) are NOT literals.\n */\nexport function isLiteralInput(\n\tinput: TemplateInput,\n): input is number | boolean | null {\n\treturn (\n\t\tinput === null || (typeof input !== \"string\" && typeof input !== \"object\")\n\t);\n}\n\n/**\n * Checks whether a value is a template object (`TemplateInputObject`).\n * Template objects are processed recursively by the engine:\n * each property is analyzed/executed individually.\n */\nexport function isObjectInput(\n\tinput: TemplateInput,\n): input is TemplateInputObject {\n\treturn input !== null && typeof input === \"object\";\n}\n\n/**\n * Infers the JSON Schema of a non-string primitive value.\n *\n * @param value - The primitive value (number, boolean, null)\n * @returns The corresponding JSON Schema\n *\n * @example\n * ```\n * inferPrimitiveSchema(42) // → { type: \"number\" }\n * inferPrimitiveSchema(true) // → { type: \"boolean\" }\n * inferPrimitiveSchema(null) // → { type: \"null\" }\n * ```\n */\nexport function inferPrimitiveSchema(\n\tvalue: number | boolean | null,\n): JSONSchema7 {\n\tif (value === null) return { type: \"null\" };\n\tif (typeof value === \"boolean\") return { type: \"boolean\" };\n\tif (typeof value === \"number\") {\n\t\treturn Number.isInteger(value) ? { type: \"integer\" } : { type: \"number\" };\n\t}\n\t// Exhaustiveness check — all branches are covered above.\n\t// If the type of `value` changes, TypeScript will raise an error here.\n\tvalue satisfies never;\n\treturn { type: \"null\" };\n}\n\n// ─── Diagnostic Codes ────────────────────────────────────────────────────────\n// Machine-readable codes for each error/warning type, enabling the frontend\n// to react programmatically without parsing the human-readable message.\n\nexport type DiagnosticCode =\n\t/** The referenced property does not exist in the context schema */\n\t| \"UNKNOWN_PROPERTY\"\n\t/** Type mismatch (e.g. #each on a non-array) */\n\t| \"TYPE_MISMATCH\"\n\t/** A block helper is used without a required argument */\n\t| \"MISSING_ARGUMENT\"\n\t/** Unknown block helper (neither built-in nor registered) */\n\t| \"UNKNOWN_HELPER\"\n\t/** The expression cannot be statically analyzed */\n\t| \"UNANALYZABLE\"\n\t/** The {{key:N}} syntax is used but no identifierSchemas were provided */\n\t| \"MISSING_IDENTIFIER_SCHEMAS\"\n\t/** The identifier N does not exist in the provided identifierSchemas */\n\t| \"UNKNOWN_IDENTIFIER\"\n\t/** The property does not exist in the identifier's schema */\n\t| \"IDENTIFIER_PROPERTY_NOT_FOUND\"\n\t/** Syntax error in the template */\n\t| \"PARSE_ERROR\";\n\n// ─── Diagnostic Details ──────────────────────────────────────────────────────\n// Supplementary information to understand the exact cause of the error.\n// Designed to be easily JSON-serializable and consumable by a frontend.\n\nexport interface DiagnosticDetails {\n\t/** Path of the expression that caused the error (e.g. `\"user.name.foo\"`) */\n\tpath?: string;\n\t/** Name of the helper involved (for helper-related errors) */\n\thelperName?: string;\n\t/** What was expected (e.g. `\"array\"`, `\"property to exist\"`) */\n\texpected?: string;\n\t/** What was found (e.g. `\"string\"`, `\"undefined\"`) */\n\tactual?: string;\n\t/** Available properties in the current schema (for suggestions) */\n\tavailableProperties?: string[];\n\t/** Template identifier number (for `{{key:N}}` errors) */\n\tidentifier?: number;\n}\n\n// ─── Static Analysis Result ──────────────────────────────────────────────────\n\n/** Diagnostic produced by the static analyzer */\nexport interface TemplateDiagnostic {\n\t/** \"error\" blocks execution, \"warning\" is informational */\n\tseverity: \"error\" | \"warning\";\n\n\t/** Machine-readable code identifying the error type */\n\tcode: DiagnosticCode;\n\n\t/** Human-readable message describing the problem */\n\tmessage: string;\n\n\t/** Position in the template source (if available from the AST) */\n\tloc?: {\n\t\tstart: { line: number; column: number };\n\t\tend: { line: number; column: number };\n\t};\n\n\t/** Fragment of the template source around the error */\n\tsource?: string;\n\n\t/** Structured information for debugging and frontend display */\n\tdetails?: DiagnosticDetails;\n}\n\n/** Complete result of the static analysis */\nexport interface AnalysisResult {\n\t/** true if no errors (warnings are tolerated) */\n\tvalid: boolean;\n\t/** List of diagnostics (errors + warnings) */\n\tdiagnostics: TemplateDiagnostic[];\n\t/** JSON Schema describing the template's return type */\n\toutputSchema: JSONSchema7;\n}\n\n/** Lightweight validation result (without output type inference) */\nexport interface ValidationResult {\n\t/** true if no errors (warnings are tolerated) */\n\tvalid: boolean;\n\t/** List of diagnostics (errors + warnings) */\n\tdiagnostics: TemplateDiagnostic[];\n}\n\n// ─── Public Engine Options ───────────────────────────────────────────────────\n\nexport interface TemplateEngineOptions {\n\t/**\n\t * Capacity of the parsed AST cache. Each parsed template is cached\n\t * to avoid costly re-parsing on repeated calls.\n\t * @default 256\n\t */\n\tastCacheSize?: number;\n\n\t/**\n\t * Capacity of the compiled Handlebars template cache.\n\t * @default 256\n\t */\n\tcompilationCacheSize?: number;\n\n\t/**\n\t * Custom helpers to register during engine construction.\n\t *\n\t * Each entry describes a helper with its name, implementation,\n\t * expected parameters, and return type.\n\t *\n\t * @example\n\t * ```\n\t * const engine = new Typebars({\n\t * helpers: [\n\t * {\n\t * name: \"uppercase\",\n\t * description: \"Converts a string to uppercase\",\n\t * fn: (value: string) => String(value).toUpperCase(),\n\t * params: [\n\t * { name: \"value\", type: { type: \"string\" }, description: \"The string to convert\" },\n\t * ],\n\t * returnType: { type: \"string\" },\n\t * },\n\t * ],\n\t * });\n\t * ```\n\t */\n\thelpers?: HelperConfig[];\n}\n\n// ─── Execution Options ───────────────────────────────────────────────────────\n// Optional options object for `execute()`, replacing multiple positional\n// parameters for better ergonomics.\n\nexport interface ExecuteOptions {\n\t/** JSON Schema for pre-execution static validation */\n\tschema?: JSONSchema7;\n\t/** Data by identifier `{ [id]: { key: value } }` */\n\tidentifierData?: Record<number, Record<string, unknown>>;\n\t/** Schemas by identifier (for static validation with identifiers) */\n\tidentifierSchemas?: Record<number, JSONSchema7>;\n}\n\n// ─── Combined Analyze-and-Execute Options ────────────────────────────────────\n// Optional options object for `analyzeAndExecute()`, grouping parameters\n// related to template identifiers.\n\nexport interface AnalyzeAndExecuteOptions {\n\t/** Schemas by identifier `{ [id]: JSONSchema7 }` for static analysis */\n\tidentifierSchemas?: Record<number, JSONSchema7>;\n\t/** Data by identifier `{ [id]: { key: value } }` for execution */\n\tidentifierData?: Record<number, Record<string, unknown>>;\n}\n\n// ─── Custom Helpers ──────────────────────────────────────────────────────────\n// Allows registering custom helpers with their type signature for static\n// analysis support.\n\n/** Describes a parameter expected by a helper */\nexport interface HelperParam {\n\t/** Parameter name (for documentation / introspection) */\n\tname: string;\n\n\t/**\n\t * JSON Schema describing the expected type for this parameter.\n\t * Used for documentation and static validation.\n\t */\n\ttype?: JSONSchema7;\n\n\t/** Human-readable description of the parameter */\n\tdescription?: string;\n\n\t/**\n\t * Whether the parameter is optional.\n\t * @default false\n\t */\n\toptional?: boolean;\n}\n\n/**\n * Definition of a helper registerable via `registerHelper()`.\n *\n * Contains the runtime implementation and typing metadata\n * for static analysis.\n */\nexport interface HelperDefinition {\n\t/**\n\t * Runtime implementation of the helper — will be registered with Handlebars.\n\t *\n\t * For an inline helper `{{uppercase name}}`:\n\t * `(value: string) => string`\n\t *\n\t * For a block helper `{{#repeat count}}...{{/repeat}}`:\n\t * `function(this: any, count: number, options: Handlebars.HelperOptions) { ... }`\n\t */\n\t// biome-ignore lint/suspicious/noExplicitAny: Handlebars helper signatures are inherently dynamic\n\tfn: (...args: any[]) => unknown;\n\n\t/**\n\t * Parameters expected by the helper (for documentation and analysis).\n\t *\n\t * @example\n\t * ```\n\t * params: [\n\t * { name: \"value\", type: { type: \"number\" }, description: \"The value to round\" },\n\t * { name: \"precision\", type: { type: \"number\" }, description: \"Decimal places\", optional: true },\n\t * ]\n\t * ```\n\t */\n\tparams?: HelperParam[];\n\n\t/**\n\t * JSON Schema describing the helper's return type for static analysis.\n\t * @default { type: \"string\" }\n\t */\n\treturnType?: JSONSchema7;\n\n\t/** Human-readable description of the helper */\n\tdescription?: string;\n}\n\n/**\n * Full helper configuration for registration via the `Typebars({ helpers: [...] })`\n * constructor options.\n *\n * Extends `HelperDefinition` with a required `name`.\n *\n * @example\n * ```\n * const config: HelperConfig = {\n * name: \"round\",\n * description: \"Rounds a number to a given precision\",\n * fn: (value: number, precision?: number) => { ... },\n * params: [\n * { name: \"value\", type: { type: \"number\" } },\n * { name: \"precision\", type: { type: \"number\" }, optional: true },\n * ],\n * returnType: { type: \"number\" },\n * };\n * ```\n */\nexport interface HelperConfig extends HelperDefinition {\n\t/** Name of the helper as used in templates (e.g. `\"uppercase\"`) */\n\tname: string;\n}\n\n// ─── Automatic Type Inference via json-schema-to-ts ──────────────────────────\n// Allows `defineHelper()` to infer TypeScript types for `fn` arguments\n// from the JSON Schemas declared in `params`.\n\n/**\n * Param definition used for type inference.\n * Accepts `JSONSchema` from `json-schema-to-ts` to allow `FromSchema`\n * to resolve literal types.\n */\ntype TypedHelperParam = {\n\treadonly name: string;\n\treadonly type?: JSONSchema;\n\treadonly description?: string;\n\treadonly optional?: boolean;\n};\n\n/**\n * Infers the TypeScript type of a single parameter from its JSON Schema.\n * - If `optional: true`, the resolved type is unioned with `undefined`.\n * - If `type` is not provided, the type is `unknown`.\n */\ntype InferParamType<P> = P extends {\n\treadonly type: infer S extends JSONSchema;\n\treadonly optional: true;\n}\n\t? FromSchema<S> | undefined\n\t: P extends { readonly type: infer S extends JSONSchema }\n\t\t? FromSchema<S>\n\t\t: unknown;\n\n/**\n * Maps a tuple of `TypedHelperParam` to a tuple of inferred TypeScript types,\n * usable as the `fn` signature.\n *\n * @example\n * ```\n * type Args = InferArgs<readonly [\n * { name: \"a\"; type: { type: \"string\" } },\n * { name: \"b\"; type: { type: \"number\" }; optional: true },\n * ]>;\n * // => [string, number | undefined]\n * ```\n */\ntype InferArgs<P extends readonly TypedHelperParam[]> = {\n\t[K in keyof P]: InferParamType<P[K]>;\n};\n\n/**\n * Helper configuration with generic parameter inference.\n * Used exclusively by `defineHelper()`.\n */\ninterface TypedHelperConfig<P extends readonly TypedHelperParam[]> {\n\tname: string;\n\tdescription?: string;\n\tparams: P;\n\tfn: (...args: InferArgs<P>) => unknown;\n\treturnType?: JSONSchema;\n}\n\n/**\n * Creates a `HelperConfig` with automatic type inference for `fn` arguments\n * based on the JSON Schemas declared in `params`.\n *\n * The generic parameter `const P` preserves schema literal types\n * (equivalent of `as const`), enabling `FromSchema` to resolve the\n * corresponding TypeScript types.\n *\n * @example\n * ```\n * const helper = defineHelper({\n * name: \"concat\",\n * description: \"Concatenates two strings\",\n * params: [\n * { name: \"a\", type: { type: \"string\" }, description: \"First string\" },\n * { name: \"b\", type: { type: \"string\" }, description: \"Second string\" },\n * { name: \"sep\", type: { type: \"string\" }, description: \"Separator\", optional: true },\n * ],\n * fn: (a, b, sep) => {\n * // a: string, b: string, sep: string | undefined\n * const separator = sep ?? \"\";\n * return `${a}${separator}${b}`;\n * },\n * returnType: { type: \"string\" },\n * });\n * ```\n */\nexport function defineHelper<const P extends readonly TypedHelperParam[]>(\n\tconfig: TypedHelperConfig<P>,\n): HelperConfig {\n\treturn config as unknown as HelperConfig;\n}\n"],"names":["isLiteralInput","input","isObjectInput","inferPrimitiveSchema","value","type","Number","isInteger","defineHelper","config"],"mappings":"AA8CA,OAAO,SAASA,eACfC,KAAoB,EAEpB,OACCA,QAAU,MAAS,OAAOA,QAAU,UAAY,OAAOA,QAAU,QAEnE,CAOA,OAAO,SAASC,cACfD,KAAoB,EAEpB,OAAOA,QAAU,MAAQ,OAAOA,QAAU,QAC3C,CAeA,OAAO,SAASE,qBACfC,KAA8B,EAE9B,GAAIA,QAAU,KAAM,MAAO,CAAEC,KAAM,MAAO,EAC1C,GAAI,OAAOD,QAAU,UAAW,MAAO,CAAEC,KAAM,SAAU,EACzD,GAAI,OAAOD,QAAU,SAAU,CAC9B,OAAOE,OAAOC,SAAS,CAACH,OAAS,CAAEC,KAAM,SAAU,EAAI,CAAEA,KAAM,QAAS,CACzE,CAGAD,MACA,MAAO,CAAEC,KAAM,MAAO,CACvB,CA6UA,OAAO,SAASG,aACfC,MAA4B,EAE5B,OAAOA,MACR"}