Added JSONAttemptParse & fixed uploadProgress response
All checks were successful
Build / Build NPM Project (push) Successful in 28s
Build / Tag Version (push) Successful in 7s

This commit is contained in:
2024-08-17 14:21:05 -04:00
parent 2d63db0ebb
commit 6d13df39f5
3 changed files with 12 additions and 5 deletions

View File

@ -1,3 +1,4 @@
import {deepCopy, JSONAttemptParse} from './objects.ts';
import {PromiseProgress} from './promise-progress';
export function download(href: any, name: string) {
@ -42,10 +43,11 @@ export function uploadWithProgress<T>(options: {
const formData = new FormData();
options.files.forEach(f => formData.append('file', f));
xhr.withCredentials = !!options.withCredentials
xhr.withCredentials = !!options.withCredentials;
xhr.upload.addEventListener('progress', (event) => event.lengthComputable ? prog(event.loaded / event.total) : null);
xhr.upload.addEventListener('load', (resp) => res(<any>resp));
xhr.upload.addEventListener('error', (err) => rej(err));
xhr.addEventListener('loadend', () => res(<T>JSONAttemptParse(xhr.responseText)));
xhr.addEventListener('error', () => rej(JSONAttemptParse(xhr.responseText)));
xhr.addEventListener('timeout', () => rej({error: 'Request timed out'}));
xhr.open('POST', options.url);
Object.entries(options.headers || {}).forEach(([key, value]) => xhr.setRequestHeader(key, value));