feat(catalog): fetch players, services, shops
This commit is contained in:
+37
-7
@@ -1,13 +1,43 @@
|
||||
import { useServiceStore } from "@/store/services"
|
||||
import { isApiError } from "@/lib/errors"
|
||||
import type { PlayerService } from "@/lib/types"
|
||||
|
||||
export function listServices() {
|
||||
return useServiceStore.getState().services
|
||||
import { httpJson } from "./http"
|
||||
|
||||
type Paginated<T> = {
|
||||
items: T[]
|
||||
meta: {
|
||||
total: number
|
||||
offset: number
|
||||
limit: number
|
||||
}
|
||||
}
|
||||
|
||||
export function getServiceById(serviceId: string) {
|
||||
return useServiceStore.getState().services.find((service) => service.id === serviceId)
|
||||
export async function listServices(): Promise<PlayerService[]> {
|
||||
const res = await httpJson<unknown>("/api/v1/services", { cache: "no-store" })
|
||||
if (typeof res === "object" && res !== null && "items" in res) {
|
||||
return (res as Paginated<PlayerService>).items
|
||||
}
|
||||
return res as PlayerService[]
|
||||
}
|
||||
|
||||
export function listServicesByPlayer(playerId: string) {
|
||||
return useServiceStore.getState().services.filter((service) => service.playerId === playerId)
|
||||
export async function getServiceById(serviceId: string): Promise<PlayerService | undefined> {
|
||||
try {
|
||||
return await httpJson<PlayerService>(`/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<PlayerService[]> {
|
||||
return httpJson<PlayerService[]>(`/api/v1/players/${encodeURIComponent(playerId)}/services`, {
|
||||
cache: "no-store",
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user