library.ts•3.73 kB
import type { SpotifyClient } from "../spotify-client.js";
import type { SpotifyTrack } from "../types.js";
export interface SavedTracksResponse {
items: Array<{
added_at: string;
track: SpotifyTrack;
}>;
total: number;
limit: number;
offset: number;
}
export interface RecentlyPlayedResponse {
items: Array<{
track: SpotifyTrack;
played_at: string;
context: {
type: string;
href: string;
external_urls: { spotify: string };
uri: string;
} | null;
}>;
next: string | null;
cursors: {
after: string;
before: string;
};
limit: number;
href: string;
}
/**
* Save a track to the user's library
*/
export async function saveTrack(
client: SpotifyClient,
track_id: string
): Promise<void> {
await client.put("/me/tracks", { ids: [track_id] });
}
/**
* Remove a track from the user's library
*/
export async function removeTrack(
client: SpotifyClient,
track_id: string
): Promise<void> {
await client.delete("/me/tracks", { ids: [track_id] });
}
/**
* Check if tracks are saved in the user's library
*/
export async function checkSavedTracks(
client: SpotifyClient,
track_ids: string[]
): Promise<boolean[]> {
const response = await client.get<boolean[]>("/me/tracks/contains", {
ids: track_ids.join(","),
});
return response;
}
/**
* Get user's saved tracks
*/
export async function getSavedTracks(
client: SpotifyClient,
limit: number = 20
): Promise<SavedTracksResponse> {
const response = await client.get<SavedTracksResponse>("/me/tracks", {
limit,
});
return response;
}
/**
* Get recently played tracks
*/
export async function getRecentlyPlayedTracks(
client: SpotifyClient,
limit: number = 20,
after?: number,
before?: number
): Promise<RecentlyPlayedResponse> {
const params: Record<string, any> = { limit };
if (after !== undefined) {
params.after = after;
}
if (before !== undefined) {
params.before = before;
}
const response = await client.get<RecentlyPlayedResponse>(
"/me/player/recently-played",
params
);
return response;
}
/**
* Format saved tracks for display
*/
export function formatSavedTracks(response: SavedTracksResponse): string {
if (!response.items || response.items.length === 0) {
return "No saved tracks found.";
}
const lines = ["**Your Saved Tracks:**", ""];
response.items.forEach((item, i) => {
const track = item.track;
const artists = track.artists.map((a) => a.name).join(", ");
const addedDate = new Date(item.added_at).toLocaleDateString();
lines.push(`${i + 1}. **${track.name}** by ${artists}`);
lines.push(` Album: ${track.album.name}`);
lines.push(` Added: ${addedDate}`);
lines.push(` URI: ${track.uri}`);
lines.push("");
});
lines.push(`Total saved tracks: ${response.total}`);
return lines.join("\n");
}
/**
* Format recently played tracks for display
*/
export function formatRecentlyPlayed(response: RecentlyPlayedResponse): string {
if (!response.items || response.items.length === 0) {
return "No recently played tracks found.";
}
const lines = ["**Your Recently Played Tracks:**", ""];
response.items.forEach((item, i) => {
const track = item.track;
const artists = track.artists.map((a) => a.name).join(", ");
const playedDate = new Date(item.played_at).toLocaleString();
lines.push(`${i + 1}. **${track.name}** by ${artists}`);
lines.push(` Album: ${track.album.name}`);
lines.push(` Played: ${playedDate}`);
if (item.context) {
lines.push(` Context: ${item.context.type}`);
}
lines.push(` URI: ${track.uri}`);
lines.push("");
});
return lines.join("\n");
}