Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -61,8 +61,66 @@ export const put = (u, b = {}, c = {}) =>
|
||||
export const del = (u, p = {}, c = {}) =>
|
||||
api.delete(u, { params: p, ...c }).then(r => r.data)
|
||||
|
||||
export const download = (u, p = {}, c = {}) =>
|
||||
api.get(u, { params: p, responseType: 'blob', ...c })
|
||||
.then(r => r.data)
|
||||
async function parseBlobErrorMessage(data) {
|
||||
if (!data) return ''
|
||||
|
||||
if (typeof Blob !== 'undefined' && data instanceof Blob) {
|
||||
try {
|
||||
const text = (await data.text())?.trim()
|
||||
if (!text) return ''
|
||||
|
||||
try {
|
||||
const json = JSON.parse(text)
|
||||
return (
|
||||
json?.detail ||
|
||||
json?.message ||
|
||||
json?.error ||
|
||||
text
|
||||
)
|
||||
} catch {
|
||||
return text
|
||||
}
|
||||
} catch {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof data === 'string') return data.trim()
|
||||
if (typeof data === 'object') {
|
||||
return (
|
||||
data?.detail ||
|
||||
data?.message ||
|
||||
data?.error ||
|
||||
''
|
||||
)
|
||||
}
|
||||
|
||||
return ''
|
||||
}
|
||||
|
||||
export const download = async (u, p = {}, c = {}) => {
|
||||
try {
|
||||
const r = await api.get(u, { params: p, responseType: 'blob', ...c })
|
||||
return r.data
|
||||
} catch (err) {
|
||||
let detail =
|
||||
err?.response?.data?.detail ||
|
||||
err?.response?.data?.message ||
|
||||
''
|
||||
|
||||
if (!detail) {
|
||||
detail = await parseBlobErrorMessage(err?.response?.data)
|
||||
}
|
||||
|
||||
if (!detail) {
|
||||
detail = err?.message || 'Download failed'
|
||||
}
|
||||
|
||||
const wrapped = new Error(detail)
|
||||
wrapped.status = err?.response?.status
|
||||
wrapped.original = err
|
||||
throw wrapped
|
||||
}
|
||||
}
|
||||
|
||||
export default api
|
||||
|
||||
Reference in New Issue
Block a user