2025-04-25 10:42:45 +02:00

60 lines
1.7 KiB
TypeScript

import type { Song, PlaylistNode } from '../types/interfaces';
const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:3000/api';
class Api {
async getSongs(): Promise<Song[]> {
const response = await fetch(`${API_BASE_URL}/songs`);
if (!response.ok) throw new Error('Failed to fetch songs');
return response.json();
}
async saveSongs(songs: Song[]): Promise<Song[]> {
const response = await fetch(`${API_BASE_URL}/songs/batch`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(songs),
});
if (!response.ok) throw new Error('Failed to save songs');
return response.json();
}
async getPlaylists(): Promise<PlaylistNode[]> {
const response = await fetch(`${API_BASE_URL}/playlists`);
if (!response.ok) throw new Error('Failed to fetch playlists');
return response.json();
}
async savePlaylists(playlists: PlaylistNode[]): Promise<PlaylistNode[]> {
const response = await fetch(`${API_BASE_URL}/playlists/batch`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(playlists),
});
if (!response.ok) throw new Error('Failed to save playlists');
return response.json();
}
async resetDatabase(): Promise<boolean> {
try {
const response = await fetch(`${API_BASE_URL}/reset`, {
method: 'POST',
});
if (!response.ok) {
throw new Error('Failed to reset database');
}
return true;
} catch (error) {
console.error('Error resetting database:', error);
throw error;
}
}
}
export const api = new Api();