Added JSONAttemptParse & fixed uploadProgress response
This commit is contained in:
parent
2d63db0ebb
commit
6d13df39f5
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.14.12",
|
"version": "0.15.0",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import {deepCopy, JSONAttemptParse} from './objects.ts';
|
||||||
import {PromiseProgress} from './promise-progress';
|
import {PromiseProgress} from './promise-progress';
|
||||||
|
|
||||||
export function download(href: any, name: string) {
|
export function download(href: any, name: string) {
|
||||||
@ -42,10 +43,11 @@ export function uploadWithProgress<T>(options: {
|
|||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
options.files.forEach(f => formData.append('file', f));
|
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('progress', (event) => event.lengthComputable ? prog(event.loaded / event.total) : null);
|
||||||
xhr.upload.addEventListener('load', (resp) => res(<any>resp));
|
xhr.addEventListener('loadend', () => res(<T>JSONAttemptParse(xhr.responseText)));
|
||||||
xhr.upload.addEventListener('error', (err) => rej(err));
|
xhr.addEventListener('error', () => rej(JSONAttemptParse(xhr.responseText)));
|
||||||
|
xhr.addEventListener('timeout', () => rej({error: 'Request timed out'}));
|
||||||
|
|
||||||
xhr.open('POST', options.url);
|
xhr.open('POST', options.url);
|
||||||
Object.entries(options.headers || {}).forEach(([key, value]) => xhr.setRequestHeader(key, value));
|
Object.entries(options.headers || {}).forEach(([key, value]) => xhr.setRequestHeader(key, value));
|
||||||
|
@ -186,7 +186,12 @@ export function mixin(target: any, constructors: any[]) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function sanitizedJSON(obj: any, space?: number) {
|
export function JSONAttemptParse<T>(json: string): T | string {
|
||||||
|
try { return JSON.parse(json); }
|
||||||
|
catch { return json; }
|
||||||
|
}
|
||||||
|
|
||||||
|
export function JSONSanitized(obj: any, space?: number) {
|
||||||
let cache: any[] = [];
|
let cache: any[] = [];
|
||||||
return JSON.parse(JSON.stringify(obj, (key, value) => {
|
return JSON.parse(JSON.stringify(obj, (key, value) => {
|
||||||
if (typeof value === 'object' && value !== null) {
|
if (typeof value === 'object' && value !== null) {
|
||||||
|
Loading…
Reference in New Issue
Block a user