Initial push
This commit is contained in:
35
app/api/server/versions/route.ts
Normal file
35
app/api/server/versions/route.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { auth } 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 auth.api.getSession({ headers: 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 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user