Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/app/[locale]/weather/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TempUnit, WindUnit } from "@/lib/wmo";
import { forecasts, present, TempUnit, WindUnit } from "@/lib/wmo";
import type { AppLocale } from "@/i18n/routing";
import { getCachedCity, getCachedForecasts, getCachedPresent } from "@/lib/wmo-cached";
import { getCachedCity } from "@/lib/wmo-cached";
import { Metadata } from "next";
import WeatherClassic from "@/components/weather/WeatherClassic";
import WeatherMasthead from "@/components/weather/WeatherMasthead";
Expand Down Expand Up @@ -114,37 +114,37 @@ export default async function Page(props: {
style === "classic" ? (
<WeatherClassic
city={city}
weather={await getCachedPresent(params.id, locale, unit, wind)}
weather={await present(params.id, locale, unit, wind)}
style={style}
/>
) : style === "masthead" ? (
<WeatherMasthead
city={city}
weather={await getCachedPresent(params.id, locale, unit, wind)}
weather={await present(params.id, locale, unit, wind)}
style={style}
/>
) : style === "range-bars" ? (
<WeatherRangeBars
city={city}
weather={await getCachedPresent(params.id, locale, unit, wind)}
weather={await present(params.id, locale, unit, wind)}
style={style}
/>
) : style === "capsule" ? (
<WeatherCapsule
city={city}
weather={await getCachedPresent(params.id, locale, unit, wind)}
weather={await present(params.id, locale, unit, wind)}
style={style}
/>
) : style === "beacon" ? (
<WeatherBeacon
city={city}
weather={await getCachedPresent(params.id, locale, unit, wind)}
weather={await present(params.id, locale, unit, wind)}
style={style}
/>
) : (
<WeatherPrecision
city={city}
weather={await getCachedPresent(params.id, locale, unit, wind)}
weather={await present(params.id, locale, unit, wind)}
style={style}
/>
)
Expand All @@ -154,47 +154,47 @@ export default async function Page(props: {
style === "classic" ? (
<ForecastClassic
locale={locale}
weather={await getCachedForecasts(params.id, locale, unit, days)}
weather={await forecasts(params.id, locale, unit, days)}
style={style}
showWeekday={showWeekday}
showDate={showDate}
/>
) : style === "masthead" ? (
<ForecastMasthead
locale={locale}
weather={await getCachedForecasts(params.id, locale, unit, days)}
weather={await forecasts(params.id, locale, unit, days)}
style={style}
showWeekday={showWeekday}
showDate={showDate}
/>
) : style === "range-bars" ? (
<ForecastRangeBars
locale={locale}
weather={await getCachedForecasts(params.id, locale, unit, days)}
weather={await forecasts(params.id, locale, unit, days)}
style={style}
showWeekday={showWeekday}
showDate={showDate}
/>
) : style === "capsule" ? (
<ForecastCapsule
locale={locale}
weather={await getCachedForecasts(params.id, locale, unit, days)}
weather={await forecasts(params.id, locale, unit, days)}
style={style}
showWeekday={showWeekday}
showDate={showDate}
/>
) : style === "beacon" ? (
<ForecastBeacon
locale={locale}
weather={await getCachedForecasts(params.id, locale, unit, days)}
weather={await forecasts(params.id, locale, unit, days)}
style={style}
showWeekday={showWeekday}
showDate={showDate}
/>
) : (
<ForecastPrecision
locale={locale}
weather={await getCachedForecasts(params.id, locale, unit, days)}
weather={await forecasts(params.id, locale, unit, days)}
style={style}
showWeekday={showWeekday}
showDate={showDate}
Expand Down
8 changes: 1 addition & 7 deletions src/lib/wmo-cached.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { unstable_cache } from "next/cache";
import type { AppLocale } from "@/i18n/routing";
import { city, countries, forecasts, present } from "@/lib/wmo";
import { city, countries } from "@/lib/wmo";

/** Next-runtime caching boundary for the pure `@/lib/wmo` module. */

Expand All @@ -13,9 +13,3 @@ export const getCachedCity = unstable_cache(
["wmo-city"],
{ revalidate: 86400 },
);

export const getCachedPresent = unstable_cache(present, ["wmo-present"], { revalidate: 900 });

export const getCachedForecasts = unstable_cache(forecasts, ["wmo-forecasts"], {
revalidate: 3600,
});
75 changes: 37 additions & 38 deletions src/lib/wmo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,29 +172,29 @@ export type WmoCountryResponse = {
>;
};

/**
* Represents the response structure for a present weather from `/{local}/json/present.xml`.
*/
export type WmoPresentWxResponse = {
present: Array<{
cityId: number;
daynightcode: "" | "a" | "b";
iconNum: string;
issue: string;
moonrise: string;
moonset: string;
rh: "" | number;
stnId: string;
stnName: string;
sundate: string;
sunrise: string;
sunset: string;
temp: "" | number;
wd: string;
ws: string;
wxImageCode: string;
wxdesc: string;
}>;
/** A per-city present-weather observation from WMO. */
export type WmoPresentEntry = {
cityId: number;
daynightcode: "" | "a" | "b";
issue: string;
moonrise: string;
moonset: string;
rh: "" | number;
stnId: string;
stnName: string;
sundate: string;
sunrise: string;
sunset: string;
temp: "" | number;
wd: string;
ws: string;
wxImageCode: string;
wxdesc: string;
};

/** Response from the aggregate present-weather feed. */
export type WmoPresentAllResponse = {
present: Record<string, WmoPresentEntry & { iconNum: string }>;
};

const wmoUrl = "https://worldweather.wmo.int";
Expand All @@ -217,14 +217,14 @@ const wmoCode: Record<AppLocale, string> = {
};

/**
* Generates the URL for a WMO weather icon based on the icon ID.
* Generates the URL for a WMO weather icon based on its two-digit code.
*
* @param id - The four-digit icon ID.
* @param code - The two-digit icon code.
* @param daynightCode - Code that indicate day or night version (if available).
* @returns The URL of the weather icon.
*/
function wxIconUrl(id: string, daynightCode: string) {
return `${wmoUrl}/images/i${parseInt(id.slice(0, id.length - 2))}${daynightCode}.png`;
function wxIconUrl(code: string, daynightCode: string) {
return `${wmoUrl}/images/i${parseInt(code)}${daynightCode}.png`;
}

/** Fetches a URL and parses its JSON body, mapping parse failures to a descriptive error. */
Expand Down Expand Up @@ -327,15 +327,14 @@ export async function forecasts(
min: { unit, val: parseTemp(day.minTemp, day.minTempF, unit) },
max: { unit, val: parseTemp(day.maxTemp, day.maxTempF, unit) },
},
icon: day.weatherIcon != 0 ? wxIconUrl(day.weatherIcon.toString(), "") : null,
icon: day.weatherIcon != 0 ? wxIconUrl(day.weatherIcon.toString().slice(0, -2), "") : null,
}))
.slice(0, Math.max(Math.abs(days), 1)),
};
}

/**
* Fetches the present weather data for a specified city. Throws a RangeError
* when the feed carries no entry for the city.
* Fetches the present weather data for a specified city.
*
* @param cityId - The ID of the city.
* @param locale - The locale code.
Expand All @@ -349,13 +348,8 @@ export async function present(
unit: TempUnit,
windUnit: WindUnit,
): Promise<PresentWeather> {
const json = await getJson<WmoPresentWxResponse>(`${wmoUrl}/${wmoCode[locale]}/json/present.xml`);

const wx = Object.values(json.present).find((v) => v.cityId == cityId);

if (!wx) {
throw new RangeError(`No data for the city (id: ${cityId})`);
}
const apiLocale = wmoCode[locale];
const wx = await getJson<WmoPresentEntry>(`${wmoUrl}/${apiLocale}/json/present_${cityId}.json`);

return {
issueAt: parseIssueTime(wx.issue),
Expand All @@ -365,7 +359,7 @@ export async function present(
},
rh: wx.rh || null,
weather: wx.wxdesc,
icon: wx.iconNum !== "" ? wxIconUrl(wx.iconNum, wx.daynightcode) : null,
icon: wx.wxImageCode ? wxIconUrl(wx.wxImageCode, wx.daynightcode) : null,
wind:
wx.wd !== "" && wx.ws !== ""
? {
Expand All @@ -381,6 +375,11 @@ export async function present(
};
}

/** Fetches every present-weather observation from the aggregate feed. */
export async function presentAll(locale: AppLocale): Promise<WmoPresentAllResponse> {
return getJson<WmoPresentAllResponse>(`${wmoUrl}/${wmoCode[locale]}/json/present.xml`);
}

/**
* Fetches the data of list of countries.
*
Expand Down
Loading