Skip to content
| Marketplace
Sign in
Visual Studio Code>Other>swagger2codeNew to Visual Studio Code? Get it now.
swagger2code

swagger2code

hawjs

|
14 installs
| (1) | Free
全新推出的 swagger2code - 您的终极 VSCode 插件,轻松生成前端请求联调代码!
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

使用教程

第一步 配置swagger地址

支持以下配置方式:

  • 项目根目录下的package.json中定义swagger字段
  • 项目根目录下的 swaggerrc.[json|yaml|yml|js|cjs]文件
  • 项目根目录下的 swagger.[config.js|config.cjs]文件

示例

.swaggerrc.json

{
  "services": [
    {
      "name": "user",
      "url": "http://xxxx.xxx.xx/api-docs"
    }
  ]
}

字段解释:

  • services[n].name Swagger名称
  • service[n].url Swagger地址

第二步 配置项目模版文件

方式一:手动创建适用于您项目的代码

文件路径:/_templates/api/new/index.ejs.t , 可以根据项目实际情况增加该模版。

---
to: src/api.js
inject: true
append: true
skip_if: <%= method + h.changeCase.pascal(path) %>
---

// <%= summary %>
<% requstBodyMethodList=["post",'put'] -%>
<% methodMap={delete:"del"} -%>
export const <%= method + h.changeCase.pascal(path) %> = <%= requstBodyMethodList.includes(method) ? "({ data })" : "({ params })" %> => {
  const url = `${CURRENT_API_BASE}<%= path %>`
  return <%= methodMap[method] || method %>({ url<%= requstBodyMethodList.includes(method) ? ", data":", params" %> })
}

方式二:使用swagger2code.init

使用快捷键 Ctrl+Shift+P (Mac对应 Command+Shift+p) 打开命令输入框输入对应命令

自动创建模版文件,路径:/_templates/api/new/index.ejs.t , 可以根据项目实际情况增加该模版。

第三步 生成代码

  • swagger2code.gen

选择对应的API并生成API到对应的文件(需要保证文件存在),比如上一步方式一生成代码的位置,文件路径为 src/api.js ,确保该路径下有该文件。

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2025 Microsoft