test-apifox-endpoints.js
4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env node
/**
* 测试不同的 Apifox API 端点
*/
const fs = require('fs');
const path = require('path');
const https = require('https');
// 加载配置
function loadConfig() {
const envPath = path.join(__dirname, '../.env.apifox');
const env = fs.readFileSync(envPath, 'utf-8');
const config = {};
env.split('\n').forEach(line => {
const [key, ...valueParts] = line.split('=');
const value = valueParts.join('=');
if (key && !key.startsWith('#') && value) {
config[key.trim()] = value.trim();
}
});
return config;
}
// 发送 HTTPS 请求
function httpsRequest(options) {
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
const chunks = [];
res.on('data', chunk => {
chunks.push(chunk);
});
res.on('end', () => {
const raw = Buffer.concat(chunks).toString('utf8').trim();
if (!raw) {
resolve({ statusCode: res.statusCode, data: null, headers: res.headers });
return;
}
try {
const json = JSON.parse(raw.replace(/^\uFEFF/, ''));
resolve({ statusCode: res.statusCode, data: json, headers: res.headers });
} catch (err) {
resolve({ statusCode: res.statusCode, raw, parseError: err.message, headers: res.headers });
}
});
});
req.on('error', reject);
req.end();
});
}
// 测试端点
async function testEndpoint(config, endpointPath, description) {
console.log(`\n测试: ${description}`);
console.log(`端点: ${endpointPath}`);
const options = {
hostname: 'api.apifox.com',
path: endpointPath,
method: 'GET',
headers: {
'Authorization': `Bearer ${config.VITE_APIFOX_TOKEN}`,
'Content-Type': 'application/json'
}
};
try {
const response = await httpsRequest(options);
console.log(`状态码: ${response.statusCode}`);
console.log(`Content-Type: ${response.headers['content-type']}`);
console.log(`Content-Length: ${response.headers['content-length']}`);
if (response.parseError) {
console.log(`❌ JSON 解析失败: ${response.parseError}`);
console.log(`原始响应 (前 200 字符): ${response.raw?.substring(0, 200)}`);
} else if (response.data) {
console.log(`✅ 成功`);
// 显示响应结构
if (Array.isArray(response.data)) {
console.log(` - data 是数组,长度: ${response.data.length}`);
} else if (typeof response.data === 'object') {
console.log(` - data 是对象,键: ${Object.keys(response.data).join(', ')}`);
}
// 如果有接口数据,显示第一个
if (Array.isArray(response.data) && response.data.length > 0) {
console.log(` - 第一个接口:`, JSON.stringify(response.data[0]).substring(0, 150));
}
} else {
console.log(`⚠️ 响应为空`);
}
return response.statusCode === 200 && response.data && (Array.isArray(response.data) ? response.data.length > 0 : true);
} catch (err) {
console.log(`❌ 请求失败: ${err.message}`);
return false;
}
}
// 主函数
async function main() {
const config = loadConfig();
const projectId = config.VITE_APIFOX_PROJECT_ID;
console.log('='.repeat(60));
console.log('测试不同的 Apifox API 端点');
console.log('='.repeat(60));
// 尝试不同的端点
const endpoints = [
{ path: `/api/v1/projects/${projectId}/apis?pageSize=10`, desc: '当前使用的端点 (/apis)' },
{ path: `/api/v1/projects/${projectId}/interfaces?pageSize=10`, desc: '尝试 /interfaces' },
{ path: `/v1/projects/${projectId}/apis?pageSize=10`, desc: '不带 /api 前缀' },
{ path: `/api/v1/projects/${projectId}/api-lists?pageSize=10`, desc: '尝试 /api-lists' },
{ path: `/api/v1/projects/${projectId}/endpoints?pageSize=10`, desc: '尝试 /endpoints' },
];
let successCount = 0;
for (const endpoint of endpoints) {
const success = await testEndpoint(config, endpoint.path, endpoint.desc);
if (success) successCount++;
}
console.log('\n' + '='.repeat(60));
console.log(`测试完成!成功: ${successCount}/${endpoints.length}`);
console.log('='.repeat(60));
}
main();