36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { auth, getAuthSession } from "@/lib/auth";
|
|
import { fetchVanillaVersions, fetchPaperVersions, fetchFabricVersions, type VersionInfo } from "@/lib/minecraft/versions";
|
|
import { checkRateLimit, getClientIp } from "@/lib/security/rateLimit";
|
|
|
|
export async function GET(req: NextRequest) {
|
|
const session = await getAuthSession(req.headers);
|
|
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
|
|
const ip = getClientIp(req);
|
|
const { allowed } = checkRateLimit(ip, 20);
|
|
if (!allowed) return NextResponse.json({ error: "Too many requests" }, { status: 429 });
|
|
|
|
const type = req.nextUrl.searchParams.get("type") ?? "vanilla";
|
|
|
|
try {
|
|
let versionInfos: VersionInfo[];
|
|
switch (type) {
|
|
case "paper":
|
|
versionInfos = await fetchPaperVersions();
|
|
break;
|
|
case "fabric":
|
|
versionInfos = await fetchFabricVersions();
|
|
break;
|
|
case "vanilla":
|
|
default:
|
|
versionInfos = await fetchVanillaVersions();
|
|
}
|
|
const versions = versionInfos.map((v) => v.id);
|
|
return NextResponse.json({ versions, type });
|
|
} catch (err) {
|
|
const message = err instanceof Error ? err.message : "Failed to fetch versions";
|
|
return NextResponse.json({ error: message }, { status: 503 });
|
|
}
|
|
}
|