'use client'; import React, { useState } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card'; const SimpleAPITestPage: React.FC = () => { const [response, setResponse] = useState(''); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); const [apiEndpoint, setApiEndpoint] = useState('textures'); // 默认测试textures端点 const [userId, setUserId] = useState('1'); // 默认用户ID const [textureName, setTextureName] = useState(''); const [profileId, setProfileId] = useState('1'); // 默认角色ID // 执行API测试 const executeAPITest = async () => { try { setLoading(true); setError(''); setResponse(''); let url = ''; // 根据选择的端点构建URL switch (apiEndpoint) { case 'user-profiles': url = `/api/user-profiles?userId=${userId}`; break; case 'profile': url = `/api/profile?profileId=${profileId}`; break; case 'profile-props': url = `/api/profile-props?profileId=${profileId}`; break; case 'textures': url = textureName ? `/api/textures?name=${encodeURIComponent(textureName)}` : '/api/textures'; break; default: throw new Error('未知的API端点'); } console.log(`正在请求: ${url}`); const res = await fetch(url, { method: 'GET', headers: { 'Content-Type': 'application/json', }, }); if (!res.ok) { throw new Error(`HTTP错误! 状态码: ${res.status}`); } const data = await res.json(); setResponse(JSON.stringify(data, null, 2)); } catch (err) { setError(`请求失败: ${err instanceof Error ? err.message : String(err)}`); } finally { setLoading(false); } }; // 渲染参数输入表单 const renderParamsForm = () => { switch (apiEndpoint) { case 'user-profiles': return (
setUserId(e.target.value)} placeholder="输入用户ID" />
); case 'profile': case 'profile-props': return (
setProfileId(e.target.value)} placeholder="输入角色ID" />
); case 'textures': return (
setTextureName(e.target.value)} placeholder="输入材质名称进行搜索" />
); default: return null; } }; return (

简易API测试页面

测试后端API连接 选择要测试的API端点,输入必要的参数,然后点击"执行测试"按钮
{/* API端点选择 */}
{/* 参数输入表单 */} {renderParamsForm()} {/* 执行按钮 */} {/* 结果显示区域 */} {(response || error) && (
{error ? (
{error}
) : (
                                        {response}
                                    
)}
)}

测试结果将显示在上方区域

); }; export default SimpleAPITestPage;