45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
import { isApiError } from "@/lib/errors"
|
|
import type { Player } from "@/lib/types"
|
|
|
|
import { httpJson } from "./http"
|
|
|
|
type Paginated<T> = {
|
|
items: T[]
|
|
meta: {
|
|
total: number
|
|
offset: number
|
|
limit: number
|
|
}
|
|
}
|
|
|
|
export async function listPlayers(): Promise<Player[]> {
|
|
const res = await httpJson<Paginated<Player>>("/api/v1/players?offset=0&limit=100", {
|
|
cache: "no-store",
|
|
})
|
|
return res.items
|
|
}
|
|
|
|
export async function getPlayerById(playerId: string): Promise<Player | undefined> {
|
|
try {
|
|
return await httpJson<Player>(`/api/v1/players/${encodeURIComponent(playerId)}`, {
|
|
cache: "no-store",
|
|
})
|
|
} catch (error) {
|
|
if (error instanceof Error && error.message === "UNAUTHORIZED") {
|
|
throw error
|
|
}
|
|
if (isApiError(error) && error.code === 404) {
|
|
return undefined
|
|
}
|
|
throw error
|
|
}
|
|
}
|
|
|
|
export async function listPlayersByShop(shopId: string): Promise<Player[]> {
|
|
const res = await httpJson<Paginated<Player>>(
|
|
`/api/v1/shops/${encodeURIComponent(shopId)}/players?offset=0&limit=100`,
|
|
{ cache: "no-store" },
|
|
)
|
|
return res.items
|
|
}
|