Skip to content
| Marketplace
Sign in
Visual Studio Code>Other>openapi-to-tsNew to Visual Studio Code? Get it now.
openapi-to-ts

openapi-to-ts

wuguanwen

|
35 installs
| (0) | Free
openapi转ts
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

Openapi-To-Ts

插件说明

  • 我们可以使用这个插件通过 openapi (Swagger Api Json) 生成对应API的TS类型(入参/出参)

所有的命令

  1. create-config-file:创建配置文件 在根目录创建配置文件
  2. gen-serviceTP:生成TS声明文件 根据openapi生成对应API的TS文件

如何使用?

1: 创建配置文件

  1. 自动创建:Shift+Ctrl+p 搜索 OpenapiTots: 创建配置文件 并执行命令
  2. 手动创建:在项目的根目录创建, 支持的文件名

2: 配置openapi.json的地址

  1. 配置文件中的 openapiJsonUrl 字段,类型请参考:参数说明

3: 生成

  1. 按Shift+Ctrl+p 搜索 OpenapiTots: 生成TS声明文件 并执行命令

配置文件名称支持情况

  • .openapirc
  • .openapirc.json
  • .openapirc.js
  • .openapirc.mjs
  • .openapirc.cjs
  • openapi.config.js
  • openapi.config.mjs
  • openapi.config.cjs

配置参数说明

参数 说明 类型 默认值
openapiJsonUrl openapi的json路径, namespace的优先级比外层的高 Array<{label: string, value: string, namespace?: string}> -
namespace ts类型声明的命令空间名称 string API
serversPath 生成的文件夹的路径,会多包裹一层 namespace string ./src/services/${namespace}
requestLibPath 请求库的路径 说明 string @utils/request
requestConfigType 请求方法的 config 参数类型 string {[key: string]: any}
requestConfigTypeLibPath 引入config参数类型的路径 说明 string -
serviceTemplate service函数模板 说明 template1 | template2 | template3 template1
enumStyle 枚举类型的样式 说明 string-literal | enum string-literal
nullable 使用null代替可选 boolean false
prettierrc prettier格式化配置, 优先级比根目录的prettier配置高 object {}

参数额外说明

  1. serviceTemplate

    // 预设了三种模板,不可以自定义
    // template1
    request.post(url, data/params, config)
    // template2
    request.post(url, {data, params, ...config})
    // template3
    request(url, {method, data, params, ...config})
    
  2. requestLibPath

    • 如果以import开头则替换掉整个表达式,
    • 否则只替换掉路径,import request from ${requestLibPath}
  3. requestConfigTypeLibPath

    • 如果config的类型与request方法在同一个文件导出, 可以与 requestLibPath 一同设置
  4. enumStyle

    // `string-literal` 使用字符串字面量
    type Direction = "UP" | "DOWN"
    // `enum` 使用枚举类型
    enum Direction { Up = "UP", Down = "DOWN"}
    
  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2025 Microsoft