REFACTOR http requests in frontend

This commit is contained in:
2025-07-14 23:06:27 +02:00
parent 416da3281a
commit 0fbb68fdb7
4 changed files with 138 additions and 141 deletions

View File

@@ -34,30 +34,32 @@ const ClipEdit = () => {
const sendData = async() => {
if (!id) return;
setDownloadable(false);
editFile(id, outputMetadata)
.then(() => {
const edited = await editFile(id, outputMetadata, setError);
processFile(id)
.catch((err: Error) => setError(`Failed to process file: ${err.message}`));
if (!edited) {
return;
})
.catch((err: Error) => setError(`Failed to edit file: ${err.message}`));
const interval = setInterval(async() => await pollProgress(id, interval), 500);
}
const processed = await processFile(id, setError);
if (!processed) {
return;
}
const interval = setInterval(async () => {
const progress = await getProgress(id);
const pollProgress = async (id: string, intervalId: number) => {
getProgress(id)
.then((progress) => {
setProgress(progress);
if (progress >= 1) {
clearInterval(interval);
clearInterval(intervalId);
setDownloadable(true);
console.log("Downloadable");
}
}, 500);
})
.catch((err: Error) => {
setError(`Failed to fetch progress: ${err.message}`);
clearInterval(intervalId);
});
}
const handleDownload = async () => {

View File

@@ -10,13 +10,14 @@ const clipUpload = () => {
const [error, setError] = useState<null | string>(null);
const press = (() => {
if (file) {
uploadFile(file, setError)
.then(uuid => navigate(`/create/${uuid}`))
.catch(e => console.error(e));
} else {
if (!file) {
setError("Please choose a file");
return;
}
uploadFile(file)
.then(uuid => navigate(`/create/${uuid}`))
.catch((e: Error) => setError(`Failed to upload file: ${e.message}`));
});
return (

View File

@@ -8,7 +8,9 @@ const MyClips = () => {
const [error, setError] = useState<null | string>(null);
useEffect(() => {
getClips(setError).then((data) => setClips(data));
getClips()
.then((data) => setClips(data))
.catch((err) => setError(err));
}, []);
return (

View File

@@ -2,12 +2,12 @@ import type {VideoMetadata, APIResponse, User, Clip} from "./types.ts";
/**
* Uploads a file to the backend.
* @param file - The file to upload.
*/
const uploadFile = async (file: File, setError: Function): Promise<string> => {
const uploadFile = async (file: File): Promise<string> => {
const formData = new FormData();
formData.append('file', file);
try {
const response = await fetch('/api/v1/upload', {
method: 'POST',
body: formData,
@@ -16,22 +16,18 @@ const uploadFile = async (file: File, setError: Function): Promise<string> => {
const result: APIResponse = await response.json();
if (result.status == "error") {
setError(result.message);
throw new Error(`Failed to upload file: ${result.message}`);
}
return result.data.uuid;
} catch (error: unknown) {
throw new Error(`Failed to upload file: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
};
/**
* Submits metadata changes to the backend.
* @param uuid - The UUID of the video file to edit.
* @param videoMetadata - The metadata to update.
*/
const editFile = async (
uuid: string,
videoMetadata: VideoMetadata,
setError: Function ): Promise<boolean> => {
const editFile = async (uuid: string, videoMetadata: VideoMetadata) => {
const formData = new URLSearchParams();
for (const [key, value] of Object.entries(videoMetadata)) {
@@ -40,7 +36,6 @@ const editFile = async (
}
}
try {
const response = await fetch(`/api/v1/edit/${uuid}`, {
method: 'POST',
headers: {
@@ -49,68 +44,65 @@ const editFile = async (
body: formData.toString(),
});
if (!response.ok) {
throw new Error(`Failed to edit file: ${response.status}`);
}
const result: APIResponse = await response.json();
if (result.status === "error") {
setError(result.message);
return false;
throw new Error(`Failed to edit file: ${result.message}`);
}
return true;
} catch (error: unknown) {
console.error('Error editing file:', error);
return false;
}
};
/**
* Triggers file processing.
* @param uuid - The UUID of the video file to process.
*/
const processFile = async (
uuid: string,
setError: Function): Promise<boolean> => {
try {
const processFile = async (uuid: string) => {
const response = await fetch(`/api/v1/process/${uuid}`);
const result: APIResponse = await response.json();
if (result.status === "error") {
setError(result.message);
return false;
if (!response.ok) {
throw new Error(`Failed to process file: ${response.status}`);
}
return response.ok;
} catch (error: unknown) {
console.error('Error processing file:', error);
return false;
const result: APIResponse = await response.json();
if (result.status === "error") {
throw new Error("Failed to process file: " + result.message);
}
};
/**
* Fetches the processing progress percentage.
* @param uuid - The UUID of the video file.
*/
const getProgress = async (
uuid: string): Promise<number> => {
try {
const getProgress = async (uuid: string): Promise<number> => {
const response = await fetch(`/api/v1/progress/${uuid}`);
if (!response.ok) {
console.error('Failed to fetch progress:', response.status);
return 0;
throw new Error(`Failed to fetch progress: ${response.status}`);
}
const result = await response.json();
return result.data?.progress ?? 0;
} catch (error: unknown) {
console.error('Error getting progress:', error);
return 0;
if (result.status === "error") {
throw new Error(`Failed to fetch progress: ${result.message}`);
}
if (!result.data || typeof result.data.progress !== 'number') {
throw new Error('Invalid progress data received');
}
return result.data.progress;
};
/**
* Fetches original metadata from the backend.
* @param uuid - The UUID of the video file.
*/
const getMetadata = async (
uuid: string): Promise<VideoMetadata> => {
try {
const getMetadata = async (uuid: string): Promise<VideoMetadata> => {
const response = await fetch(`/api/v1/metadata/original/${uuid}`);
if (!response.ok) {
@@ -118,69 +110,69 @@ const getMetadata = async (
}
const result = await response.json();
return result.data;
} catch (error: unknown) {
console.error('Error fetching metadata:', error);
return {
title: '',
description: '',
startPoint: 0,
endPoint: 0,
fps: 0,
width: 0,
height: 0,
fileSize: 0,
};
if (result.status === "error") {
throw new Error(`Failed to fetch metadata: ${result.message}`);
}
return result.data;
};
/**
* Fetches the current user information. Returns null if not authenticated.
*/
const getUser = async (): Promise<null | User > => {
try {
const response = await fetch('/api/v1/auth/user', {credentials: "include",});
const result = await response.json();
return result.data;
} catch (error: unknown) {
console.error('Error fetching user:', error);
if (!response.ok) {
return null;
}
const result = await response.json();
if (result.status === "error") {
return null;
}
return result.data;
}
const getClips = async (setError: Function): Promise< Clip[]> => {
try {
const response = await fetch('/api/v1/clips/', {credentials: "include",});
/**
* Fetches all clips for the current user.
*/
const getClips = async (): Promise<Clip[]> => {
const response = await fetch('/api/v1/clips/', { credentials: 'include' });
if (!response.ok) {
const errorResult: APIResponse = await response.json();
setError(errorResult.message);
return [];
throw new Error(`Failed to fetch clips: ${errorResult.message}`);
}
try {
const result: APIResponse = await response.json();
return result.data;
} catch (error: unknown) {
console.error('Error fetching clips:', error);
setError('Failed to fetch clips');
return [];
} catch {
throw new Error('Failed to parse response');
}
}
/**
* Fetches a clip by its ID.
* @param id
*/
const getClipById = async (id: string): Promise<Clip | null> => {
try {
const response = await fetch(`/api/v1/clips/${id}`, {credentials: "include",});
if (!response.ok) {
console.error('Failed to fetch clip:', response.status);
return null;
throw new Error(`Failed to fetch clip: ${response.status}`);
}
try{
const result: APIResponse = await response.json();
return result.data;
} catch (error: unknown) {
console.error('Error fetching clip:', error);
return null;
throw new Error(`Failed to parse clip response:
${error instanceof Error ? error.message : 'Unknown error'}`);
}
};