Skip to content

Commit

Permalink
Handle subsonic endpoints that potentially return optional response w…
Browse files Browse the repository at this point in the history
…hen no items
  • Loading branch information
jeffvli committed Sep 26, 2024
1 parent 0e8b2ae commit c96f5b2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 50 deletions.
50 changes: 25 additions & 25 deletions src/renderer/api/subsonic/subsonic-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ export const SubsonicController: ControllerEndpoint = {
}

const results =
res.body.searchResult3.album?.map((album) =>
res.body.searchResult3?.album?.map((album) =>
ssNormalize.album(album, apiClientProps.server),
) || [];

Expand Down Expand Up @@ -325,14 +325,14 @@ export const SubsonicController: ControllerEndpoint = {
}

const results =
res.body.starred.album?.map((album) =>
res.body.starred?.album?.map((album) =>
ssNormalize.album(album, apiClientProps.server),
) || [];

return {
items: sortAlbumList(results, query.sortBy, query.sortOrder),
startIndex: 0,
totalRecordCount: res.body.starred.album?.length || 0,
totalRecordCount: res.body.starred?.album?.length || 0,
};
}

Expand Down Expand Up @@ -410,7 +410,7 @@ export const SubsonicController: ControllerEndpoint = {
throw new Error('Failed to get album list count');
}

const albumCount = res.body.searchResult3.album?.length;
const albumCount = (res.body.searchResult3?.album || [])?.length;

totalRecordCount += albumCount;
startIndex += albumCount;
Expand All @@ -433,7 +433,7 @@ export const SubsonicController: ControllerEndpoint = {
throw new Error('Failed to get album list');
}

return res.body.starred.album?.length || 0;
return (res.body.starred?.album || []).length || 0;
}

let type = ALBUM_LIST_SORT_MAPPING[query.sortBy] ?? AlbumListSortType.ALPHABETICAL_BY_NAME;
Expand Down Expand Up @@ -523,7 +523,7 @@ export const SubsonicController: ControllerEndpoint = {
throw new Error('Failed to get genre list');
}

let results = res.body.genres.genre;
let results = res.body.genres?.genre || [];

