Files
whattoplay/server/steam-backend.mjs
2026-02-06 16:53:07 +01:00

56 lines
1.5 KiB
JavaScript

/**
* Steam Backend - Isoliertes Modul für Steam API Calls
* Keine Dependencies zu Vite oder Express
*/
/**
* Ruft Steam API auf und gibt formatierte Spiele zurück
* @param {string} apiKey - Steam Web API Key
* @param {string} steamId - Steam User ID
* @returns {Promise<{games: Array, count: number}>}
*/
export async function fetchSteamGames(apiKey, steamId) {
if (!apiKey || !steamId) {
throw new Error("apiKey und steamId sind erforderlich");
}
// Steam API aufrufen
const url = new URL(
"https://api.steampowered.com/IPlayerService/GetOwnedGames/v1/",
);
url.searchParams.set("key", apiKey);
url.searchParams.set("steamid", steamId);
url.searchParams.set("include_appinfo", "true");
url.searchParams.set("include_played_free_games", "true");
const response = await fetch(url);
if (!response.ok) {
throw new Error(
`Steam API Error: ${response.status} ${response.statusText}`,
);
}
const data = await response.json();
const rawGames = data.response?.games ?? [];
// Spiele formatieren
const games = rawGames.map((game) => ({
id: `steam-${game.appid}`,
title: game.name,
source: "steam",
sourceId: String(game.appid),
platform: "PC",
lastPlayed: game.rtime_last_played
? new Date(game.rtime_last_played * 1000).toISOString().slice(0, 10)
: null,
playtimeHours: Math.round((game.playtime_forever / 60) * 10) / 10,
url: `https://store.steampowered.com/app/${game.appid}`,
}));
return {
games,
count: games.length,
};
}