Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
26c6084052 | |||
874bba59d3 | |||
948fba3a2c |
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.14.6",
|
||||
"version": "0.14.9",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
27
src/http.ts
27
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 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;
|
||||
}
|
||||
|
||||
@ -104,12 +105,18 @@ export class Http {
|
||||
});
|
||||
|
||||
const data = new Response(stream);
|
||||
const content = resp.headers.get('Content-Type')?.toLowerCase();
|
||||
if(content?.includes('json')) resp.data = <T>await data.json();
|
||||
else if(content?.includes('text')) resp.data = <T>await data.text();
|
||||
else if(content?.includes('form')) resp.data = <T>await data.formData();
|
||||
else if(content?.includes('application')) resp.data = <T>await data.blob();
|
||||
else resp.data = <any>null;
|
||||
resp.blob = data.blob;
|
||||
resp.formData = data.formData;
|
||||
resp.json = data.json;
|
||||
resp.text = data.text;
|
||||
|
||||
if(opts.decode !== false) {
|
||||
const content = resp.headers.get('Content-Type')?.toLowerCase();
|
||||
if(content?.includes('application')) resp.data = <T>await data.blob();
|
||||
else if(content?.includes('form')) resp.data = <T>await data.formData();
|
||||
else if(content?.includes('json')) resp.data = <T>await data.json();
|
||||
else if(content?.includes('text')) resp.data = <T>await data.text();
|
||||
}
|
||||
|
||||
if(resp.ok) res(resp);
|
||||
else rej(resp);
|
||||
|
Reference in New Issue
Block a user