TS 中文文档 TS 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • 入门教程

    • TypeScript 手册
    • 基础知识
    • 日常类型
    • 类型缩小
    • 更多关于函数
    • 对象类型
    • 从类型创建类型
    • 泛型
    • Keyof 类型运算符
    • Typeof 类型运算符
    • 索引访问类型
    • 条件类型
    • 映射类型
    • 模板字面类型
    • 类
    • 模块
  • 参考手册

  • 项目配置

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


  1. ``` sh
  2. npm install openapi-typescript-codegen --save-dev

  3. ```

Usage


  1. ``` sh
  2. $ openapi --help

  3.   Usage: openapi [options]

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

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

  24. ```

Docker usage


To build the Docker container, execute the following command:

  1. ``` sh
  2. docker build . --tag openapi-typescript-codegen

  3. ```

After this is done, you can execute the CLI commands:

  1. ``` sh
  2. docker run openapi-typescript-codegen --help
  3. docker run openapi-typescript-codegen --input sample.yaml --output client

  4. ```

Documentation


Basic usage
OpenAPI object
Client instances --name
Argument vs. Object style --useOptions
Enums vs. Union types --useUnionTypes
Runtime schemas --exportSchemas
Enum with custom names and descriptions
Nullable props (OpenAPI v2)
Authorization
External references
Canceling requests
Custom request file

Support


Babel support
Axios support
Angular support
Node-Fetch support
Last Updated: 2023-09-03 17:10:52