Added node support for uploadWithProgress
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.29.5",
|
||||
"version": "0.29.6",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
||||
24
src/files.ts
24
src/files.ts
@@ -83,6 +83,7 @@ export function timestampFilename(name?: string, date: Date | number | string =
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return {PromiseProgress<T>} Promise of request with `onProgress` callback
|
||||
@@ -93,10 +94,12 @@ export function uploadWithProgress<T>(options: {
|
||||
headers?: {[key: string]: string};
|
||||
withCredentials?: boolean;
|
||||
}): PromiseProgress<T> {
|
||||
// Browser environment - use XMLHttpRequest for progress
|
||||
if (typeof XMLHttpRequest !== 'undefined') {
|
||||
return new PromiseProgress<T>((res, rej, prog) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
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.upload.addEventListener('progress', (event) => event.lengthComputable ? prog(event.loaded / event.total) : null);
|
||||
@@ -109,3 +112,22 @@ export function uploadWithProgress<T>(options: {
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user