Fixed http no decode

This commit is contained in:
Zakary Timson 2025-02-18 16:04:18 -05:00
parent f755d8f5b8
commit a3b34ef03f
2 changed files with 23 additions and 8 deletions

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

@ -22,6 +22,22 @@ export type HttpDefaults = {
url?: string; url?: string;
} }
class HttpResponse<T = any> extends Response {
data?: T
ok!: boolean;
redirected!: boolean;
type!: ResponseType;
url!: string;
constructor(resp: Response, stream: ReadableStream) {
super(stream, {headers: resp.headers, status: resp.status, statusText: resp.statusText});
this.ok = resp.ok;
this.redirected = resp.redirected;
this.type = resp.type;
this.url = resp.url;
}
}
export class Http { export class Http {
private static interceptors: {[key: string]: HttpInterceptor} = {}; private static interceptors: {[key: string]: HttpInterceptor} = {};
@ -101,18 +117,17 @@ export class Http {
push(); push();
}).catch((error: any) => controller.error(error)); }).catch((error: any) => controller.error(error));
} }
push(); push();
} }
}); });
resp.data = new Response(stream); resp = new HttpResponse<T>(resp, stream);
if(opts.decode == null || opts.decode) { if(opts.decode !== false) {
const content = resp.headers.get('Content-Type')?.toLowerCase(); const content = resp.headers.get('Content-Type')?.toLowerCase();
if(content?.includes('form')) resp.data = <T>await resp.data.formData(); if(content?.includes('form')) resp.data = <T>await resp.formData();
else if(content?.includes('json')) resp.data = <T>await resp.data.json(); else if(content?.includes('json')) resp.data = <T>await resp.json();
else if(content?.includes('text')) resp.data = <T>await resp.data.text(); else if(content?.includes('text')) resp.data = <T>await resp.text();
else if(content?.includes('application')) resp.data = <T>await resp.data.blob(); else if(content?.includes('application')) resp.data = <T>await resp.blob();
} }
if(resp.ok) res(resp); if(resp.ok) res(resp);