Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
86196c3feb | |||
0985ff145e | |||
7cd717fc7d | |||
2fe8cdb96a | |||
34c2df7a1a | |||
1d5509a078 | |||
9f57b93a9f | |||
d0e9cbcaa6 |
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.5.0",
|
||||
"version": "0.9.0",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
8
src/download.ts
Normal file
8
src/download.ts
Normal file
@ -0,0 +1,8 @@
|
||||
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);
|
||||
}
|
@ -1,10 +1,12 @@
|
||||
export * from './array';
|
||||
export * from './aset';
|
||||
export * from './download';
|
||||
export * from './emitter';
|
||||
export * from './errors';
|
||||
export * from './logger';
|
||||
export * from './misc';
|
||||
export * from './objects';
|
||||
export * from './promise-progress';
|
||||
export * from './string';
|
||||
export * from './time';
|
||||
export * from './xhr';
|
||||
|
26
src/promise-progress.ts
Normal file
26
src/promise-progress.ts
Normal file
@ -0,0 +1,26 @@
|
||||
export type ProgressCallback = (progress: number) => any;
|
||||
|
||||
export class PromiseProgress<T> extends Promise<T> {
|
||||
private listeners: ProgressCallback[] = [];
|
||||
|
||||
private _progress = 0;
|
||||
get progress() { return this._progress; }
|
||||
set progress(p: number) {
|
||||
if(p == this._progress) return;
|
||||
this._progress = p;
|
||||
this.listeners.forEach(l => l(p));
|
||||
}
|
||||
|
||||
constructor(executor: (resolve: (value: T) => any, reject: (reason: any) => void, progress: (progress: number) => any) => void) {
|
||||
super((resolve, reject) => executor(
|
||||
(value: T) => resolve(value),
|
||||
(reason: any) => reject(reason),
|
||||
(progress: number) => this.progress = progress
|
||||
));
|
||||
}
|
||||
|
||||
onProgress(callback: ProgressCallback) {
|
||||
this.listeners.push(callback);
|
||||
return this;
|
||||
}
|
||||
}
|
17
src/xhr.ts
17
src/xhr.ts
@ -45,14 +45,6 @@ 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');
|
||||
@ -82,12 +74,3 @@ export class XHR {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user