Whether to include additional playback state details
Promise resolving to playback state or null if not playing
export async function getCurrentPlayback(
detailed: boolean = false,
): Promise<SpotifyPlaybackState | null> {
await ensureValidToken();
try {
const response = await retryApiCall(async () => {
return await spotifyAxios.get(
`${API_BASE_URL}/me/player${detailed ? "?additional_types=episode" : ""}`,
{
headers: {
Authorization: `Bearer ${getAccessToken()}`,
},
},
);
}, 3);
// Spotify returns 204 No Content if user is not playing anything
if (response.status === 204 || !response.data) {
return null;
}
return response.data as SpotifyPlaybackState;
} catch (error: unknown) {
const err = error as Error & AxiosErrorResponse;
// Don't log 4xx errors as they're usually expected
if (
err.response &&
err.response.status >= 400 &&
err.response.status < 500
) {
return null;
}
saveLog(`Failed to get playback state: ${err.message}`, "ERROR");
throw new Error(`Failed to get playback state: ${err.message}`);
}
}
Gets the user's current playback state