Skip to content
Kit

Templates

defineTemplate declares a template that writes a plugin's files for each schema or operation. Covers its kinds, the TemplateContext, file, from, code, and the memo cache.

A template writes your plugin's files for each node of one kind. Define it with defineTemplate, then register it in your plugin's setup with addTemplate.

zodSchema.tsx
import { addTemplate, defineExtension, defineTemplate } from 'kubb/kit'
import { File } from '@kubb/renderer-jsx'

export const zodSchema = defineTemplate({
  kind: 'Schema',
  render(node, ctx) {
    if (!node.name) return null
    return <File {...ctx.file(node)}>…</File>
  },
})

export const pluginZod = defineExtension({
  kind: 'plugin',
  meta: { name: 'plugin-zod' },
  setup(options) {
    addTemplate(zodSchema)
    return options
  },
})

defineTemplate

FieldTypeDoes
kind'Schema' | 'Operation' | 'Operations'Required. Picks the nodes render gets.
render(node, ctx) => OutputRequired. Returns the files for one node, or for every operation with 'Operations'.
kindrender getsRunsUse it for
'Schema'A SchemaNodeOnce per schema the plugin keepsTypes, validators, factories
'Operation'An OperationNodeOnce per operation the plugin keepsClients, hooks, handlers
'Operations'Array<OperationNode>Once, after every operationIndex or aggregate files

kind types the node, so render needs no annotation. Type the context with the plugin: defineTemplate<PluginZod>({ … }).

Output is JSX, a FileNode, Array<FileNode>, null, or undefined, or a promise of one of them. A FileNode is stored as it is. To write files yourself, call ctx.upsertFile() and return nothing.

  • Kit renders JSX with jsxRenderer. Pass another renderer with addTemplate(template, { renderer }).
  • Templates of one kind run in the order you add them.
  • A template is a plain value. Import another plugin's template and pass it to addTemplate, or call its render from your own template.

Skipping nodes

Return null from render to write nothing for a node. Check the node first:

getOperation.ts
import { ast, defineTemplate } from 'kubb/kit'

export const getOperation = defineTemplate({
  kind: 'Operation',
  render(node, ctx) {
    if (!ast.http.isOperation(node) || node.method !== 'GET') return null
    // node is a GET operation here
    return null
  },
})

Writing FileNodes

A template can return FileNodes from ast.factory instead of JSX:

operationFile.ts
import { ast, defineTemplate } from 'kubb/kit'

export const operationFile = defineTemplate({
  kind: 'Operation',
  render(node, ctx) {
    const file = ctx.resolver.file({
      name: node.operationId,
      extname: '.ts',
      root: ctx.root,
      output: { path: 'operations', mode: 'directory' },
    })

    return [
      ast.factory.createFile({
        baseName: file.baseName,
        path: file.path,
        sources: [
          ast.factory.createSource({
            nodes: [ast.factory.createText(`export const op = '${node.operationId}'`)],
          }),
        ],
      }),
    ]
  },
})

This template writes operations/<operationId>.ts in the output folder. When your plugin has an output option, pass ctx.options.output instead of a fixed value.

TemplateContext

render gets ctx with these fields. Kit runs it through withContext, so this, ctx and useKubb() are the same template context, and composables such as useConfig() and memo() work there. Setup-only composables such as addTemplate report KUBB_NO_CONTEXT.

FieldTypeHolds
ctx.configConfigThe resolved Kubb config.
ctx.rootstringAbsolute path of the root output folder, the same for every plugin.
ctx.optionsTResolvedOptionsThe node's options after exclude, include, and override. In operations(), the plugin's options.
ctx.pluginPluginThe current plugin.
ctx.resolverResolverThe current plugin's resolver.
ctx.printerPrinter | undefinedThe printer from the plugin's setPrinter factory, or undefined without one.
ctx.adapterAdapterThe adapter that parsed the input.
ctx.metaInputMetaDocument metadata: title, version, baseURL, servers, and the auth schemes.
ctx.schemasReadonlyArray<SchemaNode>Every schema in the spec, in adapter order. The OpenAPI adapter puts a referenced schema first, except in reference cycles.
ctx.operationsReadonlyArray<OperationNode>Every operation in the spec, in adapter order.
ctx.hooksHookable<KubbHooks>The build's hook emitter.
ctx.signalAbortSignal | undefinedStops generation at the next safe checkpoint.

ctx also has these helpers:

HelperSignatureDoes
ctx.addFile()(...files: Array<FileNode>) => Promise<void>Adds files, skipping any that already exist.
ctx.upsertFile()(...files: Array<FileNode>) => Promise<void>Adds files, or merges their sources and imports into an existing file.
ctx.getFile()(path: string) => FileNode | undefinedReturns the file at path as the build holds it now, such as one an earlier plugin wrote.
ctx.dispatch()({ result, renderer?, pointer? }) => Promise<void>Adds a template result: files as they are, or elements through renderer.
ctx.getPlugin()(name: string) => Plugin | undefinedReturns a plugin by name.
ctx.requirePlugin()(name: string) => PluginReturns a plugin by name, or throws.
ctx.getResolver()(name: string) => ResolverReturns another plugin's resolver.
ctx.file()(node, options?) => FileTargetReturns the <File> props for a node's file. See file.
ctx.from()(source?) => FromReturns a plugin's resolver whose names carry their imports. See from.
ctx.memo()(key: string, compute: () => T) => TCaches a value for the whole build. See memo.
ctx.info()(message: string, options?: { code?, help?, location? }) => voidReports an info diagnostic for this plugin.
ctx.warn()(message: string, options?: { code?, help?, location? }) => voidReports a warning, which does not fail the build.
ctx.error()(error: string | Error, options?: { code?, help?, location? }) => voidReports an error, which fails the build. A Diagnostics.Error keeps its code, help, and location.