if (query.searchTerm) {
const searchResults = filter(results, (genre) =>
Expand Down Expand Up @@ -588,7 +588,7 @@ export const SubsonicController: ControllerEndpoint = {
throw new Error('Failed to get playlist list');
}

let results = res.body.playlists.playlist;
let results = res.body.playlists?.playlist || [];

if (query.searchTerm) {
const searchResults = filter(results, (playlist) => {
Expand Down Expand Up @@ -634,7 +634,7 @@ export const SubsonicController: ControllerEndpoint = {
throw new Error('Failed to get playlist list');
}

let results = res.body.playlists.playlist;
let results = res.body.playlists?.playlist || [];

if (query.searchTerm) {
const searchResults = filter(results, (playlist) => {
Expand Down Expand Up @@ -689,10 +689,10 @@ export const SubsonicController: ControllerEndpoint = {
throw new Error('Failed to get random songs');
}

const results = res.body.randomSongs?.song || [];

return {
items: res.body.randomSongs?.song?.map((song) =>
ssNormalize.song(song, apiClientProps.server, ''),
),
items: results.map((song) => ssNormalize.song(song, apiClientProps.server, '')),
startIndex: 0,
totalRecordCount: res.body.randomSongs?.song?.length || 0,
};
Expand Down Expand Up @@ -818,11 +818,11 @@ export const SubsonicController: ControllerEndpoint = {
throw new Error('Failed to get song list');
}

const results = res.body.songsByGenre?.song || [];

return {
items:
res.body.songsByGenre.song?.map((song) =>
ssNormalize.song(song, apiClientProps.server, ''),
) || [],
results.map((song) => ssNormalize.song(song, apiClientProps.server, '')) || [],
startIndex: 0,
totalRecordCount: null,
};
Expand All @@ -840,14 +840,14 @@ export const SubsonicController: ControllerEndpoint = {
}

const results =
res.body.starred.song?.map((song) =>
(res.body.starred?.song || []).map((song) =>
ssNormalize.song(song, apiClientProps.server, ''),
) || [];

return {
items: sortSongList(results, query.sortBy, query.sortOrder),
startIndex: 0,
totalRecordCount: res.body.starred.song?.length || 0,
totalRecordCount: (res.body.starred?.song || []).length || 0,
};
}

Expand Down Expand Up @@ -973,7 +973,7 @@ export const SubsonicController: ControllerEndpoint = {
throw new Error('Failed to get song list count');
}

const songCount = res.body.searchResult3.song?.length || 0;
const songCount = (res.body.searchResult3?.song || []).length || 0;

totalRecordCount += songCount;
startIndex += songCount;
Expand Down Expand Up @@ -1001,7 +1001,7 @@ export const SubsonicController: ControllerEndpoint = {
throw new Error('Failed to get song list count');
}

const numberOfResults = res.body.songsByGenre.song?.length || 0;
const numberOfResults = (res.body.songsByGenre?.song || []).length || 0;

if (numberOfResults !== 1) {
fetchNextSection = false;
Expand All @@ -1026,7 +1026,7 @@ export const SubsonicController: ControllerEndpoint = {
throw new Error('Failed to get song list count');
}

const numberOfResults = res.body.songsByGenre.song?.length || 0;
const numberOfResults = (res.body.songsByGenre?.song || []).length || 0;

totalRecordCount = startIndex + numberOfResults;
startIndex += numberOfResults;
Expand All @@ -1048,7 +1048,7 @@ export const SubsonicController: ControllerEndpoint = {
throw new Error('Failed to get song list');
}

return res.body.starred.song?.length || 0;
return (res.body.starred?.song || []).length || 0;
}

let totalRecordCount = 0;
Expand All @@ -1070,7 +1070,7 @@ export const SubsonicController: ControllerEndpoint = {
throw new Error('Failed to get song list count');
}

const numberOfResults = res.body.searchResult3.song?.length || 0;
const numberOfResults = (res.body.searchResult3?.song || []).length || 0;

// Check each batch of 5000 songs to check for data
sectionIndex += 5000;
Expand Down Expand Up @@ -1099,7 +1099,7 @@ export const SubsonicController: ControllerEndpoint = {
throw new Error('Failed to get song list count');
}

const numberOfResults = res.body.searchResult3.song?.length || 0;
const numberOfResults = (res.body.searchResult3?.song || []).length || 0;

totalRecordCount = startIndex + numberOfResults;
startIndex += numberOfResults;
Expand Down Expand Up @@ -1237,13 +1237,13 @@ export const SubsonicController: ControllerEndpoint = {
}

return {
albumArtists: res.body.searchResult3?.artist?.map((artist) =>
albumArtists: (res.body.searchResult3?.artist || [])?.map((artist) =>
ssNormalize.albumArtist(artist, apiClientProps.server),
),
albums: res.body.searchResult3?.album?.map((album) =>
albums: (res.body.searchResult3?.album || []).map((album) =>
ssNormalize.album(album, apiClientProps.server),
),
songs: res.body.searchResult3?.song?.map((song) =>
songs: (res.body.searchResult3?.song || []).map((song) =>
ssNormalize.song(song, apiClientProps.server, ''),
),
};
Expand Down
64 changes: 39 additions & 25 deletions src/renderer/api/subsonic/subsonic-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,11 @@ const topSongsListParameters = z.object({
});

const topSongsList = z.object({
topSongs: z.object({
song: z.array(song),
}),
topSongs: z
.object({
song: z.array(song),
})
.optional(),
});

const scrobbleParameters = z.object({
Expand All @@ -204,11 +206,13 @@ const scrobbleParameters = z.object({
const scrobble = z.null();

const search3 = z.object({
searchResult3: z.object({
album: z.array(album),
artist: z.array(albumArtist),
song: z.array(song),
}),
searchResult3: z
.object({
album: z.array(album).optional(),
artist: z.array(albumArtist).optional(),
song: z.array(song).optional(),
})
.optional(),
});

const search3Parameters = z.object({
Expand All @@ -231,9 +235,11 @@ const randomSongListParameters = z.object({
});

const randomSongList = z.object({
randomSongs: z.object({
song: z.array(song),
}),
randomSongs: z
.object({
song: z.array(song),
})
.optional(),
});

const ping = z.object({
Expand Down Expand Up @@ -310,11 +316,13 @@ const getStarredParameters = z.object({
});

const getStarred = z.object({
starred: z.object({
album: z.array(albumListEntry),
artist: z.array(artistListEntry),
song: z.array(song),
}),
starred: z
.object({
album: z.array(albumListEntry),
artist: z.array(artistListEntry),
song: z.array(song),
})
.optional(),
});

const getSongsByGenreParameters = z.object({
Expand All @@ -325,9 +333,11 @@ const getSongsByGenreParameters = z.object({
});

const getSongsByGenre = z.object({
songsByGenre: z.object({
song: z.array(song),
}),
songsByGenre: z
.object({
song: z.array(song),
})
.optional(),
});

const getAlbumParameters = z.object({
Expand Down Expand Up @@ -408,9 +418,11 @@ const playlistListEntry = playlist.omit({
});

const getPlaylists = z.object({
playlists: z.object({
playlist: z.array(playlistListEntry),
}),
playlists: z
.object({
playlist: z.array(playlistListEntry),
})
.optional(),
});

const getPlaylistParameters = z.object({
Expand All @@ -430,9 +442,11 @@ const genre = z.object({
const getGenresParameters = z.object({});

const getGenres = z.object({
genres: z.object({
genre: z.array(genre),
}),
genres: z
.object({
genre: z.array(genre),
})
.optional(),
});

export enum AlbumListSortType {
Expand Down

0 comments on commit c96f5b2

Please sign in to comment.