jukebox-vibe/web/src/api.ts

48 lines
1.6 KiB
TypeScript
Raw Normal View History

import type { Sound, SoundsResponse, VoiceChannelInfo } from './types';
2025-08-07 23:24:56 +02:00
const API_BASE = import.meta.env.VITE_API_BASE_URL || '/api';
export async function fetchSounds(q?: string, folderKey?: string): Promise<SoundsResponse> {
2025-08-07 23:24:56 +02:00
const url = new URL(`${API_BASE}/sounds`, window.location.origin);
if (q) url.searchParams.set('q', q);
if (folderKey !== undefined) url.searchParams.set('folder', folderKey);
2025-08-07 23:24:56 +02:00
const res = await fetch(url.toString());
if (!res.ok) throw new Error('Fehler beim Laden der Sounds');
return res.json();
}
export async function fetchChannels(): Promise<VoiceChannelInfo[]> {
const res = await fetch(`${API_BASE}/channels`);
if (!res.ok) throw new Error('Fehler beim Laden der Channels');
return res.json();
}
export async function playSound(soundName: string, guildId: string, channelId: string, volume: number, relativePath?: string): Promise<void> {
2025-08-07 23:24:56 +02:00
const res = await fetch(`${API_BASE}/play`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ soundName, guildId, channelId, volume, relativePath })
2025-08-07 23:24:56 +02:00
});
if (!res.ok) {
const data = await res.json().catch(() => ({}));
throw new Error(data?.error || 'Play fehlgeschlagen');
}
}
export async function setVolumeLive(guildId: string, volume: number): Promise<void> {
const res = await fetch(`${API_BASE}/volume`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ guildId, volume })
});
if (!res.ok) {
const data = await res.json().catch(() => ({}));
throw new Error(data?.error || 'Volume ändern fehlgeschlagen');
}
}
2025-08-07 23:24:56 +02:00