diff --git a/web/src/lib/api/client.ts b/web/src/lib/api/client.ts index dadc2549..aa0d168e 100644 --- a/web/src/lib/api/client.ts +++ b/web/src/lib/api/client.ts @@ -58,6 +58,6 @@ export const api = { apiFetch(path, { method: 'PUT', body: JSON.stringify(body) }) as Promise, patch: (path: string, body: unknown): Promise => apiFetch(path, { method: 'PATCH', body: JSON.stringify(body) }) as Promise, - del: (path: string): Promise => - apiFetch(path, { method: 'DELETE' }) as Promise + del: (path: string): Promise => + apiFetch(path, { method: 'DELETE' }) as Promise }; diff --git a/web/src/lib/api/playlists.ts b/web/src/lib/api/playlists.ts index 31c3cf22..2b442068 100644 --- a/web/src/lib/api/playlists.ts +++ b/web/src/lib/api/playlists.ts @@ -1,5 +1,5 @@ import { createQuery } from '@tanstack/svelte-query'; -import { apiFetch } from './client'; +import { api } from './client'; import { qk } from './queries'; import type { Playlist, PlaylistDetail } from './types'; @@ -12,13 +12,11 @@ export type ListPlaylistsResponse = { export async function listPlaylists(kind?: PlaylistKind): Promise { const k = kind ?? 'user'; - return (await apiFetch(`/api/playlists?kind=${k}`, { method: 'GET' })) as ListPlaylistsResponse; + return api.get(`/api/playlists?kind=${k}`); } export async function getPlaylist(id: string): Promise { - return (await apiFetch(`/api/playlists/${encodeURIComponent(id)}`, { - method: 'GET' - })) as PlaylistDetail; + return api.get(`/api/playlists/${encodeURIComponent(id)}`); } export type CreatePlaylistInput = { @@ -28,11 +26,7 @@ export type CreatePlaylistInput = { }; export async function createPlaylist(input: CreatePlaylistInput): Promise { - return (await apiFetch('/api/playlists', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(input) - })) as Playlist; + return api.post('/api/playlists', input); } export type UpdatePlaylistInput = { @@ -42,38 +36,31 @@ export type UpdatePlaylistInput = { }; export async function updatePlaylist(id: string, input: UpdatePlaylistInput): Promise { - return (await apiFetch(`/api/playlists/${encodeURIComponent(id)}`, { - method: 'PATCH', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(input) - })) as Playlist; + return api.patch(`/api/playlists/${encodeURIComponent(id)}`, input); } export async function deletePlaylist(id: string): Promise { - await apiFetch(`/api/playlists/${encodeURIComponent(id)}`, { method: 'DELETE' }); + await api.del(`/api/playlists/${encodeURIComponent(id)}`); } export async function appendTracks(playlistID: string, trackIDs: string[]): Promise { - return (await apiFetch(`/api/playlists/${encodeURIComponent(playlistID)}/tracks`, { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ track_ids: trackIDs }) - })) as PlaylistDetail; + return api.post( + `/api/playlists/${encodeURIComponent(playlistID)}/tracks`, + { track_ids: trackIDs } + ); } export async function removePlaylistTrack(playlistID: string, position: number): Promise { - return (await apiFetch( - `/api/playlists/${encodeURIComponent(playlistID)}/tracks/${position}`, - { method: 'DELETE' } - )) as PlaylistDetail; + return api.del( + `/api/playlists/${encodeURIComponent(playlistID)}/tracks/${position}` + ); } export async function reorderPlaylist(playlistID: string, orderedPositions: number[]): Promise { - return (await apiFetch(`/api/playlists/${encodeURIComponent(playlistID)}/tracks`, { - method: 'PUT', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ ordered_positions: orderedPositions }) - })) as PlaylistDetail; + return api.put( + `/api/playlists/${encodeURIComponent(playlistID)}/tracks`, + { ordered_positions: orderedPositions } + ); } export function createPlaylistsQuery(kind?: PlaylistKind) { @@ -103,10 +90,7 @@ export type RefreshDiscoverResponse = { // returns the resulting Discover playlist's id + track count. // Used by the detail-page Refresh button and the home tile kebab. export async function refreshDiscover(): Promise { - return (await apiFetch('/api/playlists/system/discover/refresh', { - method: 'POST', - body: JSON.stringify({}) - })) as RefreshDiscoverResponse; + return api.post('/api/playlists/system/discover/refresh', {}); } // For-You refresh ------------------------------------------------------------ @@ -125,7 +109,5 @@ export type RefreshForYouResponse = { // triggers refresh, the frontend then calls getPlaylist(new_id) to // get full TrackRef snapshots and enqueues for playback. export async function refreshForYou(): Promise { - return (await apiFetch('/api/playlists/system/for-you/refresh', { - method: 'POST' - })) as RefreshForYouResponse; + return api.post('/api/playlists/system/for-you/refresh', {}); }