bubble up all fetch errors in the http helper
This commit is contained in:
parent
1af23ac544
commit
0f10aebfd2
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.20.1",
|
"version": "0.20.2",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
81
src/http.ts
81
src/http.ts
@ -75,47 +75,52 @@ export class Http {
|
|||||||
|
|
||||||
// Send request
|
// Send request
|
||||||
return new PromiseProgress((res, rej, prog) => {
|
return new PromiseProgress((res, rej, prog) => {
|
||||||
fetch(url, {
|
try {
|
||||||
headers,
|
fetch(url, {
|
||||||
method: opts.method || (opts.body ? 'POST' : 'GET'),
|
headers,
|
||||||
body: opts.body
|
method: opts.method || (opts.body ? 'POST' : 'GET'),
|
||||||
}).then(async (resp: any) => {
|
body: opts.body
|
||||||
for(let fn of [...Object.values(Http.interceptors), ...Object.values(this.interceptors)]) {
|
}).then(async (resp: any) => {
|
||||||
await new Promise<void>(res => fn(resp, () => res()));
|
for(let fn of [...Object.values(Http.interceptors), ...Object.values(this.interceptors)]) {
|
||||||
}
|
await new Promise<void>(res => fn(resp, () => res()));
|
||||||
|
|
||||||
const contentLength = resp.headers.get('Content-Length');
|
|
||||||
const total = contentLength ? parseInt(contentLength, 10) : 0;
|
|
||||||
let loaded = 0;
|
|
||||||
|
|
||||||
const reader = resp.body?.getReader();
|
|
||||||
const stream = new ReadableStream({
|
|
||||||
start(controller) {
|
|
||||||
function push() {
|
|
||||||
reader?.read().then((event: any) => {
|
|
||||||
if(event.done) return controller.close();
|
|
||||||
loaded += event.value.byteLength;
|
|
||||||
prog(loaded / total);
|
|
||||||
controller.enqueue(event.value);
|
|
||||||
push();
|
|
||||||
}).catch((error: any) => controller.error(error));
|
|
||||||
}
|
|
||||||
push();
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
resp.data = new Response(stream);
|
const contentLength = resp.headers.get('Content-Length');
|
||||||
if(opts.decode == null || opts.decode) {
|
const total = contentLength ? parseInt(contentLength, 10) : 0;
|
||||||
const content = resp.headers.get('Content-Type')?.toLowerCase();
|
let loaded = 0;
|
||||||
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);
|
const reader = resp.body?.getReader();
|
||||||
else rej(resp);
|
const stream = new ReadableStream({
|
||||||
})
|
start(controller) {
|
||||||
|
function push() {
|
||||||
|
reader?.read().then((event: any) => {
|
||||||
|
if(event.done) return controller.close();
|
||||||
|
loaded += event.value.byteLength;
|
||||||
|
prog(loaded / total);
|
||||||
|
controller.enqueue(event.value);
|
||||||
|
push();
|
||||||
|
}).catch((error: any) => controller.error(error));
|
||||||
|
}
|
||||||
|
|
||||||
|
push();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
}).catch(err => rej(err));
|
||||||
|
} catch(err) {
|
||||||
|
rej(err);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user