8000 GitHub - mengyaolin/ts-codegen: Typescript code generator from swagger. 一个生成前端接口层代码和对应 TypeScript 定义的工具。
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Typescript code generator from swagger. 一个生成前端接口层代码和对应 TypeScript 定义的工具。

Notifications You must be signed in to change notification settings

mengyaolin/ts-codegen

 
 

Repository files navigation

TS Codegen

Build Status codecov License NPM

TS Codegen 是一个用于生成「前端接口层代码」以及对应「TypeScript 定义」的工具。你只需要提供一个 Swagger 或 Openapi 的 JSON 文件,它就可以为您生成相应的代码。

快速尝试 Try it

可以通过下面的命令进行快速尝试:

git clone https://github.com/reeli/ts-codegen-examples
cd  ts-codegen-examples
npm install
npx ts-codegen

然后我们就可以在 clients 目录下看到生成的结果,并开始使用(参考 src/index.ts 8000 中的代码)。

开始

  1. 安装

    npm install @ts-tool/ts-codegen-cli -D

  2. 生成配置文件

    npx ts-codegen init

    这个命令会在你的 project 根目录下生成一个配置文件:ts-codegen.config.json。

  3. 修改配置文件

    根据自己的需求修改文件 ts-codegen.config.json,配置必填的 requestCreateLibapiSpecsPaths 参数后,即可使用。参数说明详见 附录:TS Codegen Config 参数说明

  4. 执行命令

    cd 到你项目的根目录下,执行如下命令:

    npx ts-codegen

    然后就可以在配置的 clients 目录下看到生成的结果,并开始使用。

使用核心依赖进行二次封装

如果您想自己编写命令行工具,或者对核心功能进行一些修改,可以使用 npm intall @ts-tool/ts-codegen-core 安装核心依赖包,然后根据核心依赖包提供的方法进行二次封装。

版本迁移

请参考版本迁移文档

附录:TS Codegen Config 参数说明

  • requestCreateMethod: String [可选项]

    表示方法或函数名,用于创建发起请求的函数。默认值为 createRequest。你可以自己实现 requestCreateMethod 方法,也可以参考示例代码。下面是一个基于 axios 实现的 createRequest 示例:

    import axios, { AxiosRequestConfig } from "axios";
    
    const axiosInstance = axios.create({
      baseURL: "https://petstore.swagger.io",
    });
    
    export const createRequest = <TReq, TResp = any>(
      _: string,
      requestConfigCreator: (args: TReq) => AxiosRequestConfig,
    ) => {
      return (args: TReq) => {
        return axiosInstance.request<TResp>(requestConfigCreator(args));
      };
    };
  • requestCreateLib: String [必填项]

    表示 requestCreateMethod 所在的文件路径。用于导入 requestCreateMethod 方法。

  • apiSpecsPaths: Array [必填项]

    用于配置 swagger/openapi 文件所在的地址(path),以及它对应的生成文件的名称(name)。其中 path 既可以是远端 url,也可以是本地 swagger/openapi 所在的文件路径。CLI 工具会根据你的配置,自动读取远端或者本地文件,生成对应代码。目前支持的文件格式有 .json, .yaml, .yml

    {
      apiSpecsPaths: [
        {
          path: "https://petstore.swagger.io/v2/swagger.json", // 远程获取 Swagger 文档(json 格式)
          name: "PetStoreService1",
        },
        {
          path: "https://petstore.swagger.io/v2/swagger.yaml", // 远程获取 Swagger 文档(yaml 格式)
          name: "PetStoreService2",
        },
        {
          path: "./examples/swagger.json", // 从本地文件读取 Swagger 文档(json 格式)
          name: "SwaggerService",
        },
        {
          path: "./examples/openapi.json", // 从本地文件读取 OpenAPI 文档(json 格式)
          name: "OpenApiService",
        },
        {
          path: "./examples/demo.yaml", // 从本地文件读取 Swagger 文档(yaml 格式)
          name: "DemoService",
        },
      ],
    }

    注意:提供的 Swagger/Openapi json 中,必须保证每个 API 请求都包含属性 operationId

  • outputFolder: String [可选项]

    表示输出生成代码的目录名称。默认值为 clients。

  • options: Object [可选项]

    表示一些额外配置。其中,typeWithPrefixbackwardCompatible 仅在需要兼容老版本时进行配置。

    • typeWithPrefix: Boolean [可选项],默认值: false

      如果设置为 true,会为所有的生成的 interface 和 type 加上前缀,其中 interface 加上 I 前缀,type 加上 T 前缀。

    • backwardCompatible: Boolean [可选项],默认值: false

      用于兼容老版本,一般不推荐设置为 true。如果你使用了之前的版本,并且希望尽可能兼容以前的老版本,可以将其设置为 true。

    • withComments: Boolean [可选项],默认值: false

      用于设置在生成代码中是否显示注解。比如你在 swagger 文档中通过 summary, description 等字段,为一个 API 添加了描述,你就可以通过这个开关来控制这个描述是否显示在最终的生成代码中。

About

Typescript code generator from swagger. 一个生成前端接口层代码和对应 TypeScript 定义的工具。

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 99.8%
  • Other 0.2%
0