| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- import { pageMetadata } from "@/lib/page-metadata"
- export const metadata = pageMetadata("docs/api/codegen")
- # @json-render/codegen
- Utilities for generating code from UI trees.
- ## Tree Traversal
- ### traverseSpec
- Walk the UI spec depth-first.
- ```typescript
- function traverseSpec(
- spec: Spec,
- visitor: TreeVisitor,
- startKey?: string
- ): void
- interface TreeVisitor {
- (element: UIElement, key: string, depth: number, parent: UIElement | null): void;
- }
- ```
- ### collectUsedComponents
- Get all unique component types used in a spec.
- ```typescript
- function collectUsedComponents(spec: Spec): Set<string>
- // Example
- const components = collectUsedComponents(spec);
- // Set { 'Card', 'Metric', 'Chart' }
- ```
- ### collectStatePaths
- Get all state paths referenced in props (statePath, bindPath, etc.).
- ```typescript
- function collectStatePaths(spec: Spec): Set<string>
- // Example
- const paths = collectStatePaths(spec);
- // Set { 'analytics/revenue', 'analytics/customers' }
- ```
- ### collectActions
- Get all action names used in the spec.
- ```typescript
- function collectActions(spec: Spec): Set<string>
- // Example
- const actions = collectActions(spec);
- // Set { 'submit_form', 'refresh_data' }
- ```
- ## Serialization
- ### serializePropValue
- Serialize a single value to a code string.
- ```typescript
- function serializePropValue(
- value: unknown,
- options?: SerializeOptions
- ): { value: string; needsBraces: boolean }
- // Examples
- serializePropValue("hello")
- // { value: '"hello"', needsBraces: false }
- serializePropValue(42)
- // { value: '42', needsBraces: true }
- serializePropValue({ $state: '/user/name' })
- // { value: '{ $state: "/user/name" }', needsBraces: true }
- ```
- ### serializeProps
- Serialize a props object to a JSX attributes string.
- ```typescript
- function serializeProps(
- props: Record<string, unknown>,
- options?: SerializeOptions
- ): string
- // Example
- serializeProps({ title: 'Dashboard', columns: 3, disabled: true })
- // 'title="Dashboard" columns={3} disabled'
- ```
- ### escapeString
- Escape a string for use in code.
- ```typescript
- function escapeString(
- str: string,
- quotes?: 'single' | 'double'
- ): string
- ```
- ## Types
- ### GeneratedFile
- ```typescript
- interface GeneratedFile {
- /** File path relative to project root */
- path: string;
- /** File contents */
- content: string;
- }
- ```
- ### CodeGenerator
- ```typescript
- interface CodeGenerator {
- /** Generate files from a UI spec */
- generate(spec: Spec): GeneratedFile[];
- }
- ```
- ### SerializeOptions
- ```typescript
- interface SerializeOptions {
- /** Quote style for strings */
- quotes?: 'single' | 'double';
- /** Indent for objects/arrays */
- indent?: number;
- }
- ```
|