Compare commits

..

7 Commits

Author SHA1 Message Date
d587b92ea4 Messing around with http decoding
All checks were successful
Build / Build NPM Project (push) Successful in 24s
Build / Tag Version (push) Successful in 7s
2024-08-13 15:38:20 -04:00
26c6084052 Messing around with http decoding
All checks were successful
Build / Build NPM Project (push) Successful in 24s
Build / Tag Version (push) Successful in 7s
2024-08-13 15:21:56 -04:00
874bba59d3 Messing around with http decoding
All checks were successful
Build / Build NPM Project (push) Successful in 24s
Build / Tag Version (push) Successful in 7s
2024-08-13 15:19:07 -04:00
948fba3a2c Allow disabling response decoding
All checks were successful
Build / Build NPM Project (push) Successful in 49s
Build / Tag Version (push) Successful in 8s
2024-08-13 14:48:48 -04:00
a168b03caf Renamed entry point to index
All checks were successful
Build / Build NPM Project (push) Successful in 47s
Build / Tag Version (push) Successful in 7s
2024-08-09 12:27:44 -04:00
75160b7dbc Fixed upload headers
All checks were successful
Build / Build NPM Project (push) Successful in 22s
Build / Tag Version (push) Successful in 4s
2024-07-19 10:16:31 -04:00
af887a0bfb Fixed http body
All checks were successful
Build / Build NPM Project (push) Successful in 22s
Build / Tag Version (push) Successful in 4s
2024-07-19 10:05:47 -04:00
4 changed files with 22 additions and 20 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@ztimson/utils", "name": "@ztimson/utils",
"version": "0.14.3", "version": "0.14.10",
"description": "Utility library", "description": "Utility library",
"author": "Zak Timson", "author": "Zak Timson",
"license": "MIT", "license": "MIT",
@ -9,13 +9,13 @@
"type": "git", "type": "git",
"url": "https://git.zakscode.com/ztimson/js-utilities" "url": "https://git.zakscode.com/ztimson/js-utilities"
}, },
"main": "./dist/utils.cjs", "main": "./dist/index.cjs",
"module": "./dist/utils.mjs", "module": "./dist/index.mjs",
"types": "./dist/index.d.ts", "types": "./dist/index.d.ts",
"exports": { "exports": {
".": { ".": {
"import": "./dist/utils.mjs", "import": "./dist/index.mjs",
"require": "./dist/utils.cjs", "require": "./dist/index.cjs",
"types": "./dist/index.d.ts" "types": "./dist/index.d.ts"
} }
}, },

View File

@ -43,12 +43,12 @@ export function uploadWithProgress<T>(options: {
options.files.forEach(f => formData.append('file', f)); options.files.forEach(f => formData.append('file', f));
xhr.withCredentials = !!options.withCredentials xhr.withCredentials = !!options.withCredentials
Object.entries(options.headers || {}).forEach(([key, value]) => xhr.setRequestHeader(key, value));
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.upload.addEventListener('load', (resp) => res(<any>resp));
xhr.upload.addEventListener('error', (err) => rej(err)); xhr.upload.addEventListener('error', (err) => rej(err));
xhr.open('POST', options.url); xhr.open('POST', options.url);
Object.entries(options.headers || {}).forEach(([key, value]) => xhr.setRequestHeader(key, value));
xhr.send(formData); xhr.send(formData);
}); });
} }

View File

@ -6,12 +6,13 @@ export type DecodedResponse<T> = Response & {data: T | null}
export type HttpInterceptor = (response: Response, next: () => void) => void; export type HttpInterceptor = (response: Response, next: () => void) => void;
export type HttpRequestOptions = { export type HttpRequestOptions = {
url?: string;
fragment?: string;
query?: {key: string, value: string}[] | {[key: string]: string};
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
body?: any; body?: any;
decode?: boolean;
fragment?: string;
headers?: {[key: string | symbol]: string | null | undefined}; 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; [key: string]: any;
} }
@ -70,7 +71,7 @@ export class Http {
}); });
if(typeof opts.body == 'object' && opts.body != null && headers['Content-Type'] == 'application/json') if(typeof opts.body == 'object' && opts.body != null && headers['Content-Type'] == 'application/json')
opts.body = JSON.stringify(opts.json); opts.body = JSON.stringify(opts.body);
// Send request // Send request
return new PromiseProgress((res, rej, prog) => { return new PromiseProgress((res, rej, prog) => {
@ -103,13 +104,14 @@ export class Http {
} }
}); });
const data = new Response(stream); resp.data = new Response(stream);
if(opts.decode !== false) {
const content = resp.headers.get('Content-Type')?.toLowerCase(); const content = resp.headers.get('Content-Type')?.toLowerCase();
if(content?.includes('json')) resp.data = <T>await data.json(); if(content?.includes('application')) resp.data = <T>await resp.data.blob();
else if(content?.includes('text')) resp.data = <T>await data.text(); else if(content?.includes('form')) resp.data = <T>await resp.data.formData();
else if(content?.includes('form')) resp.data = <T>await data.formData(); else if(content?.includes('json')) resp.data = <T>await resp.data.json();
else if(content?.includes('application')) resp.data = <T>await data.blob(); else if(content?.includes('text')) resp.data = <T>await resp.data.text();
else resp.data = <any>null; }
if(resp.ok) res(resp); if(resp.ok) res(resp);
else rej(resp); else rej(resp);

View File

@ -8,8 +8,8 @@ export default defineConfig({
entry: resolve(process.cwd(), 'src/index.ts'), entry: resolve(process.cwd(), 'src/index.ts'),
name: 'utils', name: 'utils',
fileName: (module, entryName) => { fileName: (module, entryName) => {
if(module == 'es') return 'utils.mjs'; if(module == 'es') return 'index.mjs';
if(module == 'umd') return 'utils.cjs'; if(module == 'umd') return 'index.cjs';
} }
}, },
emptyOutDir: true, emptyOutDir: true,