diff --git a/package.json b/package.json index 5173fa8..4e78206 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.14.6", + "version": "0.14.7", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/http.ts b/src/http.ts index 3923518..43e1cae 100644 --- a/src/http.ts +++ b/src/http.ts @@ -6,12 +6,13 @@ export type DecodedResponse = Response & {data: T | null} export type HttpInterceptor = (response: Response, next: () => void) => void; export type HttpRequestOptions = { - url?: string; - fragment?: string; - query?: {key: string, value: string}[] | {[key: string]: string}; - method?: 'GET' | 'POST' | 'PATCH' | 'DELETE'; body?: any; + decode?: boolean; + fragment?: string; 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; } @@ -103,13 +104,14 @@ export class Http { } }); - const data = new Response(stream); - const content = resp.headers.get('Content-Type')?.toLowerCase(); - if(content?.includes('json')) resp.data = await data.json(); - else if(content?.includes('text')) resp.data = await data.text(); - else if(content?.includes('form')) resp.data = await data.formData(); - else if(content?.includes('application')) resp.data = await data.blob(); - else resp.data = null; + resp.data = new Response(stream); + if(opts.decode !== false) { + const content = resp.headers.get('Content-Type')?.toLowerCase(); + 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('form')) resp.data = await resp.data.formData(); + else if(content?.includes('application')) resp.data = await resp.data.blob(); + } if(resp.ok) res(resp); else rej(resp);