feat(games): fetch games from backend

This commit is contained in:
zetaloop
2026-02-28 16:23:30 +08:00
parent 6dd21e1090
commit f4365668ab
5 changed files with 96 additions and 14 deletions
+32 -5
View File
@@ -1,9 +1,36 @@
import { mockGames } from "@/lib/mock"
import { isApiError } from "@/lib/errors"
import type { Game } from "@/lib/types"
export function listGames() {
return mockGames
import { httpJson } from "./http"
type Paginated<T> = {
items: T[]
meta: {
total: number
offset: number
limit: number
}
}
export function getGameById(gameId: string) {
return mockGames.find((game) => game.id === gameId)
export async function listGames(): Promise<Game[]> {
const res = await httpJson<Paginated<Game>>("/api/v1/games?offset=0&limit=1000", {
cache: "no-store",
})
return res.items
}
export async function getGameById(gameId: string): Promise<Game | undefined> {
try {
return await httpJson<Game>(`/api/v1/games/${encodeURIComponent(gameId)}`, {
cache: "no-store",
})
} catch (error) {
if (error instanceof Error && error.message === "UNAUTHORIZED") {
throw error
}
if (isApiError(error) && error.code === 404) {
return undefined
}
throw error
}
}