23 lines
972 B
TypeScript
23 lines
972 B
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { getTrends, type TrendGranularity } from "@/lib/queries";
|
|
|
|
const GRANULARITIES: TrendGranularity[] = ["hour", "day", "week", "month"];
|
|
|
|
export async function GET(req: NextRequest) {
|
|
const sp = req.nextUrl.searchParams;
|
|
const requestedGranularity = sp.get("granularity");
|
|
const granularity = GRANULARITIES.includes(requestedGranularity as TrendGranularity)
|
|
? requestedGranularity as TrendGranularity
|
|
: "day";
|
|
const startTs = sp.get("start") ? Number(sp.get("start")) : undefined;
|
|
const endTs = sp.get("end") ? Number(sp.get("end")) : undefined;
|
|
const channelId = sp.get("channel_id") ? Number(sp.get("channel_id")) : undefined;
|
|
|
|
const data = await getTrends(granularity, startTs, endTs, {
|
|
username: sp.get("username") || undefined,
|
|
model: sp.get("model") || undefined,
|
|
channelId: Number.isFinite(channelId) ? channelId : undefined,
|
|
});
|
|
return NextResponse.json(data);
|
|
}
|