diff --git a/apifox.js b/apifox.js index 8be74c2..24fd92f 100644 --- a/apifox.js +++ b/apifox.js @@ -353,12 +353,56 @@ export class ApifoxService { return resolved; } + /** + * 从 apiDetail 中提取请求参数的 schema + * GET 请求从 parameters.query 中提取 + * POST/PUT/PATCH 请求从 requestBody.jsonSchema 中提取 + * @param {Object} apiDetail - API 详情对象 + * @param {Object} context - 包含 projectId 和 allSchemas 的上下文 + * @returns {Promise} 请求参数的 schema + */ + async extractRequestSchema(apiDetail, context) { + const method = (apiDetail.method || '').toLowerCase(); + + // GET 请求:从 parameters.query 中提取参数定义 + if (method === 'get') { + const queryParams = apiDetail.parameters?.query; + if (!queryParams || queryParams.length === 0) { + return null; + } + + // 将 query 参数转换为 JSON Schema 格式 + const properties = {}; + const required = []; + + for (const param of queryParams) { + properties[param.name] = param.schema || { type: param.type || 'string' }; + if (param.required) { + required.push(param.name); + } + } + + return { + type: 'object', + properties, + required: required.length > 0 ? required : undefined + }; + } + + // POST/PUT/PATCH 请求:从 requestBody.jsonSchema 中提取 + const requestSchemaId = this.getSchemaRefId(apiDetail.requestBody?.jsonSchema); + if (requestSchemaId) { + return await this.getSchemaById(requestSchemaId, context); + } + + return null; + } + async getApiDetailResp(apiDetail, projectId) { const pid = projectId || this.defaultProjectId; const dataSchema = await this.getDataSchema(pid); const context = { projectId: pid, allSchemas: dataSchema }; - const requestSchemaId = this.getSchemaRefId(apiDetail.requestBody.jsonSchema); const responseSchema = Array.isArray(apiDetail.responses) ? apiDetail.responses[0].jsonSchema : apiDetail.responses.jsonSchema; const responseSchemaId = this.getSchemaRefId(responseSchema); @@ -402,7 +446,7 @@ export class ApifoxService { name: apiDetail.name, method: apiDetail.method, url: apiDetail.url, - requestSchema: await this.getSchemaById(requestSchemaId, context), + requestSchema: await this.extractRequestSchema(apiDetail, context), responseSchema: (responseDataSchema && responseDataSchema.type === 'array') ? { type: 'array', items: respDataSchemaId ? await this.getSchemaById(respDataSchemaId, context) : null, diff --git a/index.js b/index.js index 65ceed3..0652e83 100644 --- a/index.js +++ b/index.js @@ -83,7 +83,7 @@ server.registerTool( { description: "根据接口目录获取Apifox目录下所有接口定义数据", inputSchema: z.object({ - folderName: z.string().describe("接口路径"), + folderName: z.string().describe("目录路径"), projectId: z.number().optional().describe("Apifox项目ID"), }), },