Compare commits
8 Commits
0693397164
...
0.14.0
Author | SHA1 | Date | |
---|---|---|---|
0a775f919b | |||
7063b80bdd | |||
f3049160b6 | |||
e21273ddb2 | |||
8d3b427c1b | |||
87857a01c5 | |||
535e579b43 | |||
2934f86530 |
25
.github/workflows/build.yaml
vendored
25
.github/workflows/build.yaml
vendored
@ -9,34 +9,19 @@ jobs:
|
|||||||
build:
|
build:
|
||||||
name: Build NPM Project
|
name: Build NPM Project
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
container:
|
container: node:20-alpine
|
||||||
image: node
|
|
||||||
volumes:
|
|
||||||
- '/mnt/swarm/gitea/runner/cache:/cache'
|
|
||||||
steps:
|
steps:
|
||||||
- name: Clone Repository
|
- name: Clone Repository
|
||||||
uses: ztimson/actions/clone@develop
|
uses: ztimson/actions/clone@develop
|
||||||
|
|
||||||
- name: Restore node_modules
|
- name: Install & Build
|
||||||
uses: ztimson/actions/cache/restore@develop
|
run: |
|
||||||
with:
|
npm i
|
||||||
key: node_modules
|
npm run build
|
||||||
|
|
||||||
- name: Install Dependencies
|
|
||||||
run: npm i
|
|
||||||
|
|
||||||
- name: Build Project
|
|
||||||
run: npm run build
|
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: npm run test:coverage
|
run: npm run test:coverage
|
||||||
|
|
||||||
- name: Cache node_modules
|
|
||||||
uses: ztimson/actions/cache@develop
|
|
||||||
with:
|
|
||||||
key: node_modules
|
|
||||||
pattern: node_modules
|
|
||||||
|
|
||||||
- name: Upload to Registry
|
- name: Upload to Registry
|
||||||
uses: ztimson/actions/npm/publish@develop
|
uses: ztimson/actions/npm/publish@develop
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.13.0",
|
"version": "0.14.0",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
54
src/files.ts
Normal file
54
src/files.ts
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
import {PromiseProgress} from './promise-progress.ts';
|
||||||
|
|
||||||
|
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 downloadBlob(blob: Blob, name: string) {
|
||||||
|
const url = URL.createObjectURL(blob);
|
||||||
|
download(url, name);
|
||||||
|
URL.revokeObjectURL(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function fileBrowser(options: {accept?: string, multiple?: boolean} = {}): Promise<File[]> {
|
||||||
|
return new Promise(res => {
|
||||||
|
const input = document.createElement('input');
|
||||||
|
input.type = 'file';
|
||||||
|
input.accept = options.accept || '*';
|
||||||
|
input.style.display='none';
|
||||||
|
input.multiple = !!options.multiple;
|
||||||
|
input.onblur = input.onchange = async () => {
|
||||||
|
res(Array.from(<any>input.files));
|
||||||
|
input.remove();
|
||||||
|
}
|
||||||
|
document.body.appendChild(input);
|
||||||
|
input.click();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function uploadWithProgress(options: {
|
||||||
|
url: string;
|
||||||
|
file: File;
|
||||||
|
headers?: {[key: string]: string};
|
||||||
|
withCredentials?: boolean;
|
||||||
|
}) {
|
||||||
|
return new PromiseProgress((res, rej, prog) => {
|
||||||
|
const xhr = new XMLHttpRequest();
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('file', options.file);
|
||||||
|
|
||||||
|
xhr.withCredentials = !!options.withCredentials
|
||||||
|
Object.entries(options.headers || {}).forEach(([key, value]) => xhr.setRequestHeader(key, value));
|
||||||
|
xhr.upload.addEventListener('progress', (event) => event.lengthComputable ? prog(event.loaded / event.total) : null);
|
||||||
|
xhr.upload.addEventListener('load', (resp) => res(resp));
|
||||||
|
xhr.upload.addEventListener('error', (err) => rej(err));
|
||||||
|
|
||||||
|
xhr.open('POST', options.url);
|
||||||
|
xhr.send(formData);
|
||||||
|
});
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
import {clean} from './objects';
|
import {clean} from './objects';
|
||||||
import {PromiseProgress} from './promise-progress.ts';
|
import {PromiseProgress} from './promise-progress';
|
||||||
|
|
||||||
export type DecodedResponse<T> = Response & {data?: T}
|
export type DecodedResponse<T> = Response & {data?: T}
|
||||||
|
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
export * from './array';
|
export * from './array';
|
||||||
export * from './aset';
|
export * from './aset';
|
||||||
export * from './download';
|
export * from './files';
|
||||||
export * from './emitter';
|
export * from './emitter';
|
||||||
export * from './errors';
|
export * from './errors';
|
||||||
export * from './http.ts';
|
export * from './http';
|
||||||
export * from './logger';
|
export * from './logger';
|
||||||
export * from './math';
|
export * from './math';
|
||||||
export * from './misc';
|
export * from './misc';
|
||||||
@ -11,4 +11,3 @@ export * from './objects';
|
|||||||
export * from './promise-progress';
|
export * from './promise-progress';
|
||||||
export * from './string';
|
export * from './string';
|
||||||
export * from './time';
|
export * from './time';
|
||||||
export * from './upload.ts';
|
|
||||||
|
@ -1,25 +0,0 @@
|
|||||||
import {PromiseProgress} from './promise-progress.ts';
|
|
||||||
|
|
||||||
export type UploadOptions = {
|
|
||||||
url: string;
|
|
||||||
file: File;
|
|
||||||
headers?: {[key: string]: string};
|
|
||||||
withCredentials?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function uploadWithProgress(options: UploadOptions) {
|
|
||||||
return new PromiseProgress((res, rej, prog) => {
|
|
||||||
const xhr = new XMLHttpRequest();
|
|
||||||
const formData = new FormData();
|
|
||||||
formData.append('file', options.file);
|
|
||||||
|
|
||||||
xhr.withCredentials = !!options.withCredentials
|
|
||||||
Object.entries(options.headers || {}).forEach(([key, value]) => xhr.setRequestHeader(key, value));
|
|
||||||
xhr.upload.addEventListener('progress', (event) => event.lengthComputable ? prog(event.loaded / event.total) : null);
|
|
||||||
xhr.upload.addEventListener('load', (resp) => res(resp));
|
|
||||||
xhr.upload.addEventListener('error', (err) => rej(err));
|
|
||||||
|
|
||||||
xhr.open('POST', options.url);
|
|
||||||
xhr.send(formData);
|
|
||||||
});
|
|
||||||
}
|
|
Reference in New Issue
Block a user