The Spotify track ID to retrieve
Promise resolving to the track data or null if not found
export async function getTrack(trackId: string): Promise<SpotifyTrack | null> {
await ensureValidToken();
try {
const response = await retryApiCall(async () => {
return await spotifyAxios.get(`${API_BASE_URL}/tracks/${trackId}`, {
headers: {
Authorization: `Bearer ${getAccessToken()}`,
},
});
});
return response.data as SpotifyTrack;
} catch (error: unknown) {
const err = error as Error & AxiosErrorResponse;
// Don't log 404 errors as they just mean the track wasn't found
if (err.response && err.response.status === 404) {
return null;
}
saveLog(`Failed to get track ${trackId}: ${err.message}`, "ERROR");
throw new Error(`Failed to get track ${trackId}: ${err.message}`);
}
}
Gets a specific track by its Spotify ID