feat(apifox): 增加提取请求参数 schema 的方法
- 实现 extractRequestSchema 方法,根据请求方法提取相应的参数 schema - GET 请求从 parameters.query 中生成 JSON Schema - POST/PUT/PATCH 请求从 requestBody.jsonSchema 解析 schema 引用并获取完整定义 - 在 getApiDetailResp 中使用 extractRequestSchema 替代原有逻辑 - 修改接口目录描述,从“接口路径”更新为“目录路径”
This commit is contained in:
1 parent
dd1d86706c
commit
00c105c796
2 files changed
+47
-3
No files matched your search
@@ -353,12 +353,56 @@ export class ApifoxService {
|
|||||||
return resolved;
|
return resolved;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从 apiDetail 中提取请求参数的 schema
|
||||||
|
* GET 请求从 parameters.query 中提取
|
||||||
|
* POST/PUT/PATCH 请求从 requestBody.jsonSchema 中提取
|
||||||
|
* @param {Object} apiDetail - API 详情对象
|
||||||
|
* @param {Object} context - 包含 projectId 和 allSchemas 的上下文
|
||||||
|
* @returns {Promise<Object|null>} 请求参数的 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) {
|
async getApiDetailResp(apiDetail, projectId) {
|
||||||
const pid = projectId || this.defaultProjectId;
|
const pid = projectId || this.defaultProjectId;
|
||||||
const dataSchema = await this.getDataSchema(pid);
|
const dataSchema = await this.getDataSchema(pid);
|
||||||
const context = { projectId: pid, allSchemas: dataSchema };
|
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 responseSchema = Array.isArray(apiDetail.responses) ? apiDetail.responses[0].jsonSchema : apiDetail.responses.jsonSchema;
|
||||||
const responseSchemaId = this.getSchemaRefId(responseSchema);
|
const responseSchemaId = this.getSchemaRefId(responseSchema);
|
||||||
|
|
||||||
@@ -402,7 +446,7 @@ export class ApifoxService {
|
|||||||
name: apiDetail.name,
|
name: apiDetail.name,
|
||||||
method: apiDetail.method,
|
method: apiDetail.method,
|
||||||
url: apiDetail.url,
|
url: apiDetail.url,
|
||||||
requestSchema: await this.getSchemaById(requestSchemaId, context),
|
requestSchema: await this.extractRequestSchema(apiDetail, context),
|
||||||
responseSchema: (responseDataSchema && responseDataSchema.type === 'array') ? {
|
responseSchema: (responseDataSchema && responseDataSchema.type === 'array') ? {
|
||||||
type: 'array',
|
type: 'array',
|
||||||
items: respDataSchemaId ? await this.getSchemaById(respDataSchemaId, context) : null,
|
items: respDataSchemaId ? await this.getSchemaById(respDataSchemaId, context) : null,
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ server.registerTool(
|
|||||||
{
|
{
|
||||||
description: "根据接口目录获取Apifox目录下所有接口定义数据",
|
description: "根据接口目录获取Apifox目录下所有接口定义数据",
|
||||||
inputSchema: z.object({
|
inputSchema: z.object({
|
||||||
folderName: z.string().describe("接口路径"),
|
folderName: z.string().describe("目录路径"),
|
||||||
projectId: z.number().optional().describe("Apifox项目ID"),
|
projectId: z.number().optional().describe("Apifox项目ID"),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in new issue
Block a user