page.mdx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. export const metadata = { title: "@json-render/codegen API" }
  2. # @json-render/codegen
  3. Utilities for generating code from UI trees.
  4. ## Tree Traversal
  5. ### traverseSpec
  6. Walk the UI spec depth-first.
  7. ```typescript
  8. function traverseSpec(
  9. spec: Spec,
  10. visitor: SpecVisitor,
  11. startKey?: string
  12. ): void
  13. interface SpecVisitor {
  14. (element: UIElement, depth: number, parent: UIElement | null): void;
  15. }
  16. ```
  17. ### collectUsedComponents
  18. Get all unique component types used in a spec.
  19. ```typescript
  20. function collectUsedComponents(spec: Spec): Set<string>
  21. // Example
  22. const components = collectUsedComponents(spec);
  23. // Set { 'Card', 'Metric', 'Chart' }
  24. ```
  25. ### collectStatePaths
  26. Get all state paths referenced in props (statePath, bindPath, valuePath, etc.).
  27. ```typescript
  28. function collectStatePaths(spec: Spec): Set<string>
  29. // Example
  30. const paths = collectStatePaths(spec);
  31. // Set { 'analytics/revenue', 'analytics/customers' }
  32. ```
  33. ### collectActions
  34. Get all action names used in the spec.
  35. ```typescript
  36. function collectActions(spec: Spec): Set<string>
  37. // Example
  38. const actions = collectActions(spec);
  39. // Set { 'submit_form', 'refresh_data' }
  40. ```
  41. ## Serialization
  42. ### serializePropValue
  43. Serialize a single value to a code string.
  44. ```typescript
  45. function serializePropValue(
  46. value: unknown,
  47. options?: SerializeOptions
  48. ): { value: string; needsBraces: boolean }
  49. // Examples
  50. serializePropValue("hello")
  51. // { value: '"hello"', needsBraces: false }
  52. serializePropValue(42)
  53. // { value: '42', needsBraces: true }
  54. serializePropValue({ path: 'user/name' })
  55. // { value: '{ path: "user/name" }', needsBraces: true }
  56. ```
  57. ### serializeProps
  58. Serialize a props object to a JSX attributes string.
  59. ```typescript
  60. function serializeProps(
  61. props: Record<string, unknown>,
  62. options?: SerializeOptions
  63. ): string
  64. // Example
  65. serializeProps({ title: 'Dashboard', columns: 3, disabled: true })
  66. // 'title="Dashboard" columns={3} disabled'
  67. ```
  68. ### escapeString
  69. Escape a string for use in code.
  70. ```typescript
  71. function escapeString(
  72. str: string,
  73. quotes?: 'single' | 'double'
  74. ): string
  75. ```
  76. ## Types
  77. ### GeneratedFile
  78. ```typescript
  79. interface GeneratedFile {
  80. /** File path relative to project root */
  81. path: string;
  82. /** File contents */
  83. content: string;
  84. }
  85. ```
  86. ### CodeGenerator
  87. ```typescript
  88. interface CodeGenerator {
  89. /** Generate files from a UI spec */
  90. generate(spec: Spec): GeneratedFile[];
  91. }
  92. ```
  93. ### SerializeOptions
  94. ```typescript
  95. interface SerializeOptions {
  96. /** Quote style for strings */
  97. quotes?: 'single' | 'double';
  98. /** Indent for objects/arrays */
  99. indent?: number;
  100. }
  101. ```