37 lines
828 B
TypeScript
37 lines
828 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=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
|
|
}
|
|
}
|