|
1 | 1 | 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' |
3 | 3 | import { recursiveToCamel, resolveFetch } from '../lib/helpers' |
4 | 4 | import { |
5 | 5 | FileObject, |
@@ -118,23 +118,16 @@ export default class StorageFileApi { |
118 | 118 |
|
119 | 119 | const cleanPath = this._removeEmptyFolders(path) |
120 | 120 | 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 | + ) |
129 | 127 |
|
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, |
138 | 131 | } |
139 | 132 | } catch (error) { |
140 | 133 | if (isStorageError(error)) { |
@@ -207,22 +200,16 @@ export default class StorageFileApi { |
207 | 200 | headers['content-type'] = options.contentType as string |
208 | 201 | } |
209 | 202 |
|
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 | + ) |
217 | 209 |
|
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, |
226 | 213 | } |
227 | 214 | } catch (error) { |
228 | 215 | if (isStorageError(error)) { |
|
0 commit comments