feat(files): fetch file blob by id
This commit is contained in:
+32
-3
@@ -1,4 +1,33 @@
|
||||
export function getFileById(fileId: string): never {
|
||||
void fileId
|
||||
throw new Error("Not implemented")
|
||||
export async function getFileById(fileId: string): Promise<Blob> {
|
||||
const res = await fetch(`/api/v1/files/${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 }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user