feat: add global weather support
All checks were successful
CI / Lint, typecheck and build (push) Successful in 9m54s
All checks were successful
CI / Lint, typecheck and build (push) Successful in 9m54s
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import type { DailyForecast, ForecastSource, HourlyForecast, WeatherForecast } from "@/types/forecast";
|
||||
import type { WeatherRegion } from "@/types/weather-region";
|
||||
import { getWeatherCapabilities } from "@/types/weather-region";
|
||||
|
||||
function readNumber(value: unknown) {
|
||||
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
||||
@@ -59,18 +61,22 @@ function normalizeForecast(raw: Partial<WeatherForecast>): WeatherForecast {
|
||||
const latitude = Number(raw.latitude);
|
||||
const longitude = Number(raw.longitude);
|
||||
if (!Number.isFinite(latitude) || !Number.isFinite(longitude)) throw new Error("Forecast service returned invalid coordinates.");
|
||||
const region: WeatherRegion = raw.region === "GLOBAL" ? "GLOBAL" : "PL";
|
||||
return {
|
||||
latitude,
|
||||
longitude,
|
||||
region,
|
||||
timezone: typeof raw.timezone === "string" ? raw.timezone : "Europe/Warsaw",
|
||||
hourly: normalizeHourlyForecast(raw.hourly),
|
||||
daily: normalizeDailyForecast(raw.daily),
|
||||
sources: normalizeSources(raw.sources),
|
||||
capabilities: raw.capabilities ?? getWeatherCapabilities(region),
|
||||
unavailableReasons: Array.isArray(raw.unavailableReasons) ? raw.unavailableReasons.filter((value): value is string => typeof value === "string") : [],
|
||||
};
|
||||
}
|
||||
|
||||
export async function fetchForecast(latitude: number, longitude: number, signal?: AbortSignal) {
|
||||
const params = new URLSearchParams({ latitude: String(latitude), longitude: String(longitude) });
|
||||
export async function fetchForecast(latitude: number, longitude: number, region: WeatherRegion, signal?: AbortSignal) {
|
||||
const params = new URLSearchParams({ latitude: String(latitude), longitude: String(longitude), region });
|
||||
const response = await fetch(`/api/forecast?${params}`, { signal });
|
||||
if (!response.ok) throw new Error("Unable to load forecast.");
|
||||
return normalizeForecast(await response.json() as Partial<WeatherForecast>);
|
||||
|
||||
Reference in New Issue
Block a user