Compare commits

..

1 Commits

Author SHA1 Message Date
5b3bbdf02c Added node support for uploadWithProgress
All checks were successful
Build / Publish Docs (push) Successful in 54s
Build / Build NPM Project (push) Successful in 38s
Build / Tag Version (push) Successful in 9s
2026-07-13 22:16:58 -04:00
2 changed files with 35 additions and 13 deletions

View File

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

View File

@@ -83,6 +83,7 @@ export function timestampFilename(name?: string, date: Date | number | string =
/** /**
* Upload file to URL with progress callback using PromiseProgress * Upload file to URL with progress callback using PromiseProgress
* Works in both browser (with progress) and Node.js (fallback without progress)
* *
* @param {{url: string, files: File[], headers?: {[p: string]: string}, withCredentials?: boolean}} options * @param {{url: string, files: File[], headers?: {[p: string]: string}, withCredentials?: boolean}} options
* @return {PromiseProgress<T>} Promise of request with `onProgress` callback * @return {PromiseProgress<T>} Promise of request with `onProgress` callback
@@ -93,10 +94,12 @@ export function uploadWithProgress<T>(options: {
headers?: {[key: string]: string}; headers?: {[key: string]: string};
withCredentials?: boolean; withCredentials?: boolean;
}): PromiseProgress<T> { }): PromiseProgress<T> {
// Browser environment - use XMLHttpRequest for progress
if (typeof XMLHttpRequest !== 'undefined') {
return new PromiseProgress<T>((res, rej, prog) => { return new PromiseProgress<T>((res, rej, prog) => {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
const formData = new FormData(); const formData = new FormData();
options.files.forEach(f => formData.append('file', f)); options.files.forEach(f => formData.append('files', f));
xhr.withCredentials = !!options.withCredentials; xhr.withCredentials = !!options.withCredentials;
xhr.upload.addEventListener('progress', (event) => event.lengthComputable ? prog(event.loaded / event.total) : null); xhr.upload.addEventListener('progress', (event) => event.lengthComputable ? prog(event.loaded / event.total) : null);
@@ -109,3 +112,22 @@ export function uploadWithProgress<T>(options: {
xhr.send(formData); xhr.send(formData);
}); });
} }
// Node.js environment - fallback to fetch without progress
return new PromiseProgress<T>(async (res, rej) => {
try {
const formData = new FormData();
options.files.forEach(f => formData.append('files', f));
const response = await fetch(options.url, {method: 'POST', headers: options.headers || {}, body: formData});
if(!response.ok) {
const error = await response.text();
rej(JSONAttemptParse(error));
} else {
const result = await response.text();
res(<T>JSONAttemptParse(result));
}
} catch (error) {
rej(error);
}
});
}