import type { SourceModule } from "../registry"; import { OnThisDayConfig, OnThisDayPayload, type OnThisDayConfig as Config, type OnThisDayPayload as Payload, } from "@/lib/schemas/sources/onthisday"; interface WikiEntry { year?: number; text: string; pages?: Array<{ content_urls?: { desktop?: { page?: string } } }>; } const MONTHS = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", ]; function map(entries: WikiEntry[] = [], limit: number) { return entries.slice(0, limit).map((e) => ({ year: e.year ?? null, text: e.text, link: e.pages?.[0]?.content_urls?.desktop?.page ?? null, })); } export const onThisDayModule: SourceModule = { kind: "onthisday", label: "On This Day", keyless: true, defaultRefreshSeconds: 86_400, configSchema: OnThisDayConfig, payloadSchema: OnThisDayPayload, async fetch({ config, signal }) { const now = new Date(); const mm = String(now.getMonth() + 1).padStart(2, "0"); const dd = String(now.getDate()).padStart(2, "0"); const res = await fetch( `https://en.wikipedia.org/api/rest_v1/feed/onthisday/all/${mm}/${dd}`, { signal, headers: { "User-Agent": "PersonalDashboard/0.1 (local app)", Accept: "application/json", }, }, ); if (!res.ok) throw new Error(`Wikipedia ${res.status}`); const j = (await res.json()) as { events?: WikiEntry[]; births?: WikiEntry[]; deaths?: WikiEntry[]; }; return { date: `${MONTHS[now.getMonth()]} ${now.getDate()}`, events: map(j.events, config.eventsLimit), births: map(j.births, config.birthsLimit), deaths: map(j.deaths, config.deathsLimit), }; }, };