openapi-ts-json-schema
Version:
Generate TypeScript-first JSON schemas from OpenAPI definitions
43 lines (42 loc) • 2.37 kB
TypeScript
import type { Options, ReturnPayload } from './types.js';
/**
* @package openapi‑ts-json-schema
*
* Convert OpenAPI definitions to TypeScript JSON Schema modules.
*
* @remarks
* This function resolves and dereferences `$ref`s in an OpenAPI document, converts OpenAPI schema objects
* to JSON Schema, and writes out `.ts` modules exporting `as const` JSON schemas. It mirrors your OpenAPI
* structure in the output and supports plugins (e.g. Fastify integration).
*
* @param options - Configuration for the conversion
* @param options.openApiDocument - Path to an OpenAPI document (JSON or YAML)
* @param options.targets - OpenAPI definition paths to generate JSON Schemas from
* @param options.targets.collections - Array of paths pointing to objects/records of definitions, where each entry will be generated (eg: `["components.schemas"]`)
* @param options.targets.single - Array of paths pointing to individual definitions to generate (eg: `["paths./users/{id}"]`)
* @param options.refHandling - Strategy for `$ref` processing (`"import"` | `"inline"` | `"keep"`) (default: `"import"`)
* @param options.moduleSystem - Controls how import specifiers are written in generated artifacts. Configure this option based on whether the consuming project is using CommonJS or ECMAScript modules
* @param options.outputPath - Directory where generated schemas will be written. (default: `schemas-autogenerated`)
* @param options.silent - If `true`, suppress logging output (default: `false`)
*
* @param options.idMapper - Optional function to map internal schema id strings to custom `$id` or import names
* @param options.schemaPatcher - Hook called for every generated schema node, allowing programmatic mutation before output
* @param options.plugins - Optional list of plugins for custom generation behavior
*
* @returns A Promise resolving to an object with:
* - `outputPath`: the folder path where schemas were generated
* - `metaData`: a map of schema identities to metadata (ids, file paths, import paths, etc.)
*
* @example
* ```ts
* import { openapiToTsJsonSchema } from "openapi‑ts-json-schema";
*
* await openapiToTsJsonSchema({
* openApiDocument: "path/to/spec.yaml",
* targets: {
* collections: ["paths"],
* },
* });
* ```
*/
export declare function openapiToTsJsonSchema(options: Options): Promise<ReturnPayload>;