import { showError } from './toast'; const API_BASE = import.meta.env.DEV ? 'http://localhost:8080/api' : '/api'; // Helper function to handle API responses and show error toasts async function handleResponse(response: Response, errorMessage: string): Promise { if (!response.ok) { let message = errorMessage; try { const errorData = await response.json(); if (errorData.error) { message = errorData.error; } } catch (e) { // If we can't parse JSON, use the default message } showError(message); throw new Error(message); } return response.json(); } export interface User { id: string; email: string; username: string; } export interface AuthResponse { token: string; user: User; } export interface Ebook { id: string; title: string; author: string | null; isbn: string | null; description: string | null; file_path: string; file_size: number | null; mime_type: string | null; cover_image_path: string | null; created_at: string; updated_at: string; } export interface ReadingProgress { ebook_id: string; user_id: string; current_page: number; total_pages: number | null; last_read_at: string; } // Get stored token function getToken(): string | null { if (typeof window !== 'undefined') { return localStorage.getItem('auth_token'); } return null; } // Create headers with auth function createHeaders(): Record { const headers: Record = { 'Content-Type': 'application/json', }; const token = getToken(); if (token) { headers['Authorization'] = `Bearer ${token}`; } return headers; } // Auth functions export async function registerUser(email: string, username: string, password: string): Promise { const response = await fetch(`${API_BASE}/auth/register`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, username, password }), }); return handleResponse(response, 'Registration failed'); } export async function loginUser(login: string, password: string): Promise { const response = await fetch(`${API_BASE}/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ login, password }), }); return handleResponse(response, 'Login failed'); } export async function getUserProfile(): Promise { const response = await fetch(`${API_BASE}/auth/profile`, { headers: createHeaders(), }); return handleResponse(response, 'Failed to get profile'); } export async function fetchEbooks(limit: number = 20, offset: number = 0): Promise { const response = await fetch(`${API_BASE}/ebooks?limit=${limit}&offset=${offset}`, { headers: createHeaders(), }); return handleResponse(response, 'Failed to fetch ebooks'); } export async function fetchEbook(id: string): Promise { const response = await fetch(`${API_BASE}/ebooks/${id}`, { headers: createHeaders(), }); return handleResponse(response, 'Failed to fetch ebook'); } export async function createEbook(data: Partial): Promise { const response = await fetch(`${API_BASE}/ebooks`, { method: 'POST', headers: createHeaders(), body: JSON.stringify(data), }); return handleResponse(response, 'Failed to create ebook'); } export async function updateEbook(id: string, data: Partial): Promise { const response = await fetch(`${API_BASE}/ebooks/${id}`, { method: 'PUT', headers: createHeaders(), body: JSON.stringify(data), }); return handleResponse(response, 'Failed to update ebook'); } export async function deleteEbook(id: string): Promise { const response = await fetch(`${API_BASE}/ebooks/${id}`, { method: 'DELETE', headers: createHeaders(), }); if (!response.ok) { let message = 'Failed to delete ebook'; try { const errorData = await response.json(); if (errorData.error) { message = errorData.error; } } catch (e) { // If we can't parse JSON, use the default message } showError(message); throw new Error(message); } } export async function fetchReadingProgress(ebookId: string): Promise { const response = await fetch(`${API_BASE}/ebooks/${ebookId}/progress`, { headers: createHeaders(), }); return handleResponse(response, 'Failed to fetch reading progress'); } export async function updateReadingProgress(ebookId: string, currentPage: number, totalPages?: number): Promise { const response = await fetch(`${API_BASE}/ebooks/${ebookId}/progress`, { method: 'PUT', headers: createHeaders(), body: JSON.stringify({ current_page: currentPage, total_pages: totalPages }), }); return handleResponse(response, 'Failed to update reading progress'); }