feat(catalog): fetch players, services, shops

This commit is contained in:
zetaloop
2026-02-28 16:37:15 +08:00
parent f4365668ab
commit f1ae3e04bb
11 changed files with 234 additions and 57 deletions
+38 -7
View File
@@ -1,13 +1,44 @@
import { usePlayerStore } from "@/store/players"
import { isApiError } from "@/lib/errors"
import type { Player } from "@/lib/types"
export function listPlayers() {
return usePlayerStore.getState().players
import { httpJson } from "./http"
type Paginated<T> = {
items: T[]
meta: {
total: number
offset: number
limit: number
}
}
export function getPlayerById(playerId: string) {
return usePlayerStore.getState().players.find((player) => player.id === playerId)
export async function listPlayers(): Promise<Player[]> {
const res = await httpJson<Paginated<Player>>("/api/v1/players?offset=0&limit=1000", {
cache: "no-store",
})
return res.items
}
export function listPlayersByShop(shopId: string) {
return usePlayerStore.getState().players.filter((player) => player.shopId === shopId)
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=1000`,
{ cache: "no-store" },
)
return res.items
}
+37 -7
View File
@@ -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",
})
}
+46 -7
View File
@@ -1,13 +1,52 @@
import { useShopStore } from "@/store/shops"
import { isApiError } from "@/lib/errors"
import type { Shop } from "@/lib/types"
export function listShops() {
return useShopStore.getState().shops
import { httpJson } from "./http"
type Paginated<T> = {
items: T[]
meta: {
total: number
offset: number
limit: number
}
}
export function getShopById(shopId: string) {
return useShopStore.getState().shops.find((shop) => shop.id === shopId)
export async function listShops(): Promise<Shop[]> {
const res = await httpJson<Paginated<Shop>>("/api/v1/shops?offset=0&limit=1000", {
cache: "no-store",
})
return res.items
}
export function getShopByOwnerId(ownerId: string) {
return useShopStore.getState().shops.find((shop) => shop.owner.id === ownerId)
export async function getShopById(shopId: string): Promise<Shop | undefined> {
try {
return await httpJson<Shop>(`/api/v1/shops/${encodeURIComponent(shopId)}`, {
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 getShopByOwnerId(ownerId: string): Promise<Shop | undefined> {
try {
return await httpJson<Shop>(`/api/v1/users/${encodeURIComponent(ownerId)}/shop`, {
cache: "no-store",
})
} catch (error) {
if (error instanceof Error && error.message === "UNAUTHORIZED") {
throw error
}
if (isApiError(error) && error.code === 404) {
return undefined
}
throw error
}
}