Compare commits

...

4 Commits

Author SHA1 Message Date
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
7626c20f08 Improved http
All checks were successful
Build / Build NPM Project (push) Successful in 22s
Build / Tag Version (push) Successful in 5s
2024-07-19 09:24:53 -04:00
6998616445 Fixed import
All checks were successful
Build / Build NPM Project (push) Successful in 22s
Build / Tag Version (push) Successful in 4s
2024-07-19 09:02:42 -04:00
3 changed files with 15 additions and 18 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@ztimson/utils", "name": "@ztimson/utils",
"version": "0.14.1", "version": "0.14.5",
"description": "Utility library", "description": "Utility library",
"author": "Zak Timson", "author": "Zak Timson",
"license": "MIT", "license": "MIT",

View File

@ -1,4 +1,4 @@
import {PromiseProgress} from './promise-progress.ts'; import {PromiseProgress} from './promise-progress';
export function download(href: any, name: string) { export function download(href: any, name: string) {
const a = document.createElement('a'); const a = document.createElement('a');
@ -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

@ -1,7 +1,7 @@
import {clean} from './objects'; import {clean} from './objects';
import {PromiseProgress} from './promise-progress'; import {PromiseProgress} from './promise-progress';
export type DecodedResponse<T> = Response & {data?: T} export type DecodedResponse<T> = Response & {data: T | null}
export type HttpInterceptor = (response: Response, next: () => void) => void; export type HttpInterceptor = (response: Response, next: () => void) => void;
@ -70,7 +70,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) => {
@ -78,14 +78,11 @@ export class Http {
headers, headers,
method: opts.method || (opts.body ? 'POST' : 'GET'), method: opts.method || (opts.body ? 'POST' : 'GET'),
body: opts.body body: opts.body
}).then(async (resp: DecodedResponse<T>) => { }).then(async (resp: any) => {
console.log('done!');
for(let fn of [...Object.values(Http.interceptors), ...Object.values(this.interceptors)]) { for(let fn of [...Object.values(Http.interceptors), ...Object.values(this.interceptors)]) {
await new Promise<void>(res => fn(resp, () => res())); await new Promise<void>(res => fn(resp, () => res()));
} }
if(!resp.ok) rej(resp);
const contentLength = resp.headers.get('Content-Length'); const contentLength = resp.headers.get('Content-Length');
const total = contentLength ? parseInt(contentLength, 10) : 0; const total = contentLength ? parseInt(contentLength, 10) : 0;
let loaded = 0; let loaded = 0;
@ -94,28 +91,28 @@ export class Http {
const stream = new ReadableStream({ const stream = new ReadableStream({
start(controller) { start(controller) {
function push() { function push() {
reader?.read().then(({done, value}) => { reader?.read().then((event: any) => {
if(done) return controller.close(); if(event.done) return controller.close();
loaded += value.byteLength; loaded += event.value.byteLength;
prog(loaded / total); prog(loaded / total);
controller.enqueue(value); controller.enqueue(event.value);
push(); push();
}).catch(error => { }).catch((error: any) => controller.error(error));
controller.error(error);
});
} }
push(); push();
} }
}); });
const data = new Response(stream); const data = new Response(stream);
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('json')) resp.data = <T>await data.json();
else if(content?.includes('text')) resp.data = <T>await data.text(); 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('form')) resp.data = <T>await data.formData();
else if(content?.includes('application')) resp.data = <T>await data.blob(); else if(content?.includes('application')) resp.data = <T>await data.blob();
res(resp); else resp.data = <any>null;
if(resp.ok) res(resp);
else rej(resp);
}) })
}); });
} }