What is openapi-typescript-codegen?
The openapi-typescript-codegen npm package is a tool that generates TypeScript clients from OpenAPI specifications. It helps developers to create strongly-typed API clients, ensuring type safety and reducing the likelihood of runtime errors.
What are openapi-typescript-codegen's main functionalities?
Generate TypeScript Client
This feature allows you to generate a TypeScript client from an OpenAPI specification file. The generated client will include all the necessary types and methods to interact with the API.
const { generate } = require('openapi-typescript-codegen');
generate({
input: './path/to/openapi.yaml',
output: './generated',
clientName: 'MyApiClient'
});
Custom Templates
This feature allows you to use custom templates for generating the TypeScript client. This can be useful if you need to adhere to specific coding standards or want to customize the generated code.
const { generate } = require('openapi-typescript-codegen');
generate({
input: './path/to/openapi.yaml',
output: './generated',
templates: './path/to/custom/templates'
});
Generate Models Only
This feature allows you to generate only the TypeScript models from an OpenAPI specification file. This can be useful if you only need the type definitions and not the full client.
const { generate } = require('openapi-typescript-codegen');
generate({
input: './path/to/openapi.yaml',
output: './generated/models',
onlyModels: true
});
Other packages similar to openapi-typescript-codegen
openapi-generator
OpenAPI Generator is a comprehensive tool that supports generating API clients, server stubs, API documentation, and configuration in various languages. Compared to openapi-typescript-codegen, it offers broader language support and more customization options.
swagger-typescript-api
swagger-typescript-api is a tool that generates TypeScript API clients from Swagger/OpenAPI definitions. It focuses on simplicity and ease of use, similar to openapi-typescript-codegen, but may offer different customization options and templates.
typescript-fetch
typescript-fetch is a generator for TypeScript clients using the Fetch API. It is part of the OpenAPI Generator project and provides a different approach to generating TypeScript clients compared to openapi-typescript-codegen, focusing on using the Fetch API for HTTP requests.
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
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
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:
[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