@devexperts/swagger-codegen-ts

中文文档
2.0.0-alpha.29发布于 2023/02/17MPL-2.0CommonJS近 30 天下载 1.3万

TS generator for swagger spec

原始使用文档
文档目录4 节
  1. Features
  2. Demo code
  3. Installation
  4. Contributing

Build Status

Typesafe OpenAPI generator for TypeScript

Features

  • Generates client code from OpenAPI 3.0, 2.0 (aka Swagger) and AsyncAPI specs
  • Pluggable HTTP clients: can use fetch, Axios or any other library
  • Flexible response types: works with Promises and reactive streams like RxJS
  • Runtime type checks: validates server responses against the spec
  • Written in pure TypeScript using fp-ts and io-ts libraries

Demo code

The examples below refer to the Pet Store OpenAPI 3.0 schema.

After running the codegen, interacting with a REST API may be as simple as this:

import { petController as createPetController } from "./src/generated/petstore.json/paths/PetController";
import { Pet } from "./src/generated/petstore.json/components/schemas/Pet";

// Creating a controller, see the "HTTP Clients" wiki page for more details
const petController = createPetController({ httpClient: fetchHttpClient });

// The returned object is guaranteed to be a valid `Pet`
const createdPet: Promise<Pet> = petController.addPet({
  body: {
    // The parameters are statically typed, IntelliSense works, too
    name: "Spotty",
    photoUrls: [],
  },
});

More usage scenarios are supported - check the usage page for more detail.

Installation

  1. Make sure the peer dependencies are installed, then install the codegen itself:

    yarn add typescript fp-ts io-ts io-ts-types
    yarn add -D @devexperts/swagger-codegen-ts
    
  2. Create a console script that would invoke the generate function, passing the options such as path to the schema file and the output directory. See the Generators page for the API reference, and examples/generate for sample scripts.

  3. In most cases, you might want to include the code generation step into the build and local launch scripts. Example:

    /* package.json */
    
      "scripts": {
    +   "generate:api": "ts-node scripts/generate-api.ts",
    -   "start": "react-scripts start",
    +   "start": "yarn generate:api && react-scripts start",
    -   "build": "react-scripts build"
    +   "build": "yarn generate:api && react-scripts build"
      }
    

Contributing

Please read the Contributors Guide for more information.

版本与发布记录

@devexperts/swagger-codegen-ts 共发布 52 个版本,首次发布于 2018/10/31;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.0-alpha.29latest2023/02/173 年前
2.0.0-alpha.282022/02/094 年前
2.0.0-alpha.272021/09/145 年前
2.0.0-alpha.262021/07/155 年前
2.0.0-alpha.252021/05/255 年前
2.0.0-alpha.242020/12/255 年前

关于 @devexperts/swagger-codegen-ts 的常见问题

@devexperts/swagger-codegen-ts 是什么?

TS generator for swagger spec

如何安装 @devexperts/swagger-codegen-ts?

在项目目录运行 npm install @devexperts/swagger-codegen-ts,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:fp-ts(^2.1.0)、io-ts(^2.0.5)、io-ts-types(^0.5.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@devexperts/swagger-codegen-ts 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/devexperts__swagger-codegen-ts 是否存在。

@devexperts/swagger-codegen-ts 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@devexperts/swagger-codegen-ts 可以商用吗?

@devexperts/swagger-codegen-ts 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@devexperts/swagger-codegen-ts 的下载量有多少?

根据 npm 官方下载统计,@devexperts/swagger-codegen-ts 在 2026/10/12 – 2026/11/10 这 30 天内被下载 13,231 次,日均约 441 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@devexperts/swagger-codegen-ts 还在维护吗?

最新版本 2.0.0-alpha.29 发布于 2023/02/17(3 年前);至今共发布 52 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/10/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。