Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
2d63db0ebb | |||
535fc0271e | |||
d587b92ea4 | |||
26c6084052 | |||
874bba59d3 | |||
948fba3a2c | |||
a168b03caf |
10
package.json
10
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.14.5",
|
||||
"version": "0.14.12",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
@ -9,13 +9,13 @@
|
||||
"type": "git",
|
||||
"url": "https://git.zakscode.com/ztimson/js-utilities"
|
||||
},
|
||||
"main": "./dist/utils.cjs",
|
||||
"module": "./dist/utils.mjs",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.mjs",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/utils.mjs",
|
||||
"require": "./dist/utils.cjs",
|
||||
"import": "./dist/index.mjs",
|
||||
"require": "./dist/index.cjs",
|
||||
"types": "./dist/index.d.ts"
|
||||
}
|
||||
},
|
||||
|
24
src/http.ts
24
src/http.ts
@ -6,12 +6,13 @@ export type DecodedResponse<T> = Response & {data: T | null}
|
||||
export type HttpInterceptor = (response: Response, next: () => void) => void;
|
||||
|
||||
export type HttpRequestOptions = {
|
||||
url?: string;
|
||||
fragment?: string;
|
||||
query?: {key: string, value: string}[] | {[key: string]: string};
|
||||
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
||||
body?: any;
|
||||
decode?: boolean;
|
||||
fragment?: string;
|
||||
headers?: {[key: string | symbol]: string | null | undefined};
|
||||
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
||||
query?: {key: string, value: string}[] | {[key: string]: string};
|
||||
url?: string;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
@ -103,13 +104,14 @@ export class Http {
|
||||
}
|
||||
});
|
||||
|
||||
const data = new Response(stream);
|
||||
const content = resp.headers.get('Content-Type')?.toLowerCase();
|
||||
if(content?.includes('json')) resp.data = <T>await data.json();
|
||||
else if(content?.includes('text')) resp.data = <T>await data.text();
|
||||
else if(content?.includes('form')) resp.data = <T>await data.formData();
|
||||
else if(content?.includes('application')) resp.data = <T>await data.blob();
|
||||
else resp.data = <any>null;
|
||||
resp.data = new Response(stream);
|
||||
if(opts.decode == null || opts.decode) {
|
||||
const content = resp.headers.get('Content-Type')?.toLowerCase();
|
||||
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('application')) resp.data = <T>await resp.data.blob();
|
||||
}
|
||||
|
||||
if(resp.ok) res(resp);
|
||||
else rej(resp);
|
||||
|
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('&');
|
||||
}
|
||||
|
@ -8,8 +8,8 @@ export default defineConfig({
|
||||
entry: resolve(process.cwd(), 'src/index.ts'),
|
||||
name: 'utils',
|
||||
fileName: (module, entryName) => {
|
||||
if(module == 'es') return 'utils.mjs';
|
||||
if(module == 'umd') return 'utils.cjs';
|
||||
if(module == 'es') return 'index.mjs';
|
||||
if(module == 'umd') return 'index.cjs';
|
||||
}
|
||||
},
|
||||
emptyOutDir: true,
|
||||
|
Reference in New Issue
Block a user