91 lines
2.0 KiB
JavaScript
91 lines
2.0 KiB
JavaScript
/**
|
|
* GOG API Handler für Vite Dev Server
|
|
* Fungiert als Proxy um CORS-Probleme zu vermeiden
|
|
*/
|
|
|
|
import { exchangeGogCode, fetchGogGames } from "./gog-backend.mjs";
|
|
import { enrichGamesWithIgdb } from "./igdb-cache.mjs";
|
|
|
|
export async function handleGogAuth(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 {
|
|
const payload = JSON.parse(body || "{}");
|
|
const { code } = payload;
|
|
|
|
if (!code) {
|
|
res.statusCode = 400;
|
|
res.end(JSON.stringify({ error: "code ist erforderlich" }));
|
|
return;
|
|
}
|
|
|
|
const tokens = await exchangeGogCode(code);
|
|
|
|
res.statusCode = 200;
|
|
res.setHeader("Content-Type", "application/json");
|
|
res.end(JSON.stringify(tokens));
|
|
} catch (error) {
|
|
res.statusCode = 500;
|
|
res.end(
|
|
JSON.stringify({
|
|
error: error instanceof Error ? error.message : String(error),
|
|
}),
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
export async function handleGogRefresh(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 {
|
|
const payload = JSON.parse(body || "{}");
|
|
const { accessToken, refreshToken } = payload;
|
|
|
|
if (!accessToken || !refreshToken) {
|
|
res.statusCode = 400;
|
|
res.end(
|
|
JSON.stringify({
|
|
error: "accessToken und refreshToken sind erforderlich",
|
|
}),
|
|
);
|
|
return;
|
|
}
|
|
|
|
const result = await fetchGogGames(accessToken, refreshToken);
|
|
result.games = await enrichGamesWithIgdb(result.games);
|
|
|
|
res.statusCode = 200;
|
|
res.setHeader("Content-Type", "application/json");
|
|
res.end(JSON.stringify(result));
|
|
} catch (error) {
|
|
res.statusCode = 500;
|
|
res.end(
|
|
JSON.stringify({
|
|
error: error instanceof Error ? error.message : String(error),
|
|
}),
|
|
);
|
|
}
|
|
});
|
|
}
|