Spotify track ID to check
Whether to suppress error logs
Promise resolving to true if track is in library
export async function isTrackInLibrary(
trackId: string,
silent: boolean = false,
): Promise<boolean> {
await ensureValidToken();
try {
const response = await retryApiCall(async () => {
return await spotifyAxios.get(
`${API_BASE_URL}/me/tracks/contains?ids=${trackId}`,
{
headers: {
Authorization: `Bearer ${getAccessToken()}`,
},
},
);
});
// Response is an array of booleans, one for each track ID
return response.data[0] === true;
} catch (error: unknown) {
const err = error as Error;
if (!silent) {
saveLog(
`Failed to check if track is in library: ${err.message}`,
"ERROR",
);
}
return false;
}
}
Checks if a track is in the user's Spotify library