Compare commits

...

2 Commits
0.4.1 ... 0.6.0

Author SHA1 Message Date
d0e9cbcaa6 Added download utilities
All checks were successful
Build / Build NPM Project (push) Successful in 37s
Build / Tag Version (push) Successful in 7s
Build / Publish (push) Successful in 14s
2024-04-21 21:33:38 -04:00
67b314b507 Added download function using fetch and links
All checks were successful
Build / Build NPM Project (push) Successful in 37s
Build / Tag Version (push) Successful in 7s
Build / Publish (push) Successful in 16s
2024-04-21 21:03:07 -04:00
4 changed files with 52 additions and 3 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@ztimson/utils",
"version": "0.4.1",
"version": "0.6.0",
"description": "Utility library",
"author": "Zak Timson",
"license": "MIT",

46
src/download.ts Normal file
View File

@ -0,0 +1,46 @@
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);
}
export function downloadStream(url: string, name?: 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);
emitter.emit('progress', 1);
if(name) {
const url = URL.createObjectURL(blob);
download(url, name);
URL.revokeObjectURL(url);
}
emitter.emit('complete', blob);
return;
} else {
const chunk = result.value;
chunks.push(chunk);
loaded += chunk.length;
const progress = Math.round((loaded / total) * 100);
emitter.emit('progress', progress);
reader.read().then(processResult);
}
});
});
return emitter;
}

View File

@ -1,5 +1,6 @@
export * from './array';
export * from './aset';
export * from './download.ts';
export * from './emitter';
export * from './errors';
export * from './logger';

View File

@ -1,3 +1,4 @@
import {TypedEmitter, TypedEvents} from './emitter.ts';
import {clean} from './objects';
export type Interceptor = (request: Response, next: () => void) => void;
@ -7,6 +8,7 @@ export type RequestOptions = {
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
body?: any;
headers?: {[key: string | symbol]: string | null | undefined};
skipConverting?: boolean;
[key: string]: any;
}
@ -67,8 +69,8 @@ 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;
});
}