()
| 2 | import { GitHubAPI } from '@/app/lib/github'; |
| 3 | |
| 4 | export async function GET() { |
| 5 | try { |
| 6 | // 获取所有文章 |
| 7 | const allArticles = await GitHubAPI.getAllArticles(); |
| 8 | |
| 9 | // 确保 allArticles 是数组 |
| 10 | if (!Array.isArray(allArticles)) { |
| 11 | console.error('GitHub API returned non-array data:', allArticles); |
| 12 | return NextResponse.json(['全部'], { status: 200 }); |
| 13 | } |
| 14 | |
| 15 | // 提取所有分类 |
| 16 | const categories = new Set<string>(); |
| 17 | allArticles.forEach(article => { |
| 18 | if (article.category) { |
| 19 | categories.add(article.category); |
| 20 | } |
| 21 | }); |
| 22 | |
| 23 | // 转换为数组并排序 |
| 24 | const categoryList = Array.from(categories).sort(); |
| 25 | |
| 26 | // 添加"全部"选项 |
| 27 | categoryList.unshift('全部'); |
| 28 | |
| 29 | return NextResponse.json(categoryList); |
| 30 | } catch (error) { |
| 31 | console.error('Error fetching categories:', error); |
| 32 | // 返回默认分类列表,避免前端报错 |
| 33 | return NextResponse.json(['全部'], { status: 200 }); |
| 34 | } |
| 35 | } |
nothing calls this directly
no test coverage detected