Skip to content

Commit

Permalink
Spotify Player: Fix Search command
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaslombart committed Nov 22, 2024
1 parent d9e9ad2 commit 183fa26
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
6 changes: 5 additions & 1 deletion extensions/spotify-player/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Spotify Player Changelog

## [Fix Search Command] - 2024-11-22

- Even though it's not documented, the Spotify API can return null items in some cases when searching for items. This has now been fixed.

## [Minor Fixes] - 2024-09-20

- Fixed an issue when "Nothing is playing" popped up after commands `next`, `previous` and `like` having `Current Track` command disabled
Expand Down Expand Up @@ -39,7 +43,7 @@

## [New Album Actions] - 2024-05-30

- Added new actions in the album panel: `Add To Library` and `Remove From Library`.
- Added new actions in the album panel: `Add To Library` and `Remove From Library`.

## [Automatically Trigger Current Track] - 2024-05-28

Expand Down
18 changes: 17 additions & 1 deletion extensions/spotify-player/src/api/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,30 @@ import { getSpotifyClient } from "../helpers/withSpotifyClient";

type SearchProps = { query: string; limit?: number };

function filterNullItems<T>(category: { items?: (T | null)[] } | undefined) {
if (!category?.items) return undefined;
return {
...category,
items: category.items.filter((item) => item !== null),
};
}

export async function search({ query, limit = 50 }: SearchProps) {
const { spotifyClient } = getSpotifyClient();

try {
const response = await spotifyClient.search(query, ["track", "artist", "album", "playlist", "show", "episode"], {
limit,
});
return response;

return {
tracks: filterNullItems(response.tracks),
artists: filterNullItems(response.artists),
albums: filterNullItems(response.albums),
playlists: filterNullItems(response.playlists),
shows: filterNullItems(response.shows),
episodes: filterNullItems(response.episodes),
};
} catch (err) {
const error = getErrorMessage(err);
console.log("search.ts Error:", error);
Expand Down

0 comments on commit 183fa26

Please sign in to comment.