From 4037816998c32a83ce4770bb37d7496ddaf29f39 Mon Sep 17 00:00:00 2001 From: zetaloop Date: Thu, 23 Apr 2026 21:14:57 +0800 Subject: [PATCH] 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 --- lib/api/favorites.ts | 4 ++-- lib/api/files.ts | 2 +- lib/api/search.ts | 6 ------ lib/api/services.ts | 13 ++++++++++--- lib/api/transactions.ts | 15 +++++++++------ 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/api/favorites.ts b/lib/api/favorites.ts index 0d6caba..e8e97f6 100644 --- a/lib/api/favorites.ts +++ b/lib/api/favorites.ts @@ -39,8 +39,8 @@ export async function isFavorited( export async function addFavorite(input: { targetType: "player" | "shop" targetId: string -}): Promise { - return await httpJson("/api/v1/favorites", { +}): Promise { + await httpJson("/api/v1/favorites", { method: "POST", json: input, cache: "no-store", diff --git a/lib/api/files.ts b/lib/api/files.ts index 1044489..498e0df 100644 --- a/lib/api/files.ts +++ b/lib/api/files.ts @@ -1,5 +1,5 @@ export async function getFileById(fileId: string): Promise { - 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() diff --git a/lib/api/search.ts b/lib/api/search.ts index 0c8198e..259564d 100644 --- a/lib/api/search.ts +++ b/lib/api/search.ts @@ -20,15 +20,9 @@ export async function searchCatalog(params: SearchCatalogParams): Promise { - return httpJson(`/api/v1/players/${encodeURIComponent(playerId)}/services`, { - cache: "no-store", - }) + 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[] } diff --git a/lib/api/transactions.ts b/lib/api/transactions.ts index 7dd5b0d..0503e62 100644 --- a/lib/api/transactions.ts +++ b/lib/api/transactions.ts @@ -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 { const res = await httpJson("/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") }