Files
juwan-frontend/lib/api/files.ts
T
zetaloop 4037816998 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
2026-04-23 21:14:57 +08:00

34 lines
894 B
TypeScript

export async function getFileById(fileId: string): Promise<Blob> {
const res = await fetch(`/api/v1/files?key=${encodeURIComponent(fileId)}`)
if (res.ok) return await res.blob()
const text = await res.text()
let json: unknown = null
if (text) {
try {
json = JSON.parse(text) as unknown
} catch {
json = null
}
}
const maybeObj = (typeof json === "object" && json !== null ? json : null) as {
code?: unknown
message?: unknown
msg?: unknown
} | null
const code = (typeof maybeObj?.code === "number" ? maybeObj.code : res.status) as number
const msg =
typeof maybeObj?.message === "string"
? maybeObj.message
: typeof maybeObj?.msg === "string"
? maybeObj.msg
: text || res.statusText || "Request failed"
if (res.status === 401 || code === 401) throw new Error("UNAUTHORIZED")
throw { code, msg }
}