( method: "POST" | "PUT", url: string, headers: Headers, body: FormData | Blob, onProgress?: (progress: UploadProgress) => void, )
| 355 | }; |
| 356 | |
| 357 | const uploadWithXhr = ( |
| 358 | method: "POST" | "PUT", |
| 359 | url: string, |
| 360 | headers: Headers, |
| 361 | body: FormData | Blob, |
| 362 | onProgress?: (progress: UploadProgress) => void, |
| 363 | ) => |
| 364 | new Promise<void>((resolve, reject) => { |
| 365 | const xhr = new XMLHttpRequest(); |
| 366 | xhr.open(method, url); |
| 367 | headers.forEach((value, key) => { |
| 368 | xhr.setRequestHeader(key, value); |
| 369 | }); |
| 370 | xhr.upload.onprogress = (event) => { |
| 371 | onProgress?.({ |
| 372 | loaded: event.loaded, |
| 373 | total: event.lengthComputable ? event.total : 0, |
| 374 | }); |
| 375 | }; |
| 376 | xhr.onload = () => { |
| 377 | if (xhr.status >= 200 && xhr.status < 300) { |
| 378 | resolve(); |
| 379 | return; |
| 380 | } |
| 381 | reject( |
| 382 | new MobileApiError( |
| 383 | "Upload target rejected the file", |
| 384 | xhr.status, |
| 385 | xhr.responseText, |
| 386 | ), |
| 387 | ); |
| 388 | }; |
| 389 | xhr.onerror = () => { |
| 390 | reject(new Error("Upload failed")); |
| 391 | }; |
| 392 | xhr.send(body); |
| 393 | }); |
| 394 | |
| 395 | const fileBlob = async (file: UploadFile) => { |
| 396 | const response = await fetch(file.uri); |
no test coverage detected