# swagger-api-gen

> this tool generates frontend api code by swagger v2 api

Latest version **1.3.7** (published 2026-07-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install swagger-api-gen
pnpm add swagger-api-gen
yarn add swagger-api-gen
bun add swagger-api-gen
```

Provides the command `api`.

## Health

**Score 60/100 (C)** — status: active.

Positive: esm support; no vulnerabilities; recently updated; high maintenance score.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 1.3.7 |
| Published | 2026-07-23 |
| First published | 2021-12-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 30.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | dreamcreativehub@gmail.com |
| Maintainers | handsome_walker |
| Keywords | swagger, api, generator, cli |

## Links

- npm: https://www.npmjs.com/package/swagger-api-gen
- Repository: https://github.com/HandsomeWalker/swagger-api-generator
- Homepage: https://github.com/HandsomeWalker/swagger-api-generator#readme
- Issues: https://github.com/HandsomeWalker/swagger-api-generator/issues
- npm.io page: https://npm.io/package/swagger-api-gen

## Dependencies (1)

- [openapi-typescript](https://npm.io/package/openapi-typescript.md) 5

## Alternatives

- [@salesforce/cli](https://npm.io/package/@salesforce/cli.md) — 389.7K weekly downloads
- [@mintlify/cli](https://npm.io/package/@mintlify/cli.md) — 208.9K weekly downloads
- [@grafana/e2e-selectors](https://npm.io/package/@grafana/e2e-selectors.md) — 128.7K weekly downloads
- [mintlify](https://npm.io/package/mintlify.md) — 112.0K weekly downloads
- [@intlayer/cli](https://npm.io/package/@intlayer/cli.md) — 22.8K weekly downloads

## Recent versions

- 1.3.7 (latest) — 2026-07-23
- 1.3.6 — 2026-07-15
- 1.3.5 — 2025-08-05
- 1.3.4 — 2025-07-26
- 1.3.3 — 2025-07-26
- 1.3.2 — 2025-07-08
- 1.3.1 — 2025-07-08
- 1.3.0 — 2025-07-08
- 1.2.3 — 2025-07-07
- 1.2.2 — 2024-03-07
- 1.2.1 — 2024-01-10
- 1.2.0 — 2023-07-13
- 1.1.0 — 2023-06-13
- 1.0.16 — 2023-03-31
- 1.0.15 — 2022-10-08
- … 13 more at https://npm.io/package/swagger-api-gen/versions

## README

# swagger-api-generator
swagger接口前端代码生成命令行工具，支持ts和js，适用于v2文档

# 特性

🛠️ 生成请求方法

📦 完整的接口入参和响应类型

📄 完整的jsdoc注释

🛜 自定义请求客户端

🌲 treeshaking友好

🧩 支持按模块生成文件

# 生成结果
```typescript
import { paths } from "./schema";
import request, {
  type ParamsProps,
  type ResponseProps,
  type CustomConfigProps,
} from "./client";
import { objToFormData } from "./utils";

/**
 * pet-uploads an image
 * @param {string | number} petId description: ID of pet to update | required: true | type: integer
 * @param {string | number} additionalMetadata description: Additional data to pass to server | required: false | type: string
 * @param {any} file description: file to upload | required: false | type: file
 */
export const petByPetIdUploadImagePOST = (
  paramConfig: ParamsProps<paths["/pet/{petId}/uploadImage"]["post"]>,
  customConfig: CustomConfigProps = {}
) =>
  request<ResponseProps<paths["/pet/{petId}/uploadImage"]["post"]>>({
    url: `/pet/${paramConfig.path["petId"]}/uploadImage`,
    method: "post",
    data: objToFormData(paramConfig.formData),
    headers: { "Content-Type": "multipart/form-data" },
    ...customConfig,
  });

/**
 * pet-Add a new pet to the store
 * @param {any} body description: Pet object that needs to be added to the store | required: true | type: undefined
 */
export const petPOST = (
  paramConfig: ParamsProps<paths["/pet"]["post"]>,
  customConfig: CustomConfigProps = {}
) =>
  request<any>({
    url: "/pet",
    method: "post",
    data: paramConfig.body["body"],
    ...customConfig,
  });
```

# 安装
```
npm install -D swagger-api-gen
```
# 用法
## 命令行
全局运行
```
api url=http://example.com/v2/api-docs
```
项目内运行
```
npx api url=http://example.com/v2/api-docs
```
## npm script使用
1.配置package.json
```
{
    "script": {
        "api": "api url=http://example.com/v2/api-docs"
    }
}
```
2.运行 `npm run api` 或 `yarn api`
## 完整示例
```
api url=http://example.com/v2/api-docs tarDir=./src/api fileName=index fileType=ts template='import request from "./request";' expandParams=true filter=pet client=true mock=true module=true
```
# 参数说明
|参数|必传|说明|默认|示例|
|----|----|----|----|----|
|url|是|swagger api地址|-|url=http://example.com/v2/api-docs|
|tarDir|否|目标目录|`当前目录`|tarDir=./src/api|
|fileName|否|生成文件名，当module选项为true时不生效|`swagger-api`|fileName=index|
|fileType|否|生成文件类型`js`或`ts`|`ts`|fileType=js|
|template|否|顶部自定义的代码段|-|template='import request from "./request";'|
|expandParams|否|是否展开传参|`false`|expandParams=false|
|filter|否|通过正则匹配接口path来筛选需要生成的接口|-|filter=pet|
|client|否|是否生成请求客户端|`false`|client=true|
|mock|否|是否生成mock请求|`false`|mock=true|
|module|否|是否分模块|`false`|module=true|

# 注意
使用 `git for windows` 终端时，参数首位的 `/` 会被解析为 `$GIT_HOME/`

解决方案如下
```
filter=//pet

MSYS_NO_PATHCONV=1 filter=/pet/find

filter=\\/pet/find
```

---
_Source: https://npm.io/package/swagger-api-gen · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
