import { Song, Playlist } from '../types/interfaces'; const API_URL = 'http://localhost:3000/api'; async function handleResponse(response: Response): Promise { if (!response.ok) { const error = await response.json().catch(() => ({ message: 'An error occurred' })); throw new Error(error.message || 'An error occurred'); } return response.json(); } export const api = { async getSongs(): Promise { console.log('Fetching songs from API...'); const response = await fetch(`${API_URL}/songs`); const data = await handleResponse(response); console.log(`Received ${data.length} songs from API`); return data; }, async saveSongs(songs: Song[]): Promise { console.log(`Saving ${songs.length} songs to API...`); const response = await fetch(`${API_URL}/songs/batch`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(songs), }); const data = await handleResponse(response); console.log(`Successfully saved ${data.length} songs`); return data; }, async getPlaylists(): Promise { console.log('Fetching playlists from API...'); const response = await fetch(`${API_URL}/playlists`); const data = await handleResponse(response); console.log(`Received ${data.length} playlists from API`); return data; }, async savePlaylists(playlists: Playlist[]): Promise { console.log(`Saving ${playlists.length} playlists to API...`); const response = await fetch(`${API_URL}/playlists/batch`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(playlists), }); const data = await handleResponse(response); console.log(`Successfully saved ${data.length} playlists`); return data; }, };