page.tsx 4.2 KB

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