UNPKG

spotify-service-rg

Version:

This is a Angular service for connecting to and using Spotify API.

390 lines (384 loc) 13.3 kB
import { Injectable, NgModule, defineInjectable, inject } from '@angular/core'; import { HttpClient, HttpClientModule } from '@angular/common/http'; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class SpotifyRgService { /** * @param {?} http */ constructor(http) { this.http = http; this.baseUrl = "https://api.spotify.com/v1"; } /** * @return {?} */ login() { window.location.href = 'https://accounts.spotify.com/authorize' + '?response_type=token' + '&client_id=' + this.clientId + (this.scopes ? '&scope=' + encodeURIComponent(this.scopes) : '') + '&redirect_uri=' + encodeURIComponent(this.callBackUrl); } //Setters for the configuration properties /** * @param {?} clientId * @return {?} */ setClientId(clientId) { this.clientId = clientId; } /** * @param {?} callBackUrl * @return {?} */ setCallBackUrl(callBackUrl) { this.callBackUrl = callBackUrl; } /** * @param {?} scopes * @return {?} */ setScopes(scopes) { this.scopes = scopes; } /** * @param {?} token * @return {?} */ setToken(token) { this.token = token; } /** * \@description Returns track * @param {?} trackId * @return {?} */ getTrack(trackId) { return this.apiGet("/tracks/" + trackId, null, this.getHeaders()); } /** * \@description Returns tracks * @param {?} trackIds * @return {?} */ getTracks(trackIds) { /** @type {?} */ const t = encodeURIComponent(trackIds); return this.apiGet("/tracks/?ids=" + t, null, this.getHeaders()); } /** * \@description Returns album * @param {?} albumId * @return {?} */ getAlbum(albumId) { return this.apiGet("/albums/" + albumId, null, this.getHeaders()); } /** * \@description Returns albums * @param {?} albumIds * @return {?} */ getAlbums(albumIds) { /** @type {?} */ const a = encodeURIComponent(albumIds); return this.apiGet("/albums/?ids=" + a, null, this.getHeaders()); } /** * \@description Returns albums tracks * @param {?} albumId * @return {?} */ getAlbumsTracks(albumId) { return this.apiGet("/albums/" + albumId + "/tracks", null, this.getHeaders()); } /** * \@description Returns artist * @param {?} artistId Artists spotify ID. * @return {?} */ getArtist(artistId) { return this.apiGet("/artists/" + artistId, null, this.getHeaders()); } /** * \@description Returns artists * @param {?} artistIds * @return {?} */ getArtists(artistIds) { /** @type {?} */ const a = encodeURIComponent(artistIds); return this.apiGet("/artists/?ids=" + a, null, this.getHeaders()); } /** * \@description Returns artists albums * @param {?} artistId Artists Spotify ID. * @return {?} */ getArtistsAlbums(artistId) { return this.apiGet("/artists/" + artistId + "/albums", null, this.getHeaders()); } /** * \@description Returns artists top tracks * @param {?} artistId Artists Spotify ID. * @param {?=} countryCode Country code in 2 char Format. For example FI. Defaults to extracting country code from token. * @return {?} */ getArtistsTopTracks(artistId, countryCode) { /** @type {?} */ var countryCode = (countryCode != null && countryCode.length == 2) ? countryCode : "from_token"; return this.apiGet("/artists/" + artistId + "/top-tracks?country=" + countryCode, null, this.getHeaders()); } /** * \@description Returns the user assosiated with auth token * @param {?} userId The Spotify User id * @return {?} */ getUserById(userId) { return this.apiGet('/users/' + userId, null, this.getHeaders()); } /** * \@description Returns the user assosiated with auth token * @return {?} */ getUser() { return this.apiGet('/me', null, this.getHeaders()); } /** * \@description Returns current users top tracks * @param {?} timeRange The range of time results are returned from. Use short_term, medium_term or long_term. Defaults to long_term. * @param {?=} count Amount of tracks returned. Max value 50. Defaults to 50. * @return {?} */ getUsersTopTracks(timeRange, count) { /** @type {?} */ const params = { //set backup default values 'time_range': timeRange == null ? 'long_term' : timeRange, 'limit': count == null ? '50' : count }; return this.apiGet("/me/top/tracks", params, this.getHeaders()); } /** * \@description Returns users playlists * @param {?} userId Users Spotify Id * @return {?} */ getUsersPlaylists(userId) { return this.apiGet('/users/' + userId + '/playlists', null, this.getHeaders()); } /** * \@description Returns recommendations based on seeds. Max 5 seeds allowed (thats total so for example 3 artists and 2 tracks) * @param {?} seedArtists Comma separated list of artist seed Ids * @param {?} seedTracks Comma separated list of track seed Ids * @return {?} */ getRecommendedTracks(seedArtists, seedTracks) { /** @type {?} */ const sA = encodeURIComponent(seedArtists); /** @type {?} */ const sT = encodeURIComponent(seedTracks); return this.apiGet('/recommendations?limit=50&seed_artists=' + sA + '&seed_tracks=' + sT, null, this.getHeaders()); } /** * \@description Get a list of new album releases featured in Spotify * @param {?=} countryCode Country code in 2 char Format. If not set searches from all. * @param {?=} count Amount of results returned. Defaults to 20. * @return {?} */ getNewReleases(countryCode, count) { /** @type {?} */ var query = ""; if (countryCode != null && countryCode != "") { query += "?country=" + countryCode; } if (count != null && count != "") { query += (query.length == 0) ? "?" : "&"; query += "limit=" + count; } return this.apiGet('/browse/new-releases' + query, null, this.getHeaders()); } /** * \@description Get a list of Spotify featured playlists * @param {?=} countryCode Country code in 2 char Format. If not set searches from all. * @param {?=} count Amount of results returned. Defaults to 20. * @return {?} */ getFeaturedPlaylists(countryCode, count) { /** @type {?} */ var query = ""; if (countryCode != null && countryCode != "") { query += "?country=" + countryCode; } if (count != null && count != "") { query += (query.length == 0) ? "?" : "&"; query += "limit=" + count; } return this.apiGet('/browse/featured-playlists' + query, null, this.getHeaders()); } /** * \@description Get a single category used to tag items in Spotify * @param {?} categoryId Spotifys Category Id * @return {?} */ getCategory(categoryId) { return this.apiGet('/browse/categories/' + categoryId, null, this.getHeaders()); } /** * \@description Get a list of categories used to tag items in Spotify * @param {?=} countryCode Country code in 2 char Format. If not set searches from all. * @param {?=} count Amount of results returned. Defaults to 20. * @return {?} */ getCategories(countryCode, count) { /** @type {?} */ var query = ""; if (countryCode != null && countryCode != "") { query += "?country=" + countryCode; } if (count != null && count != "") { query += (query.length == 0) ? "?" : "&"; query += "limit=" + count; } return this.apiGet('/browse/categories' + query, null, this.getHeaders()); } /** * \@description Get a list of Spotify playlists tagged with a particular category. * @param {?} categoryId The Spotify category ID for the category. * @param {?=} countryCode Country code in 2 char Format. If not set searches from all. * @param {?=} count Amount of results returned. Defaults to 20. * @return {?} */ getCategorysPlaylists(categoryId, countryCode, count) { /** @type {?} */ var query = ""; if (countryCode != null && countryCode != "") { query += "?country=" + countryCode; } if (count != null && count != "") { query += (query.length == 0) ? "?" : "&"; query += "limit=" + count; } return this.apiGet('/browse/categories/' + categoryId + '/playlists' + query, null, this.getHeaders()); } /** * \@description Creates a playlist * @param {?} userId Users spotify ID. * @param {?} name A name for the playlist . * @param {?} description A description for the playlist. * @param {?} isPublic Defines if a playlist is public or private * @return {?} */ createPlayList(userId, name, description, isPublic) { /** @type {?} */ var body = { "name": name, "description": description, "public": isPublic }; return this.apiPost('/me/playlists?user_id=' + userId, body, this.getHeaders()); } /** * \@description Adds tracks to playlist * @param {?} playListId * @param {?} trackUris Array of spotify track uris. Max 100 tracks can be added at a time. * @return {?} */ addTracksToPlaylist(playListId, trackUris) { /** @type {?} */ var body = { "uris": trackUris, }; return this.apiPost('/playlists/' + playListId + '/tracks', body, this.getHeaders()); } /** * \@description Returns Spotify Catalog information about artists, albums, tracks or playlists that match a keyword string. * @param {?} query Search word. * @param {?} types * @param {?} count The count is applied within each type, not on the total response. So count 3 with type album,track returns 3 albums and 3 tracks. Max 50. * @return {?} */ search(query, types, count) { /** @type {?} */ const q = encodeURIComponent(query); /** @type {?} */ const t = encodeURIComponent(types); return this.apiGet('/search?q=' + q + '&type=' + t + '&limit=' + count, null, this.getHeaders()); } /** * @private * @param {?} endpoint * @param {?} params * @param {?} headers * @return {?} */ apiGet(endpoint, params, headers) { return this.http.get(this.baseUrl + endpoint, { params: params, headers: headers, withCredentials: false }).toPromise(); } /** * @private * @param {?} endpoint * @param {?} data * @param {?} headers * @return {?} */ apiPost(endpoint, data, headers) { return this.http.post(this.baseUrl + endpoint, data, { headers: headers, }) .toPromise(); } /** * @private * @return {?} */ getHeaders() { return { 'Authorization': 'Bearer ' + this.token, 'Accept': 'application/json', 'Content-Type': 'application/json' }; } } SpotifyRgService.decorators = [ { type: Injectable, args: [{ providedIn: 'root' },] } ]; /** @nocollapse */ SpotifyRgService.ctorParameters = () => [ { type: HttpClient } ]; /** @nocollapse */ SpotifyRgService.ngInjectableDef = defineInjectable({ factory: function SpotifyRgService_Factory() { return new SpotifyRgService(inject(HttpClient)); }, token: SpotifyRgService, providedIn: "root" }); /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class SpotifyServiceRgModule { } SpotifyServiceRgModule.decorators = [ { type: NgModule, args: [{ declarations: [], imports: [ HttpClientModule ], exports: [] },] } ]; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ export { SpotifyRgService, SpotifyServiceRgModule }; //# sourceMappingURL=spotify-service-rg.js.map