52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
export const dynamic = 'force-dynamic';
|
|
import { NextResponse } from "next/server";
|
|
|
|
export async function GET() {
|
|
try {
|
|
const res = await fetch(
|
|
"https://wttr.in/Saint+Petersburg?format=j1",
|
|
{
|
|
next: { revalidate: 600 },
|
|
headers: { "User-Agent": "SmartHomeDashboard/1.0" },
|
|
}
|
|
);
|
|
if (!res.ok) throw new Error("Weather fetch failed");
|
|
const data = await res.json();
|
|
|
|
const current = data.current_condition[0];
|
|
const days = data.weather.slice(0, 3).map((day: any) => {
|
|
const desc = day.hourly[4]?.weatherDesc?.[0]?.value || "";
|
|
return {
|
|
date: day.date,
|
|
maxTemp: day.maxtempC,
|
|
minTemp: day.mintempC,
|
|
desc,
|
|
weatherCode: day.hourly[4]?.weatherCode || "113",
|
|
};
|
|
});
|
|
|
|
return NextResponse.json({
|
|
temp: current.temp_C,
|
|
feelsLike: current.FeelsLikeC,
|
|
humidity: current.humidity,
|
|
desc: current.weatherDesc[0]?.value || "",
|
|
weatherCode: current.weatherCode,
|
|
windSpeed: current.windspeedKmph,
|
|
forecast: days,
|
|
});
|
|
} catch (e) {
|
|
return NextResponse.json(
|
|
{
|
|
temp: "—",
|
|
feelsLike: "—",
|
|
humidity: "—",
|
|
desc: "Нет данных",
|
|
weatherCode: "113",
|
|
windSpeed: "—",
|
|
forecast: [],
|
|
},
|
|
{ status: 200 }
|
|
);
|
|
}
|
|
}
|