import { type ApiResponse, type UploadResponse } from "@/types"; const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || ""; /** * Base API client with error handling */ async function apiClient( endpoint: string, options?: RequestInit ): Promise> { try { const response = await fetch(`${API_BASE_URL}/api${endpoint}`, { ...options, headers: { "Content-Type": "application/json", ...options?.headers, }, }); const data = await response.json(); if (!response.ok) { return { success: false, error: data.message || "An error occurred", }; } return { success: true, data, }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : "Network error", }; } } /** * Upload a file */ export async function uploadFile(file: File, _onProgress?: (progress: number) => Promise): Promise> { const formData = new FormData(); formData.append("file", file); try { const response = await fetch(`${API_BASE_URL}/api/upload`, { method: "POST", body: formData, }); if (!response.ok) { throw new Error("Upload failed"); } const data = await response.json(); return { success: true, data, }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : "Upload failed", }; } } /** * Process video to frames */ export async function processVideoFrames(fileId: string, config: any) { return apiClient("/process/video-frames", { method: "POST", body: JSON.stringify({ fileId, config }), }); } /** * Process image compression */ export async function processImageCompression(fileId: string, config: any) { return apiClient("/process/image-compress", { method: "POST", body: JSON.stringify({ fileId, config }), }); } /** * Process audio compression */ export async function processAudioCompression(fileId: string, config: any) { return apiClient("/process/audio-compress", { method: "POST", body: JSON.stringify({ fileId, config }), }); } /** * Download processed file */ export async function downloadFile(fileId: string): Promise { const response = await fetch(`${API_BASE_URL}/api/download/${fileId}`); if (!response.ok) { throw new Error("Download failed"); } return response.blob(); } /** * Check user quota */ export async function checkQuota() { return apiClient("/quota", { method: "GET", }); }