2025-09-16 17:19:58 +08:00

34 lines
1.2 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import { getStaticArticles, getStaticCategories } from '../../../lib/static-data';
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const locale = searchParams.get('locale') || 'zh-CN';
const category = searchParams.get('category') || 'all';
// 映射locale格式
const mappedLocale = locale === 'zh' ? 'zh-CN' : locale as 'zh-CN' | 'zh-TW' | 'en';
// 获取文章数据
const allArticles = getStaticArticles(mappedLocale);
const categories = getStaticCategories(mappedLocale);
// 根据分类过滤文章
const filteredArticles = category === 'all'
? allArticles
: allArticles.filter(article => article.metadata.category === category);
return NextResponse.json({
articles: filteredArticles,
categories: categories,
total: filteredArticles.length
});
} catch (error) {
console.error('Error fetching articles:', error);
return NextResponse.json(
{ error: 'Failed to fetch articles' },
{ status: 500 }
);
}
}