test-apifox-export.js
4.05 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
#!/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, headers: res.headers });
}
});
});
req.on('error', reject);
req.end();
});
}
// 主函数
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));
// 尝试导出 OpenAPI 格式
const exportEndpoints = [
{ path: `/api/v1/projects/${projectId}/export-openapi`, desc: '导出 OpenAPI' },
{ path: `/api/v1/projects/${projectId}/export-openapi/3.0.0`, desc: '导出 OpenAPI 3.0' },
{ path: `/api/v1/projects/${projectId}/export-openapi/3.0.0/json`, desc: '导出 OpenAPI 3.0 JSON' },
];
for (const endpoint of exportEndpoints) {
console.log(`\n测试: ${endpoint.desc}`);
console.log(`端点: ${endpoint.path}`);
const options = {
hostname: 'api.apifox.com',
path: endpoint.path,
method: 'GET',
headers: {
'Authorization': `Bearer ${config.VITE_APIFOX_TOKEN}`,
'Content-Type': 'application/json',
'X-Apifox-Version': '2024-06-14' // 尝试添加版本头
}
};
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.statusCode === 200 && response.raw) {
console.log(`✅ 成功获取数据 (${response.raw.length} 字符)`);
// 尝试解析为 JSON
try {
const json = JSON.parse(response.raw);
console.log(` - JSON 解析成功`);
console.log(` - 顶层键: ${Object.keys(json).join(', ')}`);
// 检查是否有接口数据
if (json.paths) {
const pathCount = Object.keys(json.paths).length;
console.log(` - OpenAPI paths 数量: ${pathCount}`);
if (pathCount > 0) {
console.log(` ✓ 找到接口数据!`);
// 保存到文件
const outputPath = path.join(__dirname, `../test-openapi-${Date.now()}.json`);
fs.writeFileSync(outputPath, JSON.stringify(json, null, 2), 'utf-8');
console.log(` - 已保存到: ${outputPath}`);
return; // 找到数据就退出
}
}
} catch (err) {
console.log(` - JSON 解析失败,可能是其他格式`);
}
} else if (response.data) {
console.log(`✅ 响应包含 data 字段`);
console.log(` - data 键: ${Object.keys(response.data).join(', ')}`);
} else {
console.log(`⚠️ 响应为空`);
}
} catch (err) {
console.log(`❌ 请求失败: ${err.message}`);
}
}
console.log('\n' + '='.repeat(60));
}
main();