Skip to content

Commit 8436221

Browse files
authored
fix: Properly handle errors in Upload Api (related to StorageError) (#216)
* fix upload error handling * bypass type check * fix _getRequestParams
1 parent bb2f998 commit 8436221

File tree

2 files changed

+25
-36
lines changed

2 files changed

+25
-36
lines changed

src/lib/fetch.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,17 @@ const _getRequestParams = (
4545
) => {
4646
const params: { [k: string]: any } = { method, headers: options?.headers || {} }
4747

48-
if (method === 'GET') {
48+
if (method === 'GET' || !body) {
4949
return params
5050
}
5151

52-
params.headers = { 'Content-Type': 'application/json', ...options?.headers }
53-
54-
if (body) {
52+
if (body instanceof FormData) {
53+
params.body = body
54+
} else {
55+
params.headers = { 'Content-Type': 'application/json', ...options?.headers }
5556
params.body = JSON.stringify(body)
5657
}
58+
5759
return { ...params, ...parameters }
5860
}
5961

src/packages/StorageFileApi.ts

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isStorageError, StorageError, StorageUnknownError } from '../lib/errors'
2-
import { Fetch, get, head, post, remove } from '../lib/fetch'
2+
import { Fetch, get, head, post, put, remove } from '../lib/fetch'
33
import { recursiveToCamel, resolveFetch } from '../lib/helpers'
44
import {
55
FileObject,
@@ -118,23 +118,16 @@ export default class StorageFileApi {
118118

119119
const cleanPath = this._removeEmptyFolders(path)
120120
const _path = this._getFinalPath(cleanPath)
121-
const res = await this.fetch(`${this.url}/object/${_path}`, {
122-
method,
123-
body: body as BodyInit,
124-
headers,
125-
...(options?.duplex ? { duplex: options.duplex } : {}),
126-
})
127-
128-
const data = await res.json()
121+
const data = await (method == 'PUT' ? put : post)(
122+
this.fetch,
123+
`${this.url}/object/${_path}`,
124+
body as object,
125+
{ headers, ...(options?.duplex ? { duplex: options.duplex } : {}) }
126+
)
129127

130-
if (res.ok) {
131-
return {
132-
data: { path: cleanPath, id: data.Id, fullPath: data.Key },
133-
error: null,
134-
}
135-
} else {
136-
const error = data
137-
return { data: null, error }
128+
return {
129+
data: { path: cleanPath, id: data.Id, fullPath: data.Key },
130+
error: null,
138131
}
139132
} catch (error) {
140133
if (isStorageError(error)) {
@@ -207,22 +200,16 @@ export default class StorageFileApi {
207200
headers['content-type'] = options.contentType as string
208201
}
209202

210-
const res = await this.fetch(url.toString(), {
211-
method: 'PUT',
212-
body: body as BodyInit,
213-
headers,
214-
})
215-
216-
const data = await res.json()
203+
const data = await put(
204+
this.fetch,
205+
url.toString(),
206+
body as object,
207+
{ headers }
208+
)
217209

218-
if (res.ok) {
219-
return {
220-
data: { path: cleanPath, fullPath: data.Key },
221-
error: null,
222-
}
223-
} else {
224-
const error = data
225-
return { data: null, error }
210+
return {
211+
data: { path: cleanPath, fullPath: data.Key },
212+
error: null,
226213
}
227214
} catch (error) {
228215
if (isStorageError(error)) {

0 commit comments

Comments
 (0)