Skip to content
← Public packages

@kentcdodds/spotify

Spotify playback, playlist, search, library, and device helpers.

src/lib/remote-state.ts

31 lines · 962 B · TypeScript
import {
	type AccountParams,
	getAvailableDevices,
	getPlaybackState,
	normalizeDevice,
	normalizePlaybackState,
} from './spotify-api.ts'

export async function buildRemoteState(
	options: AccountParams & { message?: string } = {},
) {
	const devices = await getAvailableDevices(options)
	const playbackState = await getPlaybackState(options)
	const normalizedDevices = devices.map(normalizeDevice).filter(Boolean)
	const playback = normalizePlaybackState(playbackState.playback)
	let message = options.message ?? 'Ready.'
	if (!normalizedDevices.length) {
		message = options.message ?? 'No Spotify devices are currently available.'
	} else if (playbackState.nothingPlaying) {
		message =
			options.message ?? 'Spotify is connected, but nothing is currently playing right now.'
	}
	return {
		ok: true,
		message,
		hasActiveDevice: normalizedDevices.length > 0,
		devices: normalizedDevices,
		playback,
		nothingPlaying: playbackState.nothingPlaying,
	}
}