Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
2d63db0ebb | |||
535fc0271e | |||
d587b92ea4 | |||
26c6084052 | |||
874bba59d3 |
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.14.7",
|
||||
"version": "0.14.12",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
@ -105,11 +105,11 @@ export class Http {
|
||||
});
|
||||
|
||||
resp.data = new Response(stream);
|
||||
if(opts.decode !== false) {
|
||||
if(opts.decode == null || opts.decode) {
|
||||
const content = resp.headers.get('Content-Type')?.toLowerCase();
|
||||
if(content?.includes('json')) resp.data = <T>await resp.data.json();
|
||||
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('text')) resp.data = <T>await resp.data.text();
|
||||
else if(content?.includes('form')) resp.data = <T>await resp.data.formData();
|
||||
else if(content?.includes('application')) resp.data = <T>await resp.data.blob();
|
||||
}
|
||||
|
||||
|
12
src/misc.ts
12
src/misc.ts
@ -1,17 +1,5 @@
|
||||
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
|
||||
*
|
||||
|
@ -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
|
||||
*
|
||||
@ -185,3 +196,15 @@ export function sanitizedJSON(obj: any, space?: number) {
|
||||
return value;
|
||||
}, 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