Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
1d5509a078 | |||
9f57b93a9f | |||
d0e9cbcaa6 | |||
67b314b507 |
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.4.1",
|
"version": "0.7.1",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
52
src/download.ts
Normal file
52
src/download.ts
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
import {TypedEmitter, TypedEvents} from './emitter.ts';
|
||||||
|
|
||||||
|
export type downloadEvents = TypedEvents & {
|
||||||
|
complete: (blob: Blob) => any;
|
||||||
|
progress: (progress: number) => any;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Download a URL using fetch so progress can be tracked. Uses Typed Emitter to emit a "progress" &
|
||||||
|
* "complete" event.
|
||||||
|
*
|
||||||
|
* @param {string} url
|
||||||
|
* @param {string} downloadName
|
||||||
|
* @return {TypedEmitter<downloadEvents>}
|
||||||
|
*/
|
||||||
|
export function downloadProgress(url: string, downloadName?: string) {
|
||||||
|
const emitter = new TypedEmitter<downloadEvents>();
|
||||||
|
fetch(url).then(response => {
|
||||||
|
const contentLength = response.headers.get('Content-Length') || '0';
|
||||||
|
const total = parseInt(contentLength, 10);
|
||||||
|
let chunks: any[] = [], loaded = 0;
|
||||||
|
const reader = response.body?.getReader();
|
||||||
|
reader?.read().then(function processResult(result) {
|
||||||
|
if(result.done) {
|
||||||
|
const blob = new Blob(chunks);
|
||||||
|
if(downloadName) {
|
||||||
|
const url = URL.createObjectURL(blob);
|
||||||
|
download(url, downloadName);
|
||||||
|
URL.revokeObjectURL(url);
|
||||||
|
}
|
||||||
|
emitter.emit('complete', blob);
|
||||||
|
} else {
|
||||||
|
const chunk = result.value;
|
||||||
|
chunks.push(chunk);
|
||||||
|
loaded += chunk.length;
|
||||||
|
const progress = loaded / total;
|
||||||
|
emitter.emit('progress', progress);
|
||||||
|
reader.read().then(processResult);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return emitter;
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
export * from './array';
|
export * from './array';
|
||||||
export * from './aset';
|
export * from './aset';
|
||||||
|
export * from './download';
|
||||||
export * from './emitter';
|
export * from './emitter';
|
||||||
export * from './errors';
|
export * from './errors';
|
||||||
export * from './logger';
|
export * from './logger';
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import {TypedEmitter, TypedEvents} from './emitter.ts';
|
||||||
import {clean} from './objects';
|
import {clean} from './objects';
|
||||||
|
|
||||||
export type Interceptor = (request: Response, next: () => void) => void;
|
export type Interceptor = (request: Response, next: () => void) => void;
|
||||||
@ -7,6 +8,7 @@ export type RequestOptions = {
|
|||||||
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
||||||
body?: any;
|
body?: any;
|
||||||
headers?: {[key: string | symbol]: string | null | undefined};
|
headers?: {[key: string | symbol]: string | null | undefined};
|
||||||
|
skipConverting?: boolean;
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,8 +69,8 @@ export class XHR {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(!resp.ok) throw new Error(resp.statusText);
|
if(!resp.ok) throw new Error(resp.statusText);
|
||||||
if(resp.headers.get('Content-Type')?.startsWith('application/json')) return await resp.json();
|
if(!opts.skipConverting && 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('text/plain')) return await <any>resp.text();
|
||||||
return resp;
|
return resp;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user