1.2.5 • Published 10 months ago

@wwtest/swagger2ts v1.2.5

Weekly downloads
-
License
ISC
Repository
-
Last release
10 months ago

@wwtest/swagger2ts

swagger文档转ts工具

全局安装

npm i @wwtest/swagger2ts -g

安装好后查看版本

swagger2ts -V

使用方式

在项目根目录创建 swagger2ts.config.js 配置文件

文件内容如下(配置项参考下文):

module.exports = {
  url: "http://192.168.3.200:10052/v2/api-docs",
  typesDir: "./src/types",
  servicesDir: "./src/service",
};

在项目根目录运行

swagger2ts

默认配置文件内容如下

// 默认配置
module.exports = {
  // url: 'http://192.168.3.200:10052/v2/api-docs', // swagger文档地址
  typesDir: "./src/types", // 类型定义输出目录 必填
  servicesDir: "./src/service", // services输出目录 必填
  ignoreKeys: ["Authorization", "Tenant-Id"], // 忽略掉的传参字段
  ignoreType: ["header"], // 忽略掉的传参位置
  ignoreUrl: [], // 忽略掉的接口url
  ignoreUrlPrefix: [], // 忽略掉的接口url前缀
  typeTopConfig: ``, // type文件顶部公用内容
  serviceTopConfig: `import request from '@/utils'\n`, // service文件顶部公用内容
  getTem: `return request.get($requestUrl, $params)`, // get方法body代码模版
  postTem: `return request.post($requestUrl, $params)`, // post方法body代码模版
  exportApiTem: `export const $apiName = (params: $reqInterface): $resInterface => {
      $body
  }
  `, // serviceApiType 为 default 时生效
  serviceApiType: 'default', // default 默认 class class形式输出
};

指令

swagger2ts --version (-V) // 查看版本号
swagger2ts --help (-h) // 查看帮助信息
swagger2ts --config (-c) // 指定配置文件

注意事项

目前只支持swagger2.0版本

只支持 get 和 post 请求方式,可根据项目不同 配置 get 和 post 请求模板

使用该工具前,请务必保证您的请求方法封装完成

目前返回信息中只导出data字段类型定义

更新事项

2023-07-14

v1.1.4

fix: 修复重复输出interface的问题

2023-07-14

v1.2.0

feat: types和service层分离

2023-07-14

v1.2.3

fix: 修复某些场景service引入types缺少的问题

2023-07-18

v1.2.4

feat: 支持class service形式输出

1.2.5

10 months ago

1.2.4

10 months ago

1.2.3

10 months ago

1.2.2

10 months ago

1.2.1

10 months ago

1.2.0

10 months ago

1.1.4

10 months ago

1.1.3

10 months ago

1.1.2

10 months ago

1.1.1

10 months ago

1.1.0

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago