fix(api): align API layer with backend response formats

- wallet: parse {balance: string} response
- favorites: addFavorite returns void (EmptyResp)
- services: handle paginated response from listServicesByPlayer
- files: use query param ?key= instead of path param /:id
- search: remove unsupported selectedGames/minRating params
This commit is contained in:
zetaloop
2026-04-23 21:14:57 +08:00
parent ca4bef959f
commit 4037816998
5 changed files with 22 additions and 18 deletions
+2 -2
View File
@@ -39,8 +39,8 @@ export async function isFavorited(
export async function addFavorite(input: {
targetType: "player" | "shop"
targetId: string
}): Promise<Favorite> {
return await httpJson<Favorite>("/api/v1/favorites", {
}): Promise<void> {
await httpJson<unknown>("/api/v1/favorites", {
method: "POST",
json: input,
cache: "no-store",
+1 -1
View File
@@ -1,5 +1,5 @@
export async function getFileById(fileId: string): Promise<Blob> {
const res = await fetch(`/api/v1/files/${encodeURIComponent(fileId)}`)
const res = await fetch(`/api/v1/files?key=${encodeURIComponent(fileId)}`)
if (res.ok) return await res.blob()
-6
View File
@@ -20,15 +20,9 @@ export async function searchCatalog(params: SearchCatalogParams): Promise<Search
const searchParams = new URLSearchParams()
if (params.q) searchParams.set("q", params.q)
for (const game of params.selectedGames ?? []) {
searchParams.append("selectedGames", game)
}
if (params.min) searchParams.set("min", params.min)
if (params.max) searchParams.set("max", params.max)
if (params.onlyOnline !== undefined) searchParams.set("onlyOnline", String(params.onlyOnline))
if (params.minRating && params.minRating !== "0") searchParams.set("minRating", params.minRating)
if (params.sort && params.sort !== "composite") searchParams.set("sort", params.sort)
if (params.limit !== undefined) searchParams.set("limit", String(params.limit))
+10 -3
View File
@@ -37,7 +37,14 @@ export async function getServiceById(serviceId: string): Promise<PlayerService |
}
export async function listServicesByPlayer(playerId: string): Promise<PlayerService[]> {
return httpJson<PlayerService[]>(`/api/v1/players/${encodeURIComponent(playerId)}/services`, {
cache: "no-store",
})
const res = await httpJson<Paginated<PlayerService> | PlayerService[]>(
`/api/v1/players/${encodeURIComponent(playerId)}/services`,
{
cache: "no-store",
},
)
if (typeof res === "object" && res !== null && "items" in res) {
return (res as Paginated<PlayerService>).items
}
return res as PlayerService[]
}
+9 -6
View File
@@ -26,19 +26,22 @@ function withOffsetLimit(path: string, options?: ListWalletTransactionsOptions):
return `${path}?${searchParams.toString()}`
}
function unwrapWalletBalance(value: unknown): number | undefined {
if (typeof value === "number") return value
if (typeof value !== "object" || value === null) return undefined
type WalletBalanceResponse = {
balance: string
frozenBalance?: string
}
const v = value as { balance?: unknown; amount?: unknown }
function parseBalance(value: unknown): number | undefined {
if (typeof value !== "object" || value === null) return undefined
const v = value as WalletBalanceResponse
if (typeof v.balance === "string") return Number(v.balance)
if (typeof v.balance === "number") return v.balance
if (typeof v.amount === "number") return v.amount
return undefined
}
export async function getWalletBalance(): Promise<number> {
const res = await httpJson<unknown>("/api/v1/wallet/balance", { cache: "no-store" })
const balance = unwrapWalletBalance(res)
const balance = parseBalance(res)
if (balance === undefined) {
throw new Error("Invalid wallet balance response")
}