diff --git a/package.json b/package.json index 39b7db3..e461e1a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.23.11", + "version": "0.23.12", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/http.ts b/src/http.ts index 230d478..80f3ae4 100644 --- a/src/http.ts +++ b/src/http.ts @@ -22,6 +22,22 @@ export type HttpDefaults = { url?: string; } +class HttpResponse 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 { private static interceptors: {[key: string]: HttpInterceptor} = {}; @@ -101,18 +117,17 @@ export class Http { push(); }).catch((error: any) => controller.error(error)); } - push(); } }); - resp.data = new Response(stream); - if(opts.decode == null || opts.decode) { + resp = new HttpResponse(resp, stream); + if(opts.decode !== false) { const content = resp.headers.get('Content-Type')?.toLowerCase(); - if(content?.includes('form')) resp.data = await resp.data.formData(); - else if(content?.includes('json')) resp.data = await resp.data.json(); - else if(content?.includes('text')) resp.data = await resp.data.text(); - else if(content?.includes('application')) resp.data = await resp.data.blob(); + if(content?.includes('form')) resp.data = await resp.formData(); + else if(content?.includes('json')) resp.data = await resp.json(); + else if(content?.includes('text')) resp.data = await resp.text(); + else if(content?.includes('application')) resp.data = await resp.blob(); } if(resp.ok) res(resp);