Beta You're reading the docs for Kubb v5, which is currently in beta. View the stable v4 docs
Skip to content

AST

The @kubb/ast package defines Kubb's universal Abstract Syntax Tree, the contract between the two halves of the pipeline. Adapters produce the AST from a specification (OpenAPI, AsyncAPI, JSON Schema, and so on), and plugins consume it to emit files. Because every plugin reads the same tree, one plugin works against any spec a custom adapter supplies.

The tree shape

A single InputNode sits at the top, holding reusable schemas and operations. Operations point at parameters, an optional request body, and responses. Each of those connects back to schemas.

Resulting tree
text
InputNode
├── schemas: SchemaNode[]            (named, reusable schemas)
└── operations: OperationNode[]
    ├── parameters: ParameterNode[]  → SchemaNode
    ├── requestBody?: RequestBodyNode  → content: ContentNode[] → SchemaNode
    └── responses: ResponseNode[]      → content: ContentNode[] → SchemaNode

SchemaNode (discriminated by `type`)
  Structural:  object | array | tuple | union | intersection | enum
  Scalar:      string | number | integer | bigint | boolean
                null | any | unknown | void | never
  Special:     ref | date | datetime | time | uuid | email | url | blob

Request bodies and responses hold one ContentNode per content type (for example application/json), and each content node carries its own body schema. Every child slot is a node, so a single traversal drives walk, transform, and collect across the whole tree. Every node also carries a kind field as the discriminant, so switch (node.kind) narrows the type for you.

Spec-agnostic by design

The AST is the reason plugins stay simple. A plugin never looks at OpenAPI directly. It reads the tree the adapter produces, which is why one plugin works for OpenAPI 2.0, 3.0, 3.1, and any custom adapter.

Operations keep that neutrality even for transport details. An OperationNode is a discriminated union keyed on protocol, so the model stays spec-neutral while HTTP specifics stay typed. An HttpOperationNode (protocol: 'http') guarantees a non-nullable method and path. A GenericOperationNode describes a non-HTTP transport and omits both. @kubb/adapter-oas produces HttpOperationNodes, so OpenAPI output is unchanged. Narrow with isHttpOperationNode(node) before reading method or path.

How the AST connects the pipeline

The same tree flows through four stages, each documented on its own page:

  • Adapters build the AST from a spec.
  • Plugins read it and emit files.
  • Macros rewrite nodes before printing.
  • Parsers turn the emitted nodes into source code.

Reference

The callable surface lives in the AST API reference: the node factories, the walk, transform, and collect visitors, the type guards, and the naming helpers.