feat: 添加 CORS 支持,处理预检请求并更新 API 响应头

This commit is contained in:
katelya
2025-09-02 17:43:06 +08:00
parent c69e9a380f
commit 1e3467fff2
9 changed files with 132 additions and 23 deletions
+10 -2
View File
@@ -1,23 +1,31 @@
import { NextResponse } from 'next/server';
import { getAvailableApiSites, getCacheTime } from '@/lib/config';
import { addCorsHeaders, handleOptionsRequest } from '@/lib/cors';
export const runtime = 'edge';
// 处理OPTIONS预检请求(OrionTV客户端需要)
export async function OPTIONS() {
return handleOptionsRequest();
}
// OrionTV 兼容接口
export async function GET() {
try {
const apiSites = await getAvailableApiSites();
const cacheTime = await getCacheTime();
return NextResponse.json(apiSites, {
const response = NextResponse.json(apiSites, {
headers: {
'Cache-Control': `public, max-age=${cacheTime}, s-maxage=${cacheTime}`,
'CDN-Cache-Control': `public, s-maxage=${cacheTime}`,
'Vercel-CDN-Cache-Control': `public, s-maxage=${cacheTime}`,
},
});
return addCorsHeaders(response);
} catch (error) {
return NextResponse.json({ error: '获取资源失败' }, { status: 500 });
const response = NextResponse.json({ error: '获取资源失败' }, { status: 500 });
return addCorsHeaders(response);
}
}