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:
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user