1.0.0 • Published 4 years ago
oapi2ts v1.0.0
oapi2ts
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..>
Key | Type | Default | Description |
---|---|---|---|
--output, -o | string | Same directory | Output directory for generated types. |
--cwd, -c | string | Specification relative | Root directory for resolving $ref s |
--banner, -b | string | undefined | Comment prepended to the top of each generated file |
Generate TypeScript types from JSON Schemas
oapi2ts models [options] <schemas..>
Key | Type | Default | Description |
---|---|---|---|
--output, -o | string | Same directory | Output directory for generated types. |
--cwd, -c | string | Schema relative | Root directory for resolving $ref s |
--banner, -b | string | undefined | Comment 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.
1.0.0
4 years ago