37 lines
1.3 KiB
JavaScript
37 lines
1.3 KiB
JavaScript
// Jellyfin REST API client.
|
|
|
|
export function createJellyfinClient(baseUrl, apiKey) {
|
|
const headers = {
|
|
'Authorization': `MediaBrowser Token="${apiKey}", Client="Bordanlage", Device="Dashboard", DeviceId="bordanlage-1", Version="1.0"`,
|
|
'Content-Type': 'application/json',
|
|
}
|
|
|
|
async function get(path) {
|
|
const res = await fetch(`${baseUrl}${path}`, { headers })
|
|
if (!res.ok) throw new Error(`Jellyfin ${res.status}: ${path}`)
|
|
return res.json()
|
|
}
|
|
|
|
return {
|
|
async getArtists() {
|
|
return get('/Artists?SortBy=SortName&SortOrder=Ascending&IncludeItemTypes=Audio&Recursive=true')
|
|
},
|
|
async getAlbums(artistId) {
|
|
const q = artistId ? `&ArtistIds=${artistId}` : ''
|
|
return get(`/Items?SortBy=SortName&IncludeItemTypes=MusicAlbum&Recursive=true${q}`)
|
|
},
|
|
async getTracks(albumId) {
|
|
return get(`/Items?ParentId=${albumId}&IncludeItemTypes=Audio&SortBy=IndexNumber`)
|
|
},
|
|
async search(query) {
|
|
return get(`/Items?SearchTerm=${encodeURIComponent(query)}&IncludeItemTypes=Audio,MusicAlbum,MusicArtist&Recursive=true&Limit=20`)
|
|
},
|
|
getStreamUrl(itemId) {
|
|
return `${baseUrl}/Audio/${itemId}/stream?static=true&api_key=${apiKey}`
|
|
},
|
|
getImageUrl(itemId, type = 'Primary') {
|
|
return `${baseUrl}/Items/${itemId}/Images/${type}?api_key=${apiKey}`
|
|
}
|
|
}
|
|
}
|