spotify controls
This commit is contained in:
128
src/lib/spotify/client.ts
Normal file
128
src/lib/spotify/client.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
import type {
|
||||
SpotifyDevice,
|
||||
SpotifyDevicesResponse,
|
||||
SpotifyPlaybackState,
|
||||
SpotifyError,
|
||||
} from './types';
|
||||
|
||||
const SPOTIFY_API_BASE = 'https://api.spotify.com/v1';
|
||||
|
||||
export class SpotifyWebApiClient {
|
||||
private accessToken: string;
|
||||
|
||||
constructor(accessToken: string) {
|
||||
this.accessToken = accessToken;
|
||||
}
|
||||
|
||||
private async request<T>(
|
||||
endpoint: string,
|
||||
options: RequestInit = {}
|
||||
): Promise<T> {
|
||||
const url = `${SPOTIFY_API_BASE}${endpoint}`;
|
||||
|
||||
const response = await fetch(url, {
|
||||
...options,
|
||||
headers: {
|
||||
'Authorization': `Bearer ${this.accessToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
...options.headers,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
try {
|
||||
const error: SpotifyError = await response.json();
|
||||
throw new Error(`Spotify API Error: ${error.error?.message || 'Unknown error'}`);
|
||||
} catch (parseError) {
|
||||
throw new Error(`Spotify API Error: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (response.status === 204 || response.status === 202) {
|
||||
return {} as T;
|
||||
}
|
||||
|
||||
const contentLength = response.headers.get('content-length');
|
||||
if (contentLength === '0') {
|
||||
return {} as T;
|
||||
}
|
||||
|
||||
const contentType = response.headers.get('content-type') || '';
|
||||
if (!contentType.includes('application/json')) {
|
||||
console.warn('Non-JSON response from Spotify API:', contentType, response.status);
|
||||
return {} as T;
|
||||
}
|
||||
|
||||
try {
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.warn('Failed to parse Spotify API JSON response:', error);
|
||||
return {} as T;
|
||||
}
|
||||
}
|
||||
|
||||
async getDevices(): Promise<SpotifyDevice[]> {
|
||||
const response = await this.request<SpotifyDevicesResponse>('/me/player/devices');
|
||||
return response.devices;
|
||||
}
|
||||
|
||||
async transferPlayback(deviceId: string, play: boolean = false): Promise<void> {
|
||||
await this.request('/me/player', {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify({
|
||||
device_ids: [deviceId],
|
||||
play,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
async getPlaybackState(): Promise<SpotifyPlaybackState | null> {
|
||||
try {
|
||||
return await this.request<SpotifyPlaybackState>('/me/player');
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message.includes('204')) {
|
||||
return null;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async play(deviceId?: string): Promise<void> {
|
||||
const endpoint = deviceId ? `/me/player/play?device_id=${deviceId}` : '/me/player/play';
|
||||
await this.request(endpoint, {
|
||||
method: 'PUT',
|
||||
});
|
||||
}
|
||||
|
||||
async pause(): Promise<void> {
|
||||
await this.request('/me/player/pause', {
|
||||
method: 'PUT',
|
||||
});
|
||||
}
|
||||
|
||||
async skipToNext(): Promise<void> {
|
||||
await this.request('/me/player/next', {
|
||||
method: 'POST',
|
||||
});
|
||||
}
|
||||
|
||||
async skipToPrevious(): Promise<void> {
|
||||
await this.request('/me/player/previous', {
|
||||
method: 'POST',
|
||||
});
|
||||
}
|
||||
|
||||
async setVolume(volumePercent: number): Promise<void> {
|
||||
await this.request(`/me/player/volume?volume_percent=${volumePercent}`, {
|
||||
method: 'PUT',
|
||||
});
|
||||
}
|
||||
|
||||
async getCurrentUser(): Promise<{ id: string; display_name: string }> {
|
||||
return this.request<{ id: string; display_name: string }>('/me');
|
||||
}
|
||||
|
||||
updateAccessToken(accessToken: string): void {
|
||||
this.accessToken = accessToken;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user