Kit
Adapters
Build a Kubb adapter with defineExtension and kind adapter. It converts an input spec into the universal AST. Covers the Adapter interface.
An adapter turns your spec into the InputNode every plugin reads. Define one with defineExtension and kind: 'adapter'. setup resolves the user's options. build returns the adapter for them. meta and the factory statics work as for every extension.
For OpenAPI 2.0, 3.0, 3.1, and 3.2, use the official
@kubb/adapter-oas. defineConfig from kubb/config sets it when the config has no adapter. Write your own only for another spec, such as AsyncAPI, GraphQL, JSON Schema, or gRPC.adapterCustom.ts
import { ast, defineExtension } from 'kubb/kit'
import type { AdapterFactoryOptions } from 'kubb/kit'
type AdapterCustom = AdapterFactoryOptions<'custom', { strict?: boolean }, { strict: boolean }>
export const adapterCustom = defineExtension<AdapterCustom>({
kind: 'adapter',
meta: { name: 'custom' },
setup: (options) => ({ strict: options.strict ?? false }),
build: () => ({
document: null,
async parse(_source) {
// Fill schemas and operations from your spec.
const node = ast.factory.createInput({ schemas: [], operations: [] })
return { node, document: null, sources: [] }
},
async validate() {
// Throw here when the spec is invalid.
},
}),
})kubb.config.ts
import { defineConfig } from 'kubb/config'
import { adapterCustom } from './adapterCustom.ts'
export default defineConfig({
input: './my-spec.json',
output: { path: './src/gen' },
adapter: adapterCustom({ strict: true }),
plugins: [],
})build returns these members:
| Member | Type | Required | Purpose |
|---|---|---|---|
document | TDocument | null | Yes | Deprecated. The parsed source document of the last parse(), null before parsing. Return it from parse instead. |
parse | (source: AdapterSource, options?: { signal?: AbortSignal; harness?: Harness }) => { node, document, sources } | Promise<{ node, document, sources }> | Yes | Converts the spec into the AST and returns it as node, with the document and the sources that call read. With harness, read every $ref file with harness.read and every URL with harness.fetch, so the security rules of the run apply. Unset, a $ref reads any file and host. |
validate | (input: string, options?: { throwOnError?: boolean; harness?: Harness }) => Promise<void> | Yes | Validates the document at a path or URL. kubb validate passes throwOnError: true, so throw on invalid input. harness limits $ref reads as in parse. |
settle | () => Promise<void> | No | Waits for work parse left running, such as validation in a worker. Kubb awaits it before kubb:build:end. Must not reject. |
locate | (source: string, location: DiagnosticLocation) => { start: number; end: number } | undefined | No | Finds a diagnostic in the source text for a CLI code frame, as zero-based UTF-16 offsets with end exclusive. |
sources | Array<string> | No | Deprecated. The files and URLs the last parse() read. Return them from parse instead. |
AdapterSource is { type: 'path'; path } or { type: 'data'; data; root? }, where root is the folder relative $refs resolve from.
When the input is invalid, throw from
parse() with a clear message.Adapter naming
| Surface | Pattern | Example |
|---|---|---|
| npm package | @<scope>/adapter-<name> or kubb-adapter-<name> | @kubb/adapter-oas |
meta.name | The spec identifier, in lowercase | 'oas' |
| Factory export | adapter<Name> | adapterOas |
| Name constant | adapter<Name>Name | adapterOasName |
AdapterFactoryOptions alias | Adapter<Name> | AdapterOas |