Naming files with file

file(node, options?) returns the <File> props for the file a template writes: baseName, path, meta, and the banner and footer from the plugin's output. Spread it onto <File>. It is a composable, so it reads the current template context. ctx.file(node) and this.file(node) do the same.

  • A schema's file is named after node.name, and an operation's file after node.operationId. Pass name to choose another name.
  • An operation file that would land on a component schema's file, ignoring case, gets an Operation suffix: operation point goes to pointOperation.ts, next to schema point.ts. The declarations keep their names. Kit adds it only when the plugin has a Schema template, so a plugin that writes only operation files keeps the plain name.
  • Pass plugin to get another plugin's file, such as the types to import. file then uses that plugin's resolver, output and group, after its include, exclude and override rules. A missing plugin reports KUBB_PLUGIN_NOT_FOUND.
  • Outside a template or a printer factory, file() reports KUBB_NO_CONTEXT.
zodOperation.tsx
import { defineTemplate, file } from 'kubb/kit'
import { File } from '@kubb/renderer-jsx'

export const zodOperation = defineTemplate({
  kind: 'Operation',
  render(node) {
    const types = file(node, { plugin: 'plugin-ts' })

    return (
      <File {...file(node)}>
        <File.Import names={[{ name: 'Pet' }]} path={types.path} />
      </File>
    )
  },
})

Importing names with from and code

from(source?) wraps a plugin's resolver, so each name it returns comes with its import from that plugin's file. Call it as from(source), ctx.from(source) or this.from(source).

source is one of:

  • A plugin name, such as 'plugin-ts'.
  • A name a plugin offers with provide, such as 'types'.
  • Nothing, for the current plugin. ctx.from() is then typed with that plugin's resolver.

Any other source fails with KUBB_PLUGIN_NOT_FOUND.

MemberReturns
from(source).resolverThe plugin's resolver. Each method that returns a name returns a RefName, { name, import }, which prints as the name.
from(source).ref(node, name)name with the import from the plugin's file for node. Use it for a name no resolver method returns, such as one from a shared helper.

The first argument of a resolver method picks the file to import from:

  • An operation node: the plugin's file for that operation, with the same Operation suffix as file.
  • A schema node, { name } or a schema name: the plugin's file for that schema. The schema must be in the document.
  • Anything else, such as a status code: no import.

A name also gets no import when the result is not an identifier, such as a description, or when the plugin's include or exclude rules leave the node out.

You pass only the node. The target resolver's file.name decides the file.

Imports are value imports. Set space: 'type' on the names you use only as types.

code is a template tag that returns { text, imports }:

  • Its placeholders (${…}) take names from from, other code results, strings and numbers.
  • null, undefined and false print nothing, so ${cond && name} works when cond is a boolean.
  • code.join(parts, separator) joins several parts and keeps their imports.
  • The imports come in the order of use. <File> merges the ones that share a path.

Import paths are absolute. Pass the file you write as root, so each path becomes relative to it:

zodSchema.tsx
import { ast, code, defineTemplate, file, from } from 'kubb/kit'
import { Code, File } from '@kubb/renderer-jsx'

export const zodSchema = defineTemplate({
  kind: 'Schema',
  render(node) {
    if (!node.name) return null
    const target = file(node)
    const types = from('plugin-ts').resolver
    const source = code`export const ${node.name}Schema = z.object({}) satisfies z.ZodType<${types.name(node.name)}>`

    return (
      <File {...target}>
        <File.Import names={[{ name: 'z', space: 'value' }]} path="zod" />
        {source.imports.map((imp) => (
          <File.Import
            key={imp.path}
            names={ast.file.getNames(imp).map((item) => ({ ...item, space: 'type' }))}
            path={imp.path}
            root={target.path}
          />
        ))}
        <Code name={`${node.name}Schema`} export>
          {source.text}
        </Code>
      </File>
    )
  },
})

Caching with memo

memo(key, compute) runs compute once per key and build, then returns the stored value. It is a composable, so it reads the current context. ctx.memo(key, compute) and this.memo(key, compute) do the same. Every plugin shares the store, so:

  • Prefix the key with your plugin name, such as my-plugin:, unless every plugin should share the value.
  • Put the node in the key for a value from one node: node.operationId or node.name.
  • Use ctx.resolver.memo(key, compute) for a value only your plugin reads.
memoOperation.ts
import { ast, defineTemplate, memo } from 'kubb/kit'

export const memoOperation = defineTemplate({
  kind: 'Operation',
  render(node) {
    // One entry per operation. Other templates that ask for the same key reuse it.
    const schemas = memo(`my-plugin:schemas:${node.operationId}`, () =>
      ast.node.collect<ast.SchemaNode>(node, { depth: 'shallow', schema: (schema) => schema }),
    )

    return
  },
})
The value stays in memory until the build ends. Store derived data, such as names or paths, not generated source.