OpenAPI TypeScript Codegen
Library that generates TypeScript clients based on the OpenAPI specification. It supports various HTTP clients like Fetch, Axios, Node-Fetch, Angular, and XHR, and handles both OpenAPI v2.0 and v3.0 definitions in JSON or YAML formats. Key features included support for external references, abortable requests, and both CLI and programmatic generation. The last released version is 0.30.0. However, this package is now officially deprecated and no longer actively maintained due to time limitations from the original author. Users are strongly advised to migrate to `@hey-api/openapi-ts`, which is a maintained fork addressing previous issues and is under active development. All open PRs and issues for `openapi-typescript-codegen` were archived on May 1st, 2024, and the package is being deprecated on npm. There will be no further releases or official support for this package, making its release cadence effectively ceased.
Common errors
-
Error: Required option '--input <value>' not specified
cause The command-line interface requires an OpenAPI specification input file or URL.fixProvide the input path or URL using the `--input` (or `-i`) flag: `npx openapi --input ./spec.json --output ./generated` -
Error: Required option '--output <value>' not specified
cause The command-line interface requires an output directory where the generated client files will be written.fixSpecify the output directory using the `--output` (or `-o`) flag: `npx openapi --input ./spec.json --output ./generated` -
TypeError: generate is not a function
cause Attempting to call `generate` programmatically without correctly importing it or if the package is not resolved correctly in a CommonJS context.fixEnsure you are using `import { generate } from 'openapi-typescript-codegen';` for ESM. If in a CommonJS module, explicitly destructure, but consider that this package is deprecated and programmatic usage might be unstable; migration to `@hey-api/openapi-ts` is the long-term solution.
Warnings
- breaking This package is officially deprecated and no longer maintained. All active development has ceased, and the author recommends migrating to the `@hey-api/openapi-ts` package.
- breaking All open Pull Requests and Issues for `openapi-typescript-codegen` were archived on May 1st, 2024. No further support, bug fixes, or new features will be implemented for this package.
- gotcha Deprecated enum model generation was removed in version 0.22.0.
- gotcha Compatibility issues with Axios versions 0.27.x were fixed in v0.22.0. Older versions might not generate functional Axios clients.
- gotcha Due to issues, the internal dependency `@apidevtools/json-schema-ref-parser` was reverted to version 10.1.0 in v0.27.0. Newer features or bug fixes in later parser versions are not available.
Install
-
npm install openapi-typescript-codegen -
yarn add openapi-typescript-codegen -
pnpm add openapi-typescript-codegen
Imports
- generate
const { generate } = require('openapi-typescript-codegen');import { generate } from 'openapi-typescript-codegen'; - openapi (CLI)
openapi --input ./spec.json --output ./generated
npx openapi --input ./spec.json --output ./generated
Quickstart
npx openapi \
--input https://petstore.swagger.io/v2/swagger.json \
--output ./generated \
--client axios \
--postfixServices Service \
--postfixModels Model
// To use the generated client:
// import { PetService } from './generated/services/PetService';
// import { AxiosHttpRequest } from './generated/core/AxiosHttpRequest';
// const httpRequest = new AxiosHttpRequest({
// baseUrl: 'https://petstore.swagger.io/v2'
// });
// const petService = new PetService(httpRequest);
// petService.findPetsByStatus('available')
// .then(pets => console.log(pets))
// .catch(error => console.error(error));