Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

openapi-typescript-codegen

ferdikoomen641kMIT0.29.0TypeScript support: included

Library that generates Typescript clients based on the OpenAPI specification.

openapi, swagger, generator, typescript, yaml, json, fetch, xhr, axios, angular, node

readme

Important announcement

Please migrate your projects to use @hey-api/openapi-ts

Due to time limitations on my end, this project has been unmaintained for a while now. The @hey-api/openapi-ts project started as a fork with the goal to resolve the most pressing issues. going forward they are planning to maintain the OpenAPI generator and give it the love it deserves. Please support them with their work and make sure to migrate your projects: https://heyapi.vercel.app/openapi-ts/migrating.html

  • All open PR's and issues will be archived on the 1st of May 2024
  • All versions of this package will be deprecated in NPM

👋 Thanks for all the support, downloads and love! Cheers Ferdi.


OpenAPI Typescript Codegen

NPM License Downloads Build

Node.js library that generates Typescript clients based on the OpenAPI specification.

Why?

  • Frontend ❤️ OpenAPI, but we do not want to use JAVA codegen in our builds
  • Quick, lightweight, robust and framework-agnostic 🚀
  • Supports generation of TypeScript clients
  • Supports generations of Fetch, Node-Fetch, Axios, Angular and XHR http clients
  • Supports OpenAPI specification v2.0 and v3.0
  • Supports JSON and YAML files for input
  • Supports generation through CLI, Node.js and NPX
  • Supports tsc and @babel/plugin-transform-typescript
  • Supports aborting of requests (cancelable promise pattern)
  • Supports external references using json-schema-ref-parser

Install

npm install openapi-typescript-codegen --save-dev

Usage

$ openapi --help

  Usage: openapi [options]

  Options:
    -V, --version             output the version number
    -i, --input <value>       OpenAPI specification, can be a path, url or string content (required)
    -o, --output <value>      Output directory (required)
    -c, --client <value>      HTTP client to generate [fetch, xhr, node, axios, angular] (default: "fetch")
    --name <value>            Custom client class name
    --useOptions              Use options instead of arguments
    --useUnionTypes           Use union types instead of enums
    --exportCore <value>      Write core files to disk (default: true)
    --exportServices <value>  Write services to disk (default: true)
    --exportModels <value>    Write models to disk (default: true)
    --exportSchemas <value>   Write schemas to disk (default: false)
    --indent <value>          Indentation options [4, 2, tab] (default: "4")
    --postfixServices         Service name postfix (default: "Service")
    --postfixModels           Model name postfix
    --request <value>         Path to custom request file
    -h, --help                display help for command

  Examples
    $ openapi --input ./spec.json --output ./generated
    $ openapi --input ./spec.json --output ./generated --client xhr

Documentation

The main documentation can be found in the openapi-typescript-codegen/wiki

Sponsors

If you or your company use the OpenAPI Typescript Codegen, please consider supporting me. By sponsoring I can free up time to give this project some love! Details can be found here: https://github.com/sponsors/ferdikoomen

If you're from an enterprise looking for a fully managed SDK generation, please consider our sponsor:

speakeasy

changelog

Changelog

All notable changes to this project will be documented in this file.

[0.29.0] - 2024-04-05

Please migrate your projects to use @hey-api/openapi-ts

The @hey-api/openapi-ts project started as a fork with the goal to resolve the most pressing issues. going forward they are planning to maintain the OpenAPI generator and give it the love it deserves. Please support them with their work and make sure to migrate your projects: https://heyapi.vercel.app/openapi-ts/migrating.html

Fixed

  • Upgraded dependencies

[0.28.0] - 2024-03-19

Fixed

  • Upgraded dependencies
  • Windows issue when resolving external spec files
  • Media-type for falsy bodies except 'undefined'
  • Updated typo in generated header

    Added

  • Enabled withXSRFToken for Axios

[0.27.0] - 2024-01-15

Fixed

  • Reverted @apidevtools/json-schema-ref-parser to version 10.1.0

[0.26.0] - 2024-01-07

Fixed

  • Upgraded dependencies

[0.25.0] - 2023-07-05

Fixed

  • Upgraded dependencies

    Added

  • Allow usage of a custom axios instance
  • Added message in generated files

[0.24.0] - 2023-04-10

Fixed

  • Upgraded dependencies
  • Fixed issue with Cancelable promise
  • Fixed issue with escaping reserved keywords in schema names

    Added

  • Added --postfixModels option

[0.23.0] - 2022-06-02

Fixed

  • Upgraded dependencies
  • Added blank line at the end of generated files
  • Added support for Node.js v12

    Added

  • Added request property inside ApiError
  • Added support for @depricated inside models and operations

[0.22.0] - 2022-04-26

Fixed

  • Upgraded dependencies
  • Fixed issue with null value inside comments for OpenAPI v2 enums
  • Fixed issue with compatibility for latest version of Axios (0.27.x)

    Removed

  • Removed deprecated enum model generation

[0.21.0] - 2022-04-06

Fixed

  • Return undefined to match noImplicitReturns rule
  • Made BaseHttpRequest class abstract
  • Removed private fields using # inside CancelablePromise
  • Removed unneeded import AbortController from node-fetch client
  • Filter out wrong enum values

[0.20.1] - 2022-02-25

Fixed

  • Support enums with single quotes in names for V2

[0.20.0] - 2022-02-25

Fixed

  • Updated dependencies
  • Support enums with single quotes in names for V3
  • Generating better names when operationId is not given (breaking change)
  • Fixed issue where x-enum flags where breaking due to non-string values

[0.19.0] - 2022-02-02

Added

  • Support for Angular client with --name option
  • Added test cases for Angular client

[0.18.2] - 2022-02-02

Fixed

  • Updated dependencies
  • Fixed type definition

    Added

  • Added test cases for CLI commands
  • Added test cases for query parsing

[0.18.1] - 2022-01-31

Fixed

  • Escaping error description
  • Made Client.request and BaseHttpRequest.config props public

_## [0.18.0] - 2022-01-28

Added

  • Angular client generation!
  • Updated documentation with more examples and better descriptions

[0.17.0] - 2022-01-26

Fixed

  • Shorthand notation for properties passed through constructor
  • Simplified creation of headers
  • Prepare codebase for Angular client

[0.16.2] - 2022-01-26

Fixed

  • Removed dependency on URLSearchParams to support browser and node without any additional imports

[0.16.1] - 2022-01-26

Fixed

  • Correct export inside index.ts when giving a custom name

[0.16.0] - 2022-01-25

Added

  • Added option to set the indentation (spaces and tabs)
  • Added option to export separate client file that allows usage for multiple backends

    Fixed

  • Decoupled OpenAPI object from requests
  • Updated dependencies

[0.15.0] - 2022-01-24

Added

  • Added change log and releases on GitHub

[0.14.0] - 2022-01-24

Fixed

  • Added missing postfix options to typedef
  • Updated escaping of comments and descriptions
  • Better handling of services without tags
  • Updated dependencies