From 5404d52bfec4aa9ea3f46a15440beb77dee4c055 Mon Sep 17 00:00:00 2001 From: Adrian Bonpin Date: Fri, 15 May 2026 00:55:30 +0800 Subject: [PATCH] feat(steamdb): create API endpoint GET /api/games/:gameId/steamdb-version --- lib/api/steamdb-version.ts | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 lib/api/steamdb-version.ts diff --git a/lib/api/steamdb-version.ts b/lib/api/steamdb-version.ts new file mode 100644 index 0000000..8213f65 --- /dev/null +++ b/lib/api/steamdb-version.ts @@ -0,0 +1,50 @@ +import { Elysia, t } from "elysia" +import { db } from "@/lib/db/index" +import { games } from "@/lib/db/schema" +import { eq } from "drizzle-orm" +import { scrapeSteamDBVersion } from "@/lib/steamdb/scrape" + +export const steamdbVersionRoutes = new Elysia({ + prefix: "/games/:gameId", + detail: { tags: ["Games"] }, +}).get( + "/steamdb-version", + async ({ params, set }) => { + // Look up game's steamAppId + const [game] = await db + .select({ steamAppId: games.steamAppId }) + .from(games) + .where(eq(games.id, params.gameId)) + .limit(1) + + if (!game) { + set.status = 404 + return { error: "Game not found" } + } + + if (game.steamAppId === null) { + return { unavailable: true, reason: "no_steam_app_id" } + } + + // Check if scraping is globally disabled + if (process.env.STEAMDB_SCRAPING_ENABLED === "false") { + return { unavailable: true, reason: "scraping_disabled" } + } + + const result = await scrapeSteamDBVersion(game.steamAppId) + + if (result.versionString === null && result.buildId === null) { + return { unavailable: true, reason: "not_found" } + } + + return { + versionString: result.versionString, + buildId: result.buildId, + steamAppId: game.steamAppId, + source: "steamdb", + } + }, + { + params: t.Object({ gameId: t.String() }), + }, +) \ No newline at end of file