Files
juwan-frontend/lib/api/games.ts
T
2026-02-28 16:23:30 +08:00

37 lines
829 B
TypeScript

import { isApiError } from "@/lib/errors"
import type { Game } from "@/lib/types"
import { httpJson } from "./http"
type Paginated<T> = {
items: T[]
meta: {
total: number
offset: number
limit: number
}
}
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
}
}