refactor(frontend): remove dead code
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type {VideoMetadata, APIResponse, User, Clip, JobResponse } from "./types.ts";
|
||||
import type { VideoMetadata, APIResponse, User, Clip, JobResponse } from "./types.ts";
|
||||
|
||||
const API_URL = import.meta.env.VITE_API_URL;
|
||||
|
||||
@@ -6,57 +6,34 @@ export class AuthError extends Error {
|
||||
constructor() { super("Not authenticated"); this.name = "AuthError"; }
|
||||
}
|
||||
|
||||
/**
|
||||
* Login function
|
||||
* @param GoogleToken - The Google token received from the frontend.
|
||||
* @return A promise that resolves to a JWT
|
||||
*/
|
||||
const login = async (GoogleToken: string): Promise<string> => {
|
||||
const login = async (googleToken: string): Promise<string> => {
|
||||
const response = await fetch(API_URL + '/api/v1/users/login', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ token: GoogleToken }),
|
||||
credentials: 'include'
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ token: googleToken }),
|
||||
credentials: 'include',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Login failed: ${response.status}`);
|
||||
}
|
||||
if (!response.ok) throw new Error(`Login failed: ${response.status}`);
|
||||
|
||||
const result: APIResponse = await response.json();
|
||||
|
||||
if (result.status === "error") {
|
||||
throw new Error(`Login failed: ${result.message}`);
|
||||
}
|
||||
if (result.status === 'error') throw new Error(`Login failed: ${result.message}`);
|
||||
|
||||
return result.data.token;
|
||||
}
|
||||
};
|
||||
|
||||
const logout = async () => {
|
||||
const logout = async (): Promise<void> => {
|
||||
const response = await fetch(API_URL + '/api/v1/users/logout', {
|
||||
method: 'POST',
|
||||
credentials: 'include'
|
||||
credentials: 'include',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Logout failed: ${response.status}`);
|
||||
}
|
||||
if (!response.ok) throw new Error(`Logout failed: ${response.status}`);
|
||||
|
||||
const result: APIResponse = await response.json();
|
||||
if (result.status === 'error') throw new Error(`Logout failed: ${result.message}`);
|
||||
};
|
||||
|
||||
if (result.status === "error") {
|
||||
throw new Error(`Logout failed: ${result.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Uploads a file and starts compression in one step.
|
||||
* @param file - The file to compress.
|
||||
* @param options - The clip/compression options.
|
||||
* @returns The job UUID to poll for progress.
|
||||
*/
|
||||
const compress = async (file: File, options: VideoMetadata): Promise<string> => {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
@@ -75,114 +52,69 @@ const compress = async (file: File, options: VideoMetadata): Promise<string> =>
|
||||
credentials: 'include',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Compression failed: ${response.status}`);
|
||||
}
|
||||
if (!response.ok) throw new Error(`Compression failed: ${response.status}`);
|
||||
|
||||
const result: APIResponse = await response.json();
|
||||
|
||||
if (result.status === 'error') {
|
||||
throw new Error(`Compression failed: ${result.message}`);
|
||||
}
|
||||
if (result.status === 'error') throw new Error(`Compression failed: ${result.message}`);
|
||||
|
||||
return result.data.uuid;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetches the status of a background job.
|
||||
* @param uuid - The job UUID.
|
||||
*/
|
||||
const getJob = async (uuid: string): Promise<JobResponse> => {
|
||||
const response = await fetch(API_URL + `/api/v1/jobs/${uuid}`, { credentials: 'include' });
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch job: ${response.status}`);
|
||||
}
|
||||
if (!response.ok) throw new Error(`Failed to fetch job: ${response.status}`);
|
||||
|
||||
const result: APIResponse = await response.json();
|
||||
|
||||
if (result.status === 'error') {
|
||||
throw new Error(`Failed to fetch job: ${result.message}`);
|
||||
}
|
||||
if (result.status === 'error') throw new Error(`Failed to fetch job: ${result.message}`);
|
||||
|
||||
return result.data;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetches the current user information. Returns null if not authenticated.
|
||||
*/
|
||||
const getUser = async (): Promise<null | User > => {
|
||||
const response = await fetch(API_URL + '/api/v1/users/me', {credentials: "include"});
|
||||
const getUser = async (): Promise<User | null> => {
|
||||
const response = await fetch(API_URL + '/api/v1/users/me', { credentials: 'include' });
|
||||
if (!response.ok) return null;
|
||||
|
||||
if (!response.ok) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
if (result.status === "error") {
|
||||
return null;
|
||||
}
|
||||
|
||||
console.log(result.data);
|
||||
const result: APIResponse = await response.json();
|
||||
if (result.status === 'error') return null;
|
||||
|
||||
return result.data;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetches all clips for the current user.
|
||||
*/
|
||||
const getClips = async (): Promise<Clip[]> => {
|
||||
const response = await fetch(API_URL + '/api/v1/clips', { credentials: 'include'});
|
||||
const response = await fetch(API_URL + '/api/v1/clips', { credentials: 'include' });
|
||||
|
||||
if (!response.ok) {
|
||||
const errorResult: APIResponse = await response.json();
|
||||
throw new Error(`Failed to fetch clips: ${errorResult.message}`);
|
||||
}
|
||||
|
||||
try {
|
||||
const result: APIResponse = await response.json();
|
||||
return result.data;
|
||||
} catch {
|
||||
throw new Error('Failed to parse response');
|
||||
}
|
||||
}
|
||||
const result: APIResponse = await response.json();
|
||||
return result.data;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetches a clip by its ID.
|
||||
* @param id
|
||||
*/
|
||||
const getClipById = async (id: string): Promise<Clip | null> => {
|
||||
const response = await fetch(API_URL + `/api/v1/clips/${id}`, {credentials: "include",});
|
||||
const response = await fetch(API_URL + `/api/v1/clips/${id}`, { credentials: 'include' });
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 401 || response.status === 403) throw new AuthError();
|
||||
throw new Error(`Failed to fetch clip: ${response.status}`);
|
||||
}
|
||||
|
||||
try{
|
||||
const result: APIResponse = await response.json();
|
||||
return result.data;
|
||||
} catch (error: unknown) {
|
||||
throw new Error(`Failed to parse clip response:
|
||||
${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
}
|
||||
const result: APIResponse = await response.json();
|
||||
return result.data;
|
||||
};
|
||||
|
||||
const getVideoBlob = async(id: string): Promise<Blob> => {
|
||||
const response = await fetch(API_URL + `/api/v1/clips/${id}/media`, {credentials: "include",});
|
||||
const getVideoBlob = async (id: string): Promise<Blob> => {
|
||||
const response = await fetch(API_URL + `/api/v1/clips/${id}/media`, { credentials: 'include' });
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 401 || response.status === 403) throw new AuthError();
|
||||
throw new Error(`Failed to fetch video: ${id}: ${response.status}`)
|
||||
throw new Error(`Failed to fetch video ${id}: ${response.status}`);
|
||||
}
|
||||
|
||||
try {
|
||||
return response.blob();
|
||||
} catch {
|
||||
throw new Error(`Failed to convert Clip Return Object to blob`);
|
||||
}
|
||||
}
|
||||
return response.blob();
|
||||
};
|
||||
|
||||
const patchClip = async (id: number, data: { title?: string; description?: string }): Promise<Clip> => {
|
||||
const response = await fetch(API_URL + `/api/v1/clips/${id}`, {
|
||||
@@ -192,14 +124,10 @@ const patchClip = async (id: number, data: { title?: string; description?: strin
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to update clip: ${response.status}`);
|
||||
}
|
||||
if (!response.ok) throw new Error(`Failed to update clip: ${response.status}`);
|
||||
|
||||
const result: APIResponse = await response.json();
|
||||
if (result.status === 'error') {
|
||||
throw new Error(`Failed to update clip: ${result.message}`);
|
||||
}
|
||||
if (result.status === 'error') throw new Error(`Failed to update clip: ${result.message}`);
|
||||
|
||||
return result.data;
|
||||
};
|
||||
@@ -210,14 +138,10 @@ const deleteClip = async (id: number): Promise<void> => {
|
||||
credentials: 'include',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to delete clip: ${response.status}`);
|
||||
}
|
||||
if (!response.ok) throw new Error(`Failed to delete clip: ${response.status}`);
|
||||
|
||||
const result: APIResponse = await response.json();
|
||||
if (result.status === 'error') {
|
||||
throw new Error(`Failed to delete clip: ${result.message}`);
|
||||
}
|
||||
if (result.status === 'error') throw new Error(`Failed to delete clip: ${result.message}`);
|
||||
};
|
||||
|
||||
const getStreamStatus = async (): Promise<boolean> => {
|
||||
@@ -228,14 +152,13 @@ const getStreamStatus = async (): Promise<boolean> => {
|
||||
};
|
||||
|
||||
const isThumbnailAvailable = async (id: number): Promise<boolean> => {
|
||||
const response = await fetch(API_URL + `/api/v1/clips/${id}/thumbnail`, {credentials: "include"});
|
||||
const response = await fetch(API_URL + `/api/v1/clips/${id}/thumbnail`, { credentials: 'include' });
|
||||
return response.ok;
|
||||
}
|
||||
};
|
||||
|
||||
export {
|
||||
login,
|
||||
logout,
|
||||
getStreamStatus,
|
||||
compress,
|
||||
getJob,
|
||||
getUser,
|
||||
@@ -244,5 +167,6 @@ export {
|
||||
getVideoBlob,
|
||||
patchClip,
|
||||
deleteClip,
|
||||
isThumbnailAvailable
|
||||
};
|
||||
getStreamStatus,
|
||||
isThumbnailAvailable,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user