@contractkit/openapi-to-ck
Convert OpenAPI specs (2.0, 3.0, 3.1) into ContractKit .ck files. Useful for adopting ContractKit in projects that already have an OpenAPI document, or for round-tripping a spec through CK.
Installation
pnpm add -D @contractkit/openapi-to-ck
Use as a CLI subcommand
When the package is installed, the @contractkit/cli binary picks up an openapi-to-ck subcommand:
contractkit openapi-to-ck --input openapi.yaml --output contracts/
--input <path> | Path to an OpenAPI YAML or JSON file. |
--output <dir> | Directory to write .ck files into. |
--split <single | by-tag> | Output mode. by-tag (default) writes one file per OpenAPI tag; single writes one combined file. |
--no-comments | Don't emit OpenAPI descriptions as # comments. |
Programmatic use
import { convertOpenApiToCk } from '@contractkit/openapi-to-ck';
const { files, warnings } = await convertOpenApiToCk({
input: 'openapi.yaml',
split: 'by-tag',
includeComments: true,
onWarning: w => console.warn(w),
});
for (const [filename, source] of files) {
await fs.writeFile(filename, source);
}
Warning entries carry a JSON-pointer-style path into the OpenAPI spec, a human-readable message, and a severity of 'info' | 'warn'.
What's converted
paths | operation /path: { ... } blocks |
components.schemas | contract Name: { ... } declarations |
parameters (path / query / header) | params: { ... } / query: { ... } / headers: { ... } blocks |
requestBody.content[mime].schema | request: { mime: Type } |
responses[code].content[mime].schema | response: { code: { mime: Type } } |
responses[code].headers | per-status headers: { name: type } |
allOf | & intersection on contract declarations |
oneOf + discriminator | discriminated(by=field, A | B) |
oneOf / anyOf (no discriminator) | | union |
enum, pattern, minimum/maximum, minLength/maxLength | type constraint args |
Other exports
For tools that want to operate on the intermediate AST rather than emit .ck source:
import {
schemasToModels,
pathsToRoutes,
splitByTag,
mergeIntoSingle,
detectCircularRefs,
extractRefName,
sanitizeName,
normalize,
astToCk,
serializeType,
} from '@contractkit/openapi-to-ck';