import { prisma } from "@/lib/db"; import { decryptSecret } from "@/lib/crypto"; import type { LfmPeriod } from "@/lib/lastfm"; // Shared by the three /api/lastfm routes (was copy-pasted three times). Route-only module // (underscore prefix ⇒ not a route), so importing prisma here never reaches a client bundle. export const PERIODS: LfmPeriod[] = ["overall", "7day", "1month", "3month", "6month", "12month"]; export const LIMIT = 50; export async function getLastfmCreds(): Promise<{ username: string; apiKey: string } | null> { const rows = await prisma.config.findMany({ where: { key: { in: ["lastfm.username", "lastfm.api_key"] } }, }); const byKey = new Map(rows.map((r) => [r.key, r])); const username = byKey.get("lastfm.username")?.value ?? ""; const keyRow = byKey.get("lastfm.api_key"); const apiKey = keyRow ? (keyRow.secret ? decryptSecret(keyRow.value) : keyRow.value) : ""; if (!username || !apiKey) return null; return { username, apiKey }; } /** The `period` query param, validated to a supported LfmPeriod (default "overall"). */ export function parsePeriod(url: URL): LfmPeriod { const p = url.searchParams.get("period") ?? "overall"; return (PERIODS as string[]).includes(p) ? (p as LfmPeriod) : "overall"; } /** Whether a manual Refresh (cache bypass) was requested. */ export function wantsRefresh(url: URL): boolean { return url.searchParams.get("refresh") === "1"; }