yapi-generator-ts-mcp
v1.4.7
Published
YAPI to TypeScript MCP Server
Maintainers
Readme
YAPI TypeScript Generator
🚀 最强大的 YAPI 到 TypeScript 类型生成器 支持单个接口和分类批量生成,输出供复制使用,极致体验!
🌟 为什么选择这个工具?
✨ 核心优势
- 🎯 极简使用:一句话即可生成完整的 TypeScript 类型定义
- 📝 双输出模式:支持复制模式(控制台输出)+ 文件模式(自动保存)
- 🚀 双模式支持:支持 Cursor IDE 智能调用 + 命令行直接使用
- 🔥 批量生成:支持指定分类的批量生成
- 📂 分类分组:按API分类自动组织代码结构
- 🔍 智能过滤:支持关键字、方法、状态多维度过滤
- 🎯 智能识别:自动识别URL类型(接口/分类)无需手动指定参数
- 💾 文件输出:
--dir参数直接保存文件,省去复制粘贴 - 💡 零配置:无需复杂配置,安装即用
- 🔧 完整输出:类型定义 + API 函数 + 使用示例一步到位
- ⚡ 快速响应:秒级生成,提升开发效率
🆚 与其他工具的区别
| 特性 | YAPI TypeScript Generator | 传统工具 | |------|---------------------------|----------| | 使用复杂度 | 一句话生成 | 需要配置文件、学习命令 | | 批量生成 | 支持分类批量生成 | 通常只支持单个接口 | | 复制便利性 | 优化复制体验 | 需要从复杂结构中提取 | | URL识别 | 智能识别接口/分类URL | 需要手动指定参数 | | 分类组织 | 自动按API分类分组 | 需要手动整理 | | 过滤功能 | 多维度智能过滤 | 功能有限 | | IDE 集成 | 原生 Cursor 支持 | 需要额外插件 | | 学习成本 | 几乎为零 | 需要阅读文档 |
🚀 快速开始
步骤 1:安装
npm install yapi-generator-ts-mcp步骤 2:初始化(仅需一次)
npx yapi-init💡 这一步只配置 Cursor MCP 服务器,不会创建任何额外文件
步骤 3:开始使用
🎨 在 Cursor 中使用(推荐)
单个接口生成:
请使用YAPI Generator工具执行 npx yapi-generator-ts-mcp https://yapi.cht-group.net/project/27/interface/api/4144 生成TypeScript类型批量生成(分类):
# 生成指定分类(输出供复制)
请使用YAPI Generator工具执行 npx yapi-generator-ts-mcp https://yapi.cht-group.net/project/27/interface/api/cat_305 批量生成TypeScript类型⚡ 命令行使用
单个接口生成:
# 直接传入接口URL
npx yapi-generator-ts-mcp https://yapi.cht-group.net/project/27/interface/api/4144
# 使用参数形式
npx yapi-generator-ts-mcp --url https://yapi.cht-group.net/project/27/interface/api/4144批量生成(分类):
# 生成指定分类(输出供复制,推荐)
npx yapi-generator-ts-mcp https://yapi.cht-group.net/project/27/interface/api/cat_305
# 传统方式生成指定分类
npx yapi-generator-ts-mcp --project https://yapi.cht-group.net/project/27/interface/api/cat_305 --category 305
# 带过滤条件的批量生成
npx yapi-generator-ts-mcp https://yapi.cht-group.net/project/27/interface/api/cat_305 --filter-keyword "用户" --filter-method "POST"
# 以 JSON 格式输出
npx yapi-generator-ts-mcp https://yapi.cht-group.net/project/27/interface/api/4144 --json
npx yapi-generator-ts-mcp https://yapi.cht-group.net/project/27/interface/api/cat_305 --json
# 输出到指定目录
npx yapi-generator-ts-mcp https://yapi.cht-group.net/project/27/interface/api/4144 --dir ./types
npx yapi-generator-ts-mcp https://yapi.cht-group.net/project/27/interface/api/cat_305 --dir ./generated
# 自定义文件名输出
npx yapi-generator-ts-mcp https://yapi.cht-group.net/project/27/interface/api/4144 --dir ./types --filename user-api
npx yapi-generator-ts-mcp https://yapi.cht-group.net/project/27/interface/api/cat_305 --dir ./generated --filename my-project📖 详细功能说明
🚀 批量生成功能
🎯 智能URL识别
工具会自动识别URL类型,无需手动指定参数:
- 接口URL:自动识别为单个接口生成
- 分类URL:自动识别为分类批量生成
过滤选项
--filter-keyword <TEXT>:按关键字过滤(搜索标题、路径、描述)--filter-method <METHOD>:按HTTP方法过滤(GET、POST、PUT、DELETE等)--filter-status <STATUS>:按状态过滤
输出选项
--no-group:不按分类分组,统一输出-j, --json:以 JSON 格式输出结果(适合程序化处理)-d, --dir <PATH>:输出到指定目录,自动生成文件(默认输出到控制台)-f, --filename <NAME>:自定义文件名(不含扩展名,需配合--dir使用)
🎯 支持的 URL 格式
# 单个接口URL
https://yapi.example.com/project/{projectId}/interface/api/{interfaceId}
# 分类URL
https://yapi.example.com/project/{projectId}/interface/api/cat_{categoryId}示例:
- 单个接口:
https://yapi.cht-group.net/project/27/interface/api/4144 - 分类批量:
https://yapi.cht-group.net/project/27/interface/api/cat_305
📝 输出格式说明
单个接口输出
文本格式(默认):
✅ **TypeScript 类型生成完成**
📋 **接口信息:** 用户登录 | POST /api/user/login
═══════════════════════════════════════════════════════════════
📝 **类型定义 (复制下方代码)**
═══════════════════════════════════════════════════════════════
[TypeScript 接口定义代码]
═══════════════════════════════════════════════════════════════
🚀 **API 函数 (复制下方代码)**
═══════════════════════════════════════════════════════════════
[API 函数代码]
═══════════════════════════════════════════════════════════════
📖 **使用示例 (可选复制)**
═══════════════════════════════════════════════════════════════
[完整使用示例代码]JSON 格式(--json):
{
"success": true,
"interface": {
"id": 4144,
"title": "用户登录",
"method": "POST",
"path": "/api/user/login"
},
"types": [
{
"name": "UserLoginRequest",
"content": "export interface UserLoginRequest { ... }"
}
],
"api": {
"name": "createUserLogin",
"content": "export async function createUserLogin(...) { ... }"
},
"requestConfig": "...",
"usageExample": "..."
}分类批量生成输出
文本格式(默认):
✅ **批量生成完成** - 项目名称
📊 **统计信息:**
- 总接口数: 12
- 总分类数: 1
- 生成类型: 24
- 生成函数: 12
🎯 生成分类: 用户管理 (ID: 305)
═══════════════════════════════════════════════════════════════
📝 **完整代码 (复制下方代码)**
═══════════════════════════════════════════════════════════════
[完整的TypeScript代码供复制]JSON 格式(--json):
{
"success": true,
"project": {
"id": 27,
"name": "项目名称",
"desc": "项目描述"
},
"categories": [...],
"types": [...],
"apis": [...],
"index": "...",
"stats": {
"totalInterfaces": 12,
"totalCategories": 1,
"generatedTypes": 24,
"generatedApis": 12
}
}💡 使用技巧
- 分类模式:适合特定模块的开发,快速复制所需代码
- 单个接口:适合调试和测试特定接口
- 过滤功能:使用过滤参数精确定位需要的接口
- JSON 输出:使用
--json参数获取结构化数据,便于程序化处理或集成到其他工具 - 文件输出:使用
--dir参数将生成的代码直接保存到项目目录,省去手动复制粘贴的步骤
🎬 使用示例
示例 1:单个接口生成(复制模式)
输入:
npx yapi-generator-ts-mcp https://yapi.example.com/project/1/interface/api/100输出: 复制模式,供直接复制使用
示例 2:单个接口生成(文件模式)
输入:
npx yapi-generator-ts-mcp https://yapi.example.com/project/1/interface/api/100 --dir ./src/types输出: 自动生成以下文件到 ./src/types 目录:
用户登录.types.ts- 类型定义(包含 Request、Response 类型)用户登录.api.ts- API 函数(从 .types.ts 导入类型)
文件结构示例:
// 用户登录.types.ts
export interface UserLoginRequest {
username: string;
password: string;
}
export interface UserLoginResponse {
code: number;
data: {
token: string;
user: { id: number; name: string; };
};
}
export interface RequestConfig {
url: string;
method: string;
params?: any;
data?: any;
headers?: Record<string, string>;
}
// 用户登录.api.ts
import { UserLoginRequest, UserLoginResponse, RequestConfig } from './用户登录.types';
// 您需要根据项目实际情况实现这个函数
async function request<T>(config: RequestConfig): Promise<T> {
// TODO: 实现您的请求逻辑
// 例如使用 axios:
// const response = await axios(config);
// return response.data;
throw new Error('请实现 request 函数');
}
export async function createUserLogin(params: UserLoginRequest): Promise<UserLoginResponse> {
return request<UserLoginResponse>({
url: '/api/user/login',
method: 'POST',
params,
});
}示例 3:分类批量生成(复制模式)
输入:
npx yapi-generator-ts-mcp https://yapi.example.com/project/1/interface/api/cat_123输出: 复制模式,只生成指定分类的代码供复制
示例 4:分类批量生成(文件模式)
输入:
npx yapi-generator-ts-mcp https://yapi.example.com/project/1/interface/api/cat_123 --dir ./src/api输出: 自动生成多个文件到 ./src/api 目录,按分类分组:
项目名_用户管理.ts- 用户管理分类的所有接口项目名_商品管理.ts- 商品管理分类的所有接口项目名_index.ts- 索引文件
示例 5:在实际项目中使用生成的代码
实现request函数:
// src/utils/request.ts
import axios from 'axios';
// 复制生成的RequestConfig类型
export interface RequestConfig {
url: string;
method: string;
params?: any;
data?: any;
headers?: Record<string, string>;
}
export async function request<T>(config: RequestConfig): Promise<T> {
const response = await axios({
url: config.url,
method: config.method,
params: config.params,
data: config.data,
headers: {
'Content-Type': 'application/json',
...config.headers,
},
});
return response.data;
}- 在项目中使用:
// src/pages/Login.tsx
import { userManagementApis } from '../yapi-generated';
const handleLogin = async () => {
try {
const response = await userManagementApis.createUserLogin({
username: 'john',
password: '123456',
});
console.log('登录成功:', response.user);
} catch (error) {
console.error('登录失败:', error);
}
};🛠️ 命令行选项
单个接口生成
npx yapi-generator-ts-mcp [选项]
选项:
-u, --url <URL> YAPI 接口页面的完整 URL
-t, --token <TOKEN> 访问令牌 (可选,有默认值)
-j, --json 以 JSON 格式输出结果
-d, --dir <PATH> 输出目录路径(指定后将生成文件到该目录)
-f, --filename <NAME> 自定义文件名(不含扩展名,需配合 --dir 使用)
-h, --help 显示帮助信息
示例:
npx yapi-generator-ts-mcp https://yapi.example.com/project/1/interface/api/100
npx yapi-generator-ts-mcp --url https://yapi.example.com/project/1/interface/api/100 --token abc123
npx yapi-generator-ts-mcp https://yapi.example.com/project/1/interface/api/100 --json
npx yapi-generator-ts-mcp https://yapi.example.com/project/1/interface/api/100 --dir ./types
npx yapi-generator-ts-mcp https://yapi.example.com/project/1/interface/api/100 --dir ./types --filename user-login批量生成(分类)
npx yapi-generator-ts-mcp <PROJECT_URL|CATEGORY_URL> [选项]
选项:
-p, --project <URL> YAPI 分类页面的完整 URL
-c, --category <ID> 分类ID (分类URL会自动解析)
-j, --json 以 JSON 格式输出结果
-d, --dir <PATH> 输出目录路径(指定后将生成文件到该目录)
-f, --filename <NAME> 自定义文件名(不含扩展名,需配合 --dir 使用)
--no-group 不按分类分组
--filter-keyword <TEXT> 关键字过滤
--filter-method <METHOD> HTTP方法过滤
--filter-status <STATUS> 状态过滤
-t, --token <TOKEN> 访问令牌 (可选,有默认值)
-h, --help 显示帮助信息
示例:
# 生成指定分类(输出供复制)
npx yapi-generator-ts-mcp https://yapi.example.com/project/1/interface/api/cat_305
# 带过滤条件
npx yapi-generator-ts-mcp https://yapi.example.com/project/1/interface/api/cat_305 --filter-keyword "用户"
# 以 JSON 格式输出
npx yapi-generator-ts-mcp https://yapi.example.com/project/1/interface/api/cat_305 --json
# 输出到指定目录
npx yapi-generator-ts-mcp https://yapi.example.com/project/1/interface/api/cat_305 --dir ./generated
# 自定义文件名输出
npx yapi-generator-ts-mcp https://yapi.example.com/project/1/interface/api/cat_305 --dir ./generated --filename my-api
# 传统方式生成指定分类
npx yapi-generator-ts-mcp --project https://yapi.example.com/project/1/interface/api/cat_305 --category 305🎯 适用场景
👨💻 个人开发者
- 快速原型:快速生成类型定义,专注业务逻辑
- 模块开发:使用分类模式,快速获取特定模块的代码
- 类型安全:避免手写类型定义的错误
- 提升效率:一键生成,无需重复劳动
👥 团队开发
- 统一标准:确保前后端类型定义一致
- 代码组织:按API分类组织代码结构
- 减少沟通:自动化生成减少接口对接问题
- 代码质量:TypeScript 类型检查提升代码质量
- 协作效率:团队成员可以直接使用生成的代码
🏢 企业项目
- 规模化应用:支持大量接口的批量处理
- 标准化流程:集成到 CI/CD 流程中
- 降低维护成本:自动化减少人工维护
- 灵活过滤:支持多维度过滤满足不同需求
❓ 常见问题
Q: 支持什么样的生成模式?
A:
- 单个接口:生成单个接口的类型定义,输出供复制使用
- 分类批量:生成指定分类的接口,输出供复制使用,按分类分组
Q: 如何使用生成的代码?
A:
- 生成的代码会按分类分组显示,直接复制到项目中即可使用
- 包含完整的类型定义和API函数
- 提供详细的使用示例
Q: 如何在项目中使用生成的代码?
A: 将生成的代码复制到项目中,并实现 request 函数:
// 复制生成的类型和API函数到项目中
const response = await createUserLogin(params);Q: 支持按分类生成吗?
A: 支持!直接使用分类URL即可:
npx yapi-generator-ts-mcp https://yapi.example.com/project/27/interface/api/cat_305Q: 如何过滤特定的接口?
A: 支持多维度过滤:
npx yapi-generator-ts-mcp https://yapi.example.com/project/27/interface/api/cat_305 --filter-keyword "用户" --filter-method "POST"Q: 生成的代码可以直接使用吗?
A: 需要实现 request 函数,生成的代码中有详细说明和示例。
Q: 支持哪些 YAPI 版本?
A: 支持主流的 YAPI 版本,兼容标准的 YAPI API 格式。
Q: 生成的类型定义准确吗?
A: 基于 YAPI 的 JSON Schema 生成,准确性高。建议在使用前检查生成的类型。
Q: 可以自定义 token 吗?
A: 可以,使用 --token 参数指定您的 YAPI 访问令牌。
Q: 在 Cursor 中无法识别工具怎么办?
A: 确保已运行 npx yapi-init 进行初始化配置。
🚀 更新日志
v1.4.7 (最新)
- 🔧 重要改进:
request函数实现现在放在.api.ts中(而非.types.ts) - 🔧
.types.ts现在只包含纯类型定义,不包含任何函数声明 - 🔧
.api.ts包含request函数实现模板,方便用户直接修改 - 📝 改进代码组织,职责更清晰
v1.4.6
- 🆕 新增
--filename/-f参数,支持自定义文件名 - 🔧 优化文件名生成,支持自定义而非强制使用接口中文名
v1.4.5
- 🔧 优化文件生成逻辑,移除重复代码
- 🔧
.api.ts文件现在从.types.ts导入类型,不再重复定义 - 🔧 移除
.example.ts文件生成,简化输出结构 - 🔧 改进文件分离:
.types.ts包含类型定义,.api.ts只包含 API 函数
v1.4.4
- 🆕 新增文件输出支持(
--dir/-d参数) - 🆕 支持将生成的代码直接保存到指定目录
- 🆕 单个接口生成时自动创建
.types.ts和.api.ts文件 - 🆕 批量生成时支持按分类分组输出到多个文件
- 🔧 优化文件名生成逻辑,自动处理特殊字符
v1.4.3
- 🆕 新增 JSON 格式输出支持(
--json/-j参数) - 🆕 支持程序化处理生成结果
- 🔧 优化输出格式,更易于复制使用
v1.3.0
- 🆕 新增批量生成功能
- 🆕 支持按分类生成
- 🆕 支持分类URL直接解析
- 🆕 支持多维度过滤
- 🆕 支持分类分组输出
- 🆕 新增智能URL识别
- 🆕 新增统计信息显示
- 🆕 新增进度显示
- 🔧 优化输出格式
- 🔧 改进错误处理
v1.2.0
- 🔧 优化类型生成逻辑
- 🔧 改进复制体验
- 🔧 增强错误处理
v1.1.0
- 🆕 新增 Cursor MCP 支持
- 🔧 优化命令行界面
- 📖 完善使用文档
v1.0.0
- 🎉 初始发布
- 🔧 单个接口类型生成
- 📖 基础文档
🤝 社区与支持
🌟 推荐给朋友
如果这个工具对您有帮助,请:
- ⭐ 给项目点个 Star
- 📢 推荐给团队同事
- 💡 提供功能建议
- 🐛 报告问题和 Bug
💬 获取帮助
如果遇到问题,请:
- 查看本文档的常见问题部分
- 使用
--help查看详细帮助 - 提交 Issue 寻求帮助
🔮 未来计划
- 🚀 支持更多 YAPI 版本
- 🔧 支持自定义模板
- 🎨 支持自定义输出格式
- 🔍 支持更多过滤条件
- 🌐 支持国际化
让 YAPI 到 TypeScript 的转换变得简单高效! 🚀
