42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import { isApiError } from "@/lib/errors"
|
|
import type { Player } from "@/lib/types"
|
|
|
|
import { httpJson } from "./http"
|
|
|
|
type Paginated<T> = {
|
|
items: T[] | null
|
|
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 players = await listPlayers()
|
|
return players.filter((player) => player.shopId === shopId)
|
|
}
|