hookehuyr

feat: 添加 Apifox Skill 连接测试脚本

新增测试脚本以验证 Apifox Skill 的配置和连接功能。脚本会测试 Token 和项目 ID 的有效性,获取项目信息并列出接口列表,提供清晰的错误诊断信息以帮助用户排查配置问题。
1 +#!/usr/bin/env node
2 +
3 +/**
4 + * 测试 Apifox Skill 连接
5 + */
6 +
7 +const path = require('path');
8 +const ApifoxClient = require('/Users/huyirui/.claude/skills/apifox/apifox-client.js');
9 +
10 +// 测试配置
11 +const testConfig = {
12 + VITE_APIFOX_TOKEN: 'APS-jkT1Q61MCKgzgvfCL2euIR2TcgKsnSyc',
13 + VITE_APIFOX_PROJECT_ID: '6084040',
14 + VITE_APIFOX_CACHE_TIME: 3600
15 +};
16 +
17 +console.log('🧪 Apifox Skill 连接测试');
18 +console.log('='.repeat(50));
19 +console.log('');
20 +
21 +// 显示配置信息
22 +console.log('📝 测试配置:');
23 +console.log(` Token: ${testConfig.VITE_APIFOX_TOKEN.substring(0, 20)}...`);
24 +console.log(` 项目 ID: ${testConfig.VITE_APIFOX_PROJECT_ID}`);
25 +console.log('');
26 +
27 +async function testConnection() {
28 + const client = new ApifoxClient(testConfig);
29 +
30 + try {
31 + // 测试 1: 获取项目信息
32 + console.log('📊 测试 1/3: 获取项目信息...');
33 + const project = await client.getProjectInfo();
34 +
35 + console.log('✅ 项目信息获取成功');
36 + console.log(` 项目名称: ${project.name || '未设置'}`);
37 + console.log(` 项目 ID: ${project.id}`);
38 + console.log(` 描述: ${project.description || '无'}`);
39 + console.log('');
40 +
41 + // 测试 2: 获取接口列表
42 + console.log('📋 测试 2/3: 获取接口列表...');
43 + const apis = await client.listAllApis();
44 +
45 + console.log(`✅ 接口列表获取成功,共 ${apis.length} 个接口`);
46 + console.log('');
47 +
48 + // 测试 3: 显示前 5 个接口
49 + console.log('🔍 测试 3/3: 显示前 5 个接口...');
50 + const sampleApis = apis.slice(0, 5);
51 +
52 + sampleApis.forEach((api, index) => {
53 + const method = (api.attributes?.method || 'GET').toUpperCase();
54 + const path = api.attributes?.path || '/';
55 + const name = api.attributes?.name || '未命名';
56 +
57 + console.log(` ${index + 1}. ${method} ${path}`);
58 + console.log(` ${name}`);
59 + });
60 +
61 + if (apis.length > 5) {
62 + console.log(` ... 还有 ${apis.length - 5} 个接口`);
63 + }
64 +
65 + console.log('');
66 + console.log('='.repeat(50));
67 + console.log('✅ 所有测试通过!');
68 + console.log('');
69 + console.log('🎉 Token 和 Project ID 验证成功!');
70 + console.log('💡 你现在可以使用 Apifox Skill 了');
71 +
72 + } catch (error) {
73 + console.log('');
74 + console.log('❌ 测试失败');
75 + console.log('');
76 +
77 + if (error.message.includes('401')) {
78 + console.log('错误原因: HTTP 401 - Token 无效或已过期');
79 + console.log('');
80 + console.log('解决方案:');
81 + console.log(' 1. 检查 Token 是否正确');
82 + console.log(' 2. 在 Apifox 中重新生成 Token');
83 + console.log(' 3. 更新 .env.apifox 文件');
84 + } else if (error.message.includes('403') || error.message.includes('404')) {
85 + console.log('错误原因: HTTP 403/404 - 项目不存在或无权限');
86 + console.log('');
87 + console.log('解决方案:');
88 + console.log(' 1. 检查项目 ID 是否正确');
89 + console.log(' 2. 确认账号有该项目的访问权限');
90 + console.log(' 3. 在 Apifox 中检查项目设置');
91 + } else {
92 + console.log('错误原因:', error.message);
93 + console.log('');
94 + console.log('详细错误:');
95 + console.error(error);
96 + }
97 +
98 + process.exit(1);
99 + }
100 +}
101 +
102 +// 运行测试
103 +testConnection().catch(err => {
104 + console.error('');
105 + console.error('❌ 测试失败:', err.message);
106 + console.error('');
107 + process.exit(1);
108 +});