@remcostoeten/fync
Version:
A unified TypeScript library for easy access to popular APIs (GitHub, Spotify, GitLab, etc.)
143 lines • 4.73 kB
JavaScript
import { createSpotifyAuth, isTokenExpired, SPOTIFY_SCOPES, shouldRefreshToken, } from "./auth";
import { createSpotifyClient as createSpotifyChainableClient } from "./services/spotify-client";
function createUserClient(api, userId) {
return {
get: () => api.users[userId].get(),
playlists: {
get: (options) => api.users[userId].playlists.get(options),
},
chain: api.users[userId],
};
}
function createAuthenticatedUserClient(api) {
return {
get: () => api.me.get(),
playlists: {
get: (options) => api.me.playlists.get(options),
},
tracks: {
get: (options) => api.me.tracks.get(options),
},
albums: {
get: (options) => api.me.albums.get(options),
},
artists: {
get: (options) => api.me.following.get({
...options,
params: { type: "artist", ...options?.params },
}),
},
recentlyPlayed: {
get: (options) => api.me.player.recently_played.get(options),
},
chain: api.me,
};
}
function createPlayerClient(api) {
return {
devices: () => api.me.player.devices.get(),
currentlyPlaying: () => api.me.player.get(),
chain: api.me.player,
};
}
function createPlaylistClient(api, playlistId) {
return {
get: () => api.playlists[playlistId].get(),
tracks: {
get: (options) => api.playlists[playlistId].tracks.get(options),
},
chain: api.playlists[playlistId],
};
}
function createSearchClient(api) {
return {
tracks: (query, options) => api.search.get({
...options,
params: { q: query, type: "track", ...options?.params },
}),
artists: (query, options) => api.search.get({
...options,
params: { q: query, type: "artist", ...options?.params },
}),
albums: (query, options) => api.search.get({
...options,
params: { q: query, type: "album", ...options?.params },
}),
playlists: (query, options) => api.search.get({
...options,
params: { q: query, type: "playlist", ...options?.params },
}),
shows: (query, options) => api.search.get({
...options,
params: { q: query, type: "show", ...options?.params },
}),
episodes: (query, options) => api.search.get({
...options,
params: { q: query, type: "episode", ...options?.params },
}),
chain: api.search,
};
}
function createLibraryClient(api) {
return {
savedTracks: {
get: (options) => api.me.tracks.get(options),
},
savedAlbums: {
get: (options) => api.me.albums.get(options),
},
savedShows: {
get: (options) => api.me.shows.get(options),
},
savedEpisodes: {
get: (options) => api.me.episodes.get(options),
},
chain: api.me,
};
}
/**
* Creates a new Spotify Web API client
*
* @param config - Configuration for the Spotify client
* @param config.token - Spotify access token from OAuth 2.0 flow (required)
* @param config.baseUrl - Custom Spotify API URL (default: https://api.spotify.com/v1)
* @param config.cache - Enable response caching (default: false)
* @param config.cacheTTL - Cache time-to-live in milliseconds (default: 300000)
* @param config.timeout - Request timeout in milliseconds (default: 30000)
*
* @returns Spotify client instance with access to user data, playlists, player controls, and search
*
* @example
* ```typescript
* const spotify = Spotify({
* token: process.env.SPOTIFY_ACCESS_TOKEN,
* cache: true
* });
*
* // Get current user profile
* const user = await spotify.me.get();
*
* // Search for tracks
* const tracks = await spotify.search.tracks('bohemian rhapsody');
*
* // Control playback
* await spotify.player.play({ uris: ['spotify:track:4iV5W9uYEdYUVa79Axb7Rh'] });
*
* // Get user's playlists
* const playlists = await spotify.me.playlists.get();
* ```
*/
function Spotify(config) {
const api = createSpotifyChainableClient(config);
return {
api,
user: (userId) => createUserClient(api, userId),
me: createAuthenticatedUserClient(api),
player: createPlayerClient(api),
playlist: (playlistId) => createPlaylistClient(api, playlistId),
search: createSearchClient(api),
library: createLibraryClient(api),
};
}
export { Spotify, SPOTIFY_SCOPES, createSpotifyAuth, isTokenExpired, shouldRefreshToken, };
//# sourceMappingURL=index.js.map