yapi_interface_add
Add new API interfaces to YAPI documentation by specifying path, method, parameters, and response details for automated API management.
Instructions
新增接口
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | No | YAPI 接口页面 URL(可选),如 https://yapi.xxx.com/project/1009/interface/api/108375,会自动解析出项目 ID、接口 ID 等参数 | |
| project | No | 项目 ID(可选,不传则使用默认项目) | |
| catid | Yes | 接口分类 ID | |
| title | Yes | 接口名称 | |
| path | Yes | 接口路径,如:/api/user | |
| method | Yes | 请求方法,如:GET、POST、PUT、DELETE | |
| desc | No | 接口描述(可选) | |
| status | No | 接口状态:done(已完成)、undone(未完成) | |
| req_params | No | 路径参数 | |
| req_query | No | 查询参数 | |
| req_headers | No | 请求头 | |
| req_body_type | No | 请求体类型:form、json、file、raw | |
| req_body_form | No | 表单类型的请求体 | |
| req_body_other | No | 其他类型的请求体(JSON 字符串) | |
| res_body_type | No | 返回数据类型:json、raw | |
| res_body | No | 返回数据(JSON 字符串) |
Implementation Reference
- src/index.ts:287-391 (schema)Tool registration and input schema definition for 'yapi_interface_add' - defines the tool name, description, and all input parameters including catid, title, path, method, and optional fields like desc, status, req_params, req_query, req_headers, request body types, and response body configuration.{ name: "yapi_interface_add", description: "新增接口", inputSchema: { type: "object", properties: { url: urlParamDef, project: { type: ["number", "string"], description: projectParamDesc, }, catid: { type: "number", description: "接口分类 ID", }, title: { type: "string", description: "接口名称", }, path: { type: "string", description: "接口路径,如:/api/user", }, method: { type: "string", description: "请求方法,如:GET、POST、PUT、DELETE", }, desc: { type: "string", description: "接口描述(可选)", }, status: { type: "string", description: "接口状态:done(已完成)、undone(未完成)", enum: ["done", "undone"], }, req_params: { type: "array", description: "路径参数", items: { type: "object", properties: { name: { type: "string" }, desc: { type: "string" }, }, }, }, req_query: { type: "array", description: "查询参数", items: { type: "object", properties: { name: { type: "string" }, desc: { type: "string" }, required: { type: "string" }, }, }, }, req_headers: { type: "array", description: "请求头", items: { type: "object", properties: { name: { type: "string" }, value: { type: "string" }, }, }, }, req_body_type: { type: "string", description: "请求体类型:form、json、file、raw", enum: ["form", "json", "file", "raw"], }, req_body_form: { type: "array", description: "表单类型的请求体", items: { type: "object", properties: { name: { type: "string" }, type: { type: "string" }, desc: { type: "string" }, required: { type: "string" }, }, }, }, req_body_other: { type: "string", description: "其他类型的请求体(JSON 字符串)", }, res_body_type: { type: "string", description: "返回数据类型:json、raw", enum: ["json", "raw"], }, res_body: { type: "string", description: "返回数据(JSON 字符串)", }, }, required: ["catid", "title", "path", "method"], }, },
- src/index.ts:711-720 (handler)Handler implementation for 'yapi_interface_add' - extracts project parameter and passes remaining arguments to YAPI API endpoint '/api/interface/add' using POST method via yapiRequestWithFallback.case "yapi_interface_add": { const { project: _, ...restArgs } = args; result = await yapiRequestWithFallback( "/api/interface/add", "POST", () => restArgs, args.project ); break; }
- src/index.ts:176-215 (helper)Helper function yapiRequestWithFallback - wraps YAPI API calls with automatic token fallback; tries specified project first, or iterates through all configured projects until a valid token is found.async function yapiRequestWithFallback( endpoint: string, method: "GET" | "POST", buildParams: (project: ProjectConfig) => Record<string, unknown> | undefined, specifiedProject?: unknown ): Promise<unknown> { // 如果明确指定了项目,直接使用该项目 if (specifiedProject !== undefined && specifiedProject !== null && specifiedProject !== "") { const project = getProjectConfig(specifiedProject); return yapiRequest(endpoint, method, buildParams(project), project.token); } if (projects.length === 0) { throw new Error( "未配置任何项目,请设置 YAPI_PROJECTS 环境变量,格式: projectId1:token1,projectId2:token2" ); } // 依次尝试所有项目的 token for (const project of projects) { try { const result = await yapiRequest( endpoint, method, buildParams(project), project.token ); if (!isTokenError(result)) { return result; } } catch { // 请求失败,尝试下一个项目 continue; } } throw new Error( `所有已配置项目的 token 均无法访问此接口,已尝试项目: ${projects.map((p) => p.id).join(", ")}` ); }
- src/index.ts:87-134 (helper)Helper function yapiRequest - core HTTP request wrapper that handles GET and POST requests to YAPI API endpoints, adds token parameter, sets appropriate headers, and returns parsed JSON response.async function yapiRequest( endpoint: string, method: "GET" | "POST" = "GET", params?: Record<string, unknown>, token?: string ): Promise<unknown> { if (!YAPI_BASE_URL) { throw new Error("YAPI_BASE_URL 环境变量未配置"); } if (!token) { throw new Error("token 未提供"); } const url = new URL(endpoint, YAPI_BASE_URL); const options: RequestInit = { method, headers: { "Content-Type": "application/json", }, }; if (method === "GET") { // GET 请求时将参数添加到 URL if (params) { Object.entries(params).forEach(([key, value]) => { if (value !== undefined && value !== null) { url.searchParams.append(key, String(value)); } }); } url.searchParams.append("token", token); } else { // POST 请求时将参数放在 body 中 url.searchParams.append("token", token); if (params) { options.body = JSON.stringify(params); } } const response = await fetch(url.toString(), options); if (!response.ok) { throw new Error(`YAPI 请求失败: ${response.status} ${response.statusText}`); } return response.json(); }