81 lines
3.6 KiB
TypeScript
81 lines
3.6 KiB
TypeScript
import { ApiError, request } from './client';
|
|
import type { components } from './generated/v1';
|
|
|
|
export type AddItemsToPlaylistRequest = components['schemas']['AddItemsToPlaylistRequest'];
|
|
|
|
export type PlaylistGroup = components['schemas']['PlaylistGroupResponseModel'];
|
|
export type Playlist = components['schemas']['PlaylistResponseModel'];
|
|
export type PlaylistItem = components['schemas']['PlaylistItemResponseModel'];
|
|
export type PlaylistItemRequest = components['schemas']['PlaylistItemRequest'];
|
|
export type PlaylistPreviewItem = components['schemas']['PlaylistPreviewItemResponseModel'];
|
|
export type CreatePlaylistGroupRequest = components['schemas']['CreatePlaylistGroupRequest'];
|
|
export type UpdatePlaylistGroupRequest = components['schemas']['UpdatePlaylistGroupRequest'];
|
|
export type CreatePlaylistRequest = components['schemas']['CreatePlaylistRequest'];
|
|
export type ReplacePlaylistRequest = components['schemas']['ReplacePlaylistRequest'];
|
|
|
|
export function getPlaylistGroups(): Promise<PlaylistGroup[]> {
|
|
return request<PlaylistGroup[]>('/api/playlists/groups');
|
|
}
|
|
|
|
export function createPlaylistGroup(body: CreatePlaylistGroupRequest): Promise<PlaylistGroup> {
|
|
return request<PlaylistGroup>('/api/playlists/groups', { body, method: 'POST' });
|
|
}
|
|
|
|
export function updatePlaylistGroup(id: number, body: UpdatePlaylistGroupRequest): Promise<PlaylistGroup> {
|
|
return request<PlaylistGroup>(`/api/playlists/groups/${id}`, { body, method: 'PUT' });
|
|
}
|
|
|
|
export function deletePlaylistGroup(id: number): Promise<void> {
|
|
return request<void>(`/api/playlists/groups/${id}`, { method: 'DELETE' });
|
|
}
|
|
|
|
export function getPlaylists(playlistGroupId: number): Promise<Playlist[]> {
|
|
return request<Playlist[]>(`/api/playlists?playlistGroupId=${playlistGroupId}`);
|
|
}
|
|
|
|
export function getPlaylistById(id: number): Promise<Playlist> {
|
|
return request<Playlist>(`/api/playlists/${id}`);
|
|
}
|
|
|
|
export function getPlaylistItems(id: number): Promise<PlaylistItem[]> {
|
|
return request<PlaylistItem[]>(`/api/playlists/${id}/items`);
|
|
}
|
|
|
|
export function createPlaylist(body: CreatePlaylistRequest): Promise<Playlist> {
|
|
return request<Playlist>('/api/playlists', { body, method: 'POST' });
|
|
}
|
|
|
|
// PUT = rename + replace the full item list; returns the persisted (re-indexed) items.
|
|
export function updatePlaylist(id: number, body: ReplacePlaylistRequest): Promise<PlaylistItem[]> {
|
|
return request<PlaylistItem[]>(`/api/playlists/${id}`, { body, method: 'PUT' });
|
|
}
|
|
|
|
export function deletePlaylist(id: number): Promise<void> {
|
|
return request<void>(`/api/playlists/${id}`, { method: 'DELETE' });
|
|
}
|
|
|
|
export function previewPlaylist(body: ReplacePlaylistRequest): Promise<PlaylistPreviewItem[]> {
|
|
return request<PlaylistPreviewItem[]>('/api/playlists/preview', { body, method: 'POST' });
|
|
}
|
|
|
|
// Adds media items (bucketed by kind) to an existing playlist. The request body is the same
|
|
// ten-array shape as AddItemsToCollectionRequest; the server returns 204 on success, 404 for a
|
|
// missing playlist, 422 for validation failures. AddItemsToPlaylistRequest is the generated type
|
|
// for this endpoint's body, but the collection-shaped request is structurally identical, so
|
|
// callers can pipe toAddItemsRequest / toAddItemsRequestFromSearch output straight in.
|
|
export function addItemsToPlaylist(playlistId: number, body: AddItemsToPlaylistRequest): Promise<void> {
|
|
return request<void>(`/api/playlists/${playlistId}/items`, { body, method: 'POST' });
|
|
}
|
|
|
|
export function messageFromPlaylistError(error: unknown, fallback = 'Unable to load playlists'): string {
|
|
if (error instanceof ApiError) {
|
|
return error.detail ?? error.message;
|
|
}
|
|
|
|
if (error instanceof Error) {
|
|
return error.message;
|
|
}
|
|
|
|
return fallback;
|
|
}
|