import type { SourceModule } from "../registry"; import { CiderConfig, CiderPayload, type CiderConfig as Config, type CiderPayload as Payload, } from "@/lib/schemas/sources/cider"; export const ciderModule: SourceModule = { kind: "cider", label: "Now Playing", keyless: false, defaultRefreshSeconds: 30, configSchema: CiderConfig, payloadSchema: CiderPayload, isConfigured: (config) => config.appToken.length > 0, async fetch({ config, signal }) { const res = await fetch(`${config.host}/api/v1/playback/now-playing`, { headers: { apptoken: config.appToken }, signal, }); if (res.status === 204) return { playing: false, track: null }; if (!res.ok) throw new Error(`Cider ${res.status}`); const j = (await res.json()) as { status?: string; info?: { name?: string; artistName?: string; albumName?: string; artwork?: { url?: string }; durationInMillis?: number; currentPlaybackTime?: number; }; }; if (j.status !== "ok" || !j.info) return { playing: false, track: null }; const info = j.info; const artRaw = info.artwork?.url ?? null; // Replace {w}x{h} template Apple Music uses in artwork URLs const artworkUrl = artRaw ? artRaw.replace("{w}", "300").replace("{h}", "300") : null; return { playing: true, track: { name: info.name ?? "Unknown", artist: info.artistName ?? "Unknown", album: info.albumName ?? "Unknown", artworkUrl, durationMs: (info.durationInMillis ?? 0), currentMs: Math.round((info.currentPlaybackTime ?? 0) * 1000), }, }; }, };