MCPcopy Index your code
hub / github.com/CapSoftware/Cap / uploadWithXhr

Function uploadWithXhr

apps/mobile/src/api/mobile.ts:357–393  ·  view source on GitHub ↗
(
	method: "POST" | "PUT",
	url: string,
	headers: Headers,
	body: FormData | Blob,
	onProgress?: (progress: UploadProgress) => void,
)

Source from the content-addressed store, hash-verified

355};
356
357const uploadWithXhr = (
358 method: "POST" | "PUT",
359 url: string,
360 headers: Headers,
361 body: FormData | Blob,
362 onProgress?: (progress: UploadProgress) => void,
363) =>
364 new Promise<void>((resolve, reject) => {
365 const xhr = new XMLHttpRequest();
366 xhr.open(method, url);
367 headers.forEach((value, key) => {
368 xhr.setRequestHeader(key, value);
369 });
370 xhr.upload.onprogress = (event) => {
371 onProgress?.({
372 loaded: event.loaded,
373 total: event.lengthComputable ? event.total : 0,
374 });
375 };
376 xhr.onload = () => {
377 if (xhr.status >= 200 && xhr.status < 300) {
378 resolve();
379 return;
380 }
381 reject(
382 new MobileApiError(
383 "Upload target rejected the file",
384 xhr.status,
385 xhr.responseText,
386 ),
387 );
388 };
389 xhr.onerror = () => {
390 reject(new Error("Upload failed"));
391 };
392 xhr.send(body);
393 });
394
395const fileBlob = async (file: UploadFile) => {
396 const response = await fetch(file.uri);

Callers 1

uploadToTargetFunction · 0.85

Calls 6

rejectFunction · 0.85
onProgressFunction · 0.50
resolveFunction · 0.50
openMethod · 0.45
setRequestHeaderMethod · 0.45
sendMethod · 0.45

Tested by

no test coverage detected