Templates
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.
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
| Field | Type | Does |
|---|---|---|
kind | 'Schema' | 'Operation' | 'Operations' | Required. Picks the nodes render gets. |
render | (node, ctx) => Output | Required. Returns the files for one node, or for every operation with 'Operations'. |
kind | render gets | Runs | Use it for |
|---|---|---|---|
'Schema' | A SchemaNode | Once per schema the plugin keeps | Types, validators, factories |
'Operation' | An OperationNode | Once per operation the plugin keeps | Clients, hooks, handlers |
'Operations' | Array<OperationNode> | Once, after every operation | Index 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 withaddTemplate(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 itsrenderfrom your own template.
Skipping nodes
Return null from render to write nothing for a node. Check the node first:
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:
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.
| Field | Type | Holds |
|---|---|---|
ctx.config | Config | The resolved Kubb config. |
ctx.root | string | Absolute path of the root output folder, the same for every plugin. |
ctx.options | TResolvedOptions | The node's options after exclude, include, and override. In operations(), the plugin's options. |
ctx.plugin | Plugin | The current plugin. |
ctx.resolver | Resolver | The current plugin's resolver. |
ctx.printer | Printer | undefined | The printer from the plugin's setPrinter factory, or undefined without one. |
ctx.adapter | Adapter | The adapter that parsed the input. |
ctx.meta | InputMeta | Document metadata: title, version, baseURL, servers, and the auth schemes. |
ctx.schemas | ReadonlyArray<SchemaNode> | Every schema in the spec, in adapter order. The OpenAPI adapter puts a referenced schema first, except in reference cycles. |
ctx.operations | ReadonlyArray<OperationNode> | Every operation in the spec, in adapter order. |
ctx.hooks | Hookable<KubbHooks> | The build's hook emitter. |
ctx.signal | AbortSignal | undefined | Stops generation at the next safe checkpoint. |
ctx also has these helpers:
| Helper | Signature | Does |
|---|---|---|
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 | undefined | Returns 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 | undefined | Returns a plugin by name. |
ctx.requirePlugin() | (name: string) => Plugin | Returns a plugin by name, or throws. |
ctx.getResolver() | (name: string) => Resolver | Returns another plugin's resolver. |
ctx.file() | (node, options?) => FileTarget | Returns the <File> props for a node's file. See file. |
ctx.from() | (source?) => From | Returns a plugin's resolver whose names carry their imports. See from. |
ctx.memo() | (key: string, compute: () => T) => T | Caches a value for the whole build. See memo. |
ctx.info() | (message: string, options?: { code?, help?, location? }) => void | Reports an info diagnostic for this plugin. |
ctx.warn() | (message: string, options?: { code?, help?, location? }) => void | Reports a warning, which does not fail the build. |
ctx.error() | (error: string | Error, options?: { code?, help?, location? }) => void | Reports 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 afternode.operationId. Passnameto choose another name. - An operation file that would land on a component schema's file, ignoring case, gets an
Operationsuffix: operationpointgoes topointOperation.ts, next to schemapoint.ts. The declarations keep their names. Kit adds it only when the plugin has aSchematemplate, so a plugin that writes only operation files keeps the plain name. - Pass
pluginto get another plugin's file, such as the types to import.filethen uses that plugin's resolver,outputandgroup, after itsinclude,excludeandoverriderules. A missing plugin reportsKUBB_PLUGIN_NOT_FOUND. - Outside a template or a printer factory,
file()reportsKUBB_NO_CONTEXT.
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.
| Member | Returns |
|---|---|
from(source).resolver | The 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
Operationsuffix asfile. - 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 fromfrom, othercoderesults, strings and numbers. null,undefinedandfalseprint nothing, so${cond && name}works whencondis 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:
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.operationIdornode.name. - Use
ctx.resolver.memo(key, compute)for a value only your plugin reads.
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
},
})