Added download function using fetch and links
This commit is contained in:
parent
f5d66f0d8f
commit
67b314b507
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.4.1",
|
||||
"version": "0.5.0",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
22
src/xhr.ts
22
src/xhr.ts
@ -7,6 +7,7 @@ export type RequestOptions = {
|
||||
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
||||
body?: any;
|
||||
headers?: {[key: string | symbol]: string | null | undefined};
|
||||
skipConverting?: boolean;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
@ -44,6 +45,14 @@ export class XHR {
|
||||
return () => { this.interceptors[key] = <any>null; }
|
||||
}
|
||||
|
||||
download(opts: RequestOptions & {url: string}) {
|
||||
this.request<Response>({...opts, skipConverting: true}).then(async resp => {
|
||||
const blob = await resp.blob();
|
||||
download(URL.createObjectURL(blob), <string>opts.url.split('/').pop());
|
||||
URL.revokeObjectURL(opts.url);
|
||||
});
|
||||
}
|
||||
|
||||
async request<T>(opts: RequestOptions = {}): Promise<T> {
|
||||
if(!this.opts.url && !opts.url) throw new Error('URL needs to be set');
|
||||
const url = (opts.url?.startsWith('http') ? opts.url : (this.opts.url || '') + (opts.url || '')).replace(/([^:]\/)\/+/g, '$1');
|
||||
@ -67,9 +76,18 @@ export class XHR {
|
||||
}
|
||||
|
||||
if(!resp.ok) throw new Error(resp.statusText);
|
||||
if(resp.headers.get('Content-Type')?.startsWith('application/json')) return await resp.json();
|
||||
if(resp.headers.get('Content-Type')?.startsWith('text/plain')) return await <any>resp.text();
|
||||
if(!opts.skipConverting && resp.headers.get('Content-Type')?.startsWith('application/json')) return await resp.json();
|
||||
if(!opts.skipConverting && resp.headers.get('Content-Type')?.startsWith('text/plain')) return await <any>resp.text();
|
||||
return resp;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function download(href: any, name: string) {
|
||||
const a = document.createElement('a');
|
||||
a.href = href;
|
||||
a.download = name;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user