Allow disabling response decoding
This commit is contained in:
parent
a168b03caf
commit
948fba3a2c
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.14.6",
|
"version": "0.14.7",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
24
src/http.ts
24
src/http.ts
@ -6,12 +6,13 @@ export type DecodedResponse<T> = Response & {data: T | null}
|
|||||||
export type HttpInterceptor = (response: Response, next: () => void) => void;
|
export type HttpInterceptor = (response: Response, next: () => void) => void;
|
||||||
|
|
||||||
export type HttpRequestOptions = {
|
export type HttpRequestOptions = {
|
||||||
url?: string;
|
|
||||||
fragment?: string;
|
|
||||||
query?: {key: string, value: string}[] | {[key: string]: string};
|
|
||||||
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
|
||||||
body?: any;
|
body?: any;
|
||||||
|
decode?: boolean;
|
||||||
|
fragment?: string;
|
||||||
headers?: {[key: string | symbol]: string | null | undefined};
|
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;
|
[key: string]: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,13 +104,14 @@ export class Http {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = new Response(stream);
|
resp.data = new Response(stream);
|
||||||
const content = resp.headers.get('Content-Type')?.toLowerCase();
|
if(opts.decode !== false) {
|
||||||
if(content?.includes('json')) resp.data = <T>await data.json();
|
const content = resp.headers.get('Content-Type')?.toLowerCase();
|
||||||
else if(content?.includes('text')) resp.data = <T>await data.text();
|
if(content?.includes('json')) resp.data = <T>await resp.data.json();
|
||||||
else if(content?.includes('form')) resp.data = <T>await data.formData();
|
else if(content?.includes('text')) resp.data = <T>await resp.data.text();
|
||||||
else if(content?.includes('application')) resp.data = <T>await data.blob();
|
else if(content?.includes('form')) resp.data = <T>await resp.data.formData();
|
||||||
else resp.data = <any>null;
|
else if(content?.includes('application')) resp.data = <T>await resp.data.blob();
|
||||||
|
}
|
||||||
|
|
||||||
if(resp.ok) res(resp);
|
if(resp.ok) res(resp);
|
||||||
else rej(resp);
|
else rej(resp);
|
||||||
|
Loading…
Reference in New Issue
Block a user