import { isApiError } from "@/lib/errors" import type { PlayerService } from "@/lib/types" import { httpJson } from "./http" type Paginated = { items: T[] meta: { total: number offset: number limit: number } } export async function listServices(): Promise { const res = await httpJson("/api/v1/services", { cache: "no-store" }) if (typeof res === "object" && res !== null && "items" in res) { return (res as Paginated).items } return res as PlayerService[] } export async function getServiceById(serviceId: string): Promise { try { return await httpJson(`/api/v1/services/${encodeURIComponent(serviceId)}`, { 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 listServicesByPlayer(playerId: string): Promise { const res = await httpJson | PlayerService[]>( `/api/v1/players/${encodeURIComponent(playerId)}/services`, { cache: "no-store", }, ) if (typeof res === "object" && res !== null && "items" in res) { return (res as Paginated).items } return res as PlayerService[] }