Files
juwan-frontend/lib/api/games.ts
T
2026-04-26 01:53:05 +08:00

37 lines
841 B
TypeScript

import { isApiError } from "@/lib/errors"
import type { Game } from "@/lib/types"
import { httpJson } from "./http"
type Paginated<T> = {
items: T[] | null
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=100", {
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
}
}