()
| 27 | } |
| 28 | |
| 29 | export default function TestAPIPage() { |
| 30 | const [articles, setArticles] = useState<ArticlesResponse | null>(null); |
| 31 | const [categories, setCategories] = useState<string[] | null>(null); |
| 32 | const [loading, setLoading] = useState(true); |
| 33 | const [error, setError] = useState<string | null>(null); |
| 34 | |
| 35 | useEffect(() => { |
| 36 | const testAPIs = async () => { |
| 37 | try { |
| 38 | setLoading(true); |
| 39 | setError(null); |
| 40 | |
| 41 | // 测试文章 API |
| 42 | const articlesRes = await fetch('/api/articles'); |
| 43 | const articlesData = await articlesRes.json(); |
| 44 | setArticles(articlesData); |
| 45 | |
| 46 | // 测试分类 API |
| 47 | const categoriesRes = await fetch('/api/categories'); |
| 48 | const categoriesData = await categoriesRes.json(); |
| 49 | setCategories(categoriesData); |
| 50 | |
| 51 | } catch (err) { |
| 52 | setError(err instanceof Error ? err.message : '未知错误'); |
| 53 | } finally { |
| 54 | setLoading(false); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | testAPIs(); |
| 59 | }, []); |
| 60 | |
| 61 | if (loading) { |
| 62 | return ( |
| 63 | <div className="min-h-screen flex items-center justify-center"> |
| 64 | <div className="animate-spin rounded-full h-32 w-32 border-b-2 border-primary-600"></div> |
| 65 | </div> |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | return ( |
| 70 | <div className="min-h-screen bg-gray-50 py-8"> |
| 71 | <div className="max-w-4xl mx-auto px-4"> |
| 72 | <h1 className="text-3xl font-bold text-gray-900 mb-8">API 测试页面</h1> |
| 73 | |
| 74 | {error && ( |
| 75 | <div className="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-6"> |
| 76 | <strong>错误:</strong> {error} |
| 77 | </div> |
| 78 | )} |
| 79 | |
| 80 | <div className="grid grid-cols-1 lg:grid-cols-2 gap-8"> |
| 81 | {/* 文章 API 测试 */} |
| 82 | <div className="bg-white rounded-lg shadow-lg p-6"> |
| 83 | <h2 className="text-xl font-semibold text-gray-800 mb-4">文章 API 测试</h2> |
| 84 | {articles ? ( |
| 85 | <div> |
| 86 | <div className="mb-4"> |
nothing calls this directly
no test coverage detected