Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
57446ef09a | |||
6d13df39f5 | |||
2d63db0ebb | |||
535fc0271e |
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.10.4",
|
"version": "0.15.1",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.10.4",
|
"version": "0.15.1",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/jest": "^29.5.12",
|
"@types/jest": "^29.5.12",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.14.10",
|
"version": "0.15.1",
|
||||||
"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));
|
||||||
|
@ -105,12 +105,12 @@ export class Http {
|
|||||||
});
|
});
|
||||||
|
|
||||||
resp.data = new Response(stream);
|
resp.data = new Response(stream);
|
||||||
if(opts.decode !== false) {
|
if(opts.decode == null || opts.decode) {
|
||||||
const content = resp.headers.get('Content-Type')?.toLowerCase();
|
const content = resp.headers.get('Content-Type')?.toLowerCase();
|
||||||
if(content?.includes('application')) resp.data = <T>await resp.data.blob();
|
if(content?.includes('form')) resp.data = <T>await resp.data.formData();
|
||||||
else if(content?.includes('form')) resp.data = <T>await resp.data.formData();
|
|
||||||
else if(content?.includes('json')) resp.data = <T>await resp.data.json();
|
else if(content?.includes('json')) resp.data = <T>await resp.data.json();
|
||||||
else if(content?.includes('text')) resp.data = <T>await resp.data.text();
|
else if(content?.includes('text')) resp.data = <T>await resp.data.text();
|
||||||
|
else if(content?.includes('application')) resp.data = <T>await resp.data.blob();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(resp.ok) res(resp);
|
if(resp.ok) res(resp);
|
||||||
|
12
src/misc.ts
12
src/misc.ts
@ -1,17 +1,5 @@
|
|||||||
import {md5} from './string';
|
import {md5} from './string';
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert data into a form encoded format.
|
|
||||||
*
|
|
||||||
* @param {any} data - data to convert
|
|
||||||
* @returns {string} - Ecodeded form data
|
|
||||||
*/
|
|
||||||
export function formEncode(data: any): string {
|
|
||||||
return Object.entries(data).map(([key, value]) =>
|
|
||||||
encodeURIComponent(key) + '=' + encodeURIComponent(<any>value)
|
|
||||||
).join('&');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get profile image from Gravatar
|
* Get profile image from Gravatar
|
||||||
*
|
*
|
||||||
|
@ -119,6 +119,17 @@ export function flattenObj(obj: any, parent?: any, result: any = {}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert object to FormData
|
||||||
|
* @param target - Object to convert
|
||||||
|
* @return {FormData} - Form object
|
||||||
|
*/
|
||||||
|
export function formData(target: any): FormData {
|
||||||
|
const data = new FormData();
|
||||||
|
Object.entries(target).forEach(([key, value]) => data.append(key, <any>value));
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check that an object has the following values
|
* Check that an object has the following values
|
||||||
*
|
*
|
||||||
@ -175,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) {
|
||||||
@ -185,3 +201,15 @@ export function sanitizedJSON(obj: any, space?: number) {
|
|||||||
return value;
|
return value;
|
||||||
}, space));
|
}, space));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert object into URL encoded string
|
||||||
|
*
|
||||||
|
* @param {any} data - data to convert
|
||||||
|
* @returns {string} - Encoded form data
|
||||||
|
*/
|
||||||
|
export function urlEncode(data: any): string {
|
||||||
|
return Object.entries(data).map(([key, value]) =>
|
||||||
|
encodeURIComponent(key) + '=' + encodeURIComponent(<any>value)
|
||||||
|
).join('&');
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user