53 lines
1.6 KiB
TypeScript
53 lines
1.6 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
|
|
const ANALYTICS_BACKEND = process.env.ANALYTICS_BACKEND_URL || 'http://127.0.0.1:8000';
|
|
|
|
function getTaipeiDate(): string {
|
|
const parts = new Intl.DateTimeFormat('en-US', {
|
|
timeZone: 'Asia/Taipei',
|
|
year: 'numeric',
|
|
month: '2-digit',
|
|
day: '2-digit',
|
|
}).formatToParts(new Date());
|
|
|
|
const year = parts.find((part) => part.type === 'year')?.value;
|
|
const month = parts.find((part) => part.type === 'month')?.value;
|
|
const day = parts.find((part) => part.type === 'day')?.value;
|
|
|
|
return `${year}-${month}-${day}`;
|
|
}
|
|
|
|
export async function GET(request: Request) {
|
|
const { searchParams } = new URL(request.url);
|
|
const targetDate = searchParams.get('date') || getTaipeiDate();
|
|
|
|
if (!/^\d{4}-\d{2}-\d{2}$/.test(targetDate)) {
|
|
return NextResponse.json({ message: '日期格式必須為 YYYY-MM-DD' }, { status: 400 });
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(`${ANALYTICS_BACKEND}/analytics/daily-card/${encodeURIComponent(targetDate)}`, {
|
|
method: 'GET',
|
|
cache: 'no-store',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const message = await response.text();
|
|
return NextResponse.json({ message }, { status: response.status });
|
|
}
|
|
|
|
const data = (await response.json()) as Record<string, unknown>;
|
|
return NextResponse.json({
|
|
generated_at: new Date().toISOString(),
|
|
target_date: targetDate,
|
|
...data,
|
|
});
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : '每日作戰室服務暫時無法連線';
|
|
return NextResponse.json({ message }, { status: 502 });
|
|
}
|
|
}
|