feat(games): fetch games from backend
This commit is contained in:
+32
-5
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user