Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5b3bbdf02c |
@@ -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",
|
||||||
|
|||||||
46
src/files.ts
46
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
|
* 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,19 +94,40 @@ export function uploadWithProgress<T>(options: {
|
|||||||
headers?: {[key: string]: string};
|
headers?: {[key: string]: string};
|
||||||
withCredentials?: boolean;
|
withCredentials?: boolean;
|
||||||
}): PromiseProgress<T> {
|
}): PromiseProgress<T> {
|
||||||
return new PromiseProgress<T>((res, rej, prog) => {
|
// Browser environment - use XMLHttpRequest for progress
|
||||||
const xhr = new XMLHttpRequest();
|
if (typeof XMLHttpRequest !== 'undefined') {
|
||||||
const formData = new FormData();
|
return new PromiseProgress<T>((res, rej, prog) => {
|
||||||
options.files.forEach(f => formData.append('file', f));
|
const xhr = new XMLHttpRequest();
|
||||||
|
const formData = new FormData();
|
||||||
|
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);
|
||||||
xhr.addEventListener('loadend', () => res(<T>JSONAttemptParse(xhr.responseText)));
|
xhr.addEventListener('loadend', () => res(<T>JSONAttemptParse(xhr.responseText)));
|
||||||
xhr.addEventListener('error', () => rej(JSONAttemptParse(xhr.responseText)));
|
xhr.addEventListener('error', () => rej(JSONAttemptParse(xhr.responseText)));
|
||||||
xhr.addEventListener('timeout', () => rej({error: 'Request timed out'}));
|
xhr.addEventListener('timeout', () => rej({error: 'Request timed out'}));
|
||||||
|
|
||||||
xhr.open('POST', options.url);
|
xhr.open('POST', options.url);
|
||||||
Object.entries(options.headers || {}).forEach(([key, value]) => xhr.setRequestHeader(key, value));
|
Object.entries(options.headers || {}).forEach(([key, value]) => xhr.setRequestHeader(key, value));
|
||||||
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);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user