1.0.0 • Published 4 years ago

oapi2ts v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

oapi2ts

NPM version NPM download Build Status

oapi2ts is an utility to create TypeScript types from and OpenAPI specifications and JSON schemas. Unlike other code generators oapi2ts does not use templates to generate code but uses TypeScript's built-in API to generate and pretty-print an abstract syntax tree (AST).

Features

  • AST-based: Unlike other code generators oapi2ts does not use templates to generate code but uses TypeScript's built-in API to generate and pretty-print an abstract syntax tree.
  • Tree-shakeable: Individually exported types allows you to bundle only the ones you actually use.
  • YAML or JSON: Use YAML or JSON for your or OpenAPI Specifications and JSON Schemas.
  • External references: Resolves automatically external references and bundle or import them in generated files.
  • Implementation agnostic: Use generated types in any projet or framework.

Installation

Install in your project:

npm install oapi2ts

Or direct use using npx:

npx oapi2ts <schemas..>

CLI Usage

Generate TypeScript types from OpenAPI Specifications

oapi2ts openapi [options] <specification..>
KeyTypeDefaultDescription
--output, -ostringSame directoryOutput directory for generated types.
--cwd, -cstringSpecification relativeRoot directory for resolving $ref s
--banner, -bstringundefinedComment prepended to the top of each generated file

Generate TypeScript types from JSON Schemas

oapi2ts models [options] <schemas..>
KeyTypeDefaultDescription
--output, -ostringSame directoryOutput directory for generated types.
--cwd, -cstringSchema relativeRoot directory for resolving $ref s
--banner, -bstringundefinedComment prepended to the top of each generated file

Programmatic Usage

Generate TypeScript types from OpenAPI Specifications

import { parseOpenApi, printNodes } from "oapi2ts";

async function generateSchema(schema: JSONSchema): Promise<string> {
    const result = await parseOpenApi(schema);
    return printNodes(result.all);
}

Generate TypeScript types from JSON Schemas

import { parseSchema, printNodes } from "oapi2ts";

async function generateSchema(schema: JSONSchema): Promise<string> {
    const ast = await parseSchema(schema);
    return printNodes(ast);
}

License

This project is under MIT License. See the LICENSE file for the full license text.