Files
whattoplay/server/steam-api.mjs

60 lines
1.3 KiB
JavaScript

/**
* Steam API Handler für Vite Dev Server
* Fungiert als Proxy um CORS-Probleme zu vermeiden
*/
import { fetchSteamGames } from "./steam-backend.mjs";
import { enrichGamesWithIgdb } from "./igdb-cache.mjs";
export async function handleSteamRefresh(req, res) {
if (req.method !== "POST") {
res.statusCode = 405;
res.end("Method Not Allowed");
return;
}
let body = "";
req.on("data", (chunk) => {
body += chunk.toString();
});
req.on("end", async () => {
try {
let payload;
try {
payload = JSON.parse(body || "{}");
} catch (error) {
res.statusCode = 400;
res.end(
JSON.stringify({
error: "Ungültiges JSON im Request-Body",
}),
);
return;
}
const { apiKey, steamId } = payload;
if (!apiKey || !steamId) {
res.statusCode = 400;
res.end(JSON.stringify({ error: "apiKey und steamId erforderlich" }));
return;
}
const { games, count } = await fetchSteamGames(apiKey, steamId);
const enriched = await enrichGamesWithIgdb(games);
res.statusCode = 200;
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ games: enriched, count: enriched.length }));
} catch (error) {
res.statusCode = 500;
res.end(
JSON.stringify({
error: error instanceof Error ? error.message : String(error),
}),
);
}
});
}