UNPKG

@remcostoeten/fync

Version:

A unified TypeScript library for easy access to popular APIs (GitHub, Spotify, GitLab, etc.)

198 lines (197 loc) 4.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "SPOTIFY_SCOPES", { enumerable: true, get: function () { return _auth.SPOTIFY_SCOPES; } }); exports.Spotify = Spotify; Object.defineProperty(exports, "createSpotifyAuth", { enumerable: true, get: function () { return _auth.createSpotifyAuth; } }); Object.defineProperty(exports, "isTokenExpired", { enumerable: true, get: function () { return _auth.isTokenExpired; } }); Object.defineProperty(exports, "shouldRefreshToken", { enumerable: true, get: function () { return _auth.shouldRefreshToken; } }); var _auth = require("./auth"); var _spotifyClient = require("./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 = (0, _spotifyClient.createSpotifyClient)(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) }; }