spotify-service-rg
Version:
This is a Angular service for connecting to and using Spotify API.
633 lines (627 loc) • 23.8 kB
JavaScript
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
*/
var SpotifyRgService = /** @class */ (function () {
function SpotifyRgService(http) {
this.http = http;
this.baseUrl = "https://api.spotify.com/v1";
}
/**
* @return {?}
*/
SpotifyRgService.prototype.login = /**
* @return {?}
*/
function () {
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
//Setters for the configuration properties
/**
* @param {?} clientId
* @return {?}
*/
SpotifyRgService.prototype.setClientId =
//Setters for the configuration properties
/**
* @param {?} clientId
* @return {?}
*/
function (clientId) {
this.clientId = clientId;
};
/**
* @param {?} callBackUrl
* @return {?}
*/
SpotifyRgService.prototype.setCallBackUrl = /**
* @param {?} callBackUrl
* @return {?}
*/
function (callBackUrl) {
this.callBackUrl = callBackUrl;
};
/**
* @param {?} scopes
* @return {?}
*/
SpotifyRgService.prototype.setScopes = /**
* @param {?} scopes
* @return {?}
*/
function (scopes) {
this.scopes = scopes;
};
/**
* @param {?} token
* @return {?}
*/
SpotifyRgService.prototype.setToken = /**
* @param {?} token
* @return {?}
*/
function (token) {
this.token = token;
};
/** @description Returns track
* @param artistId Tracks Spotify ID
*/
/**
* \@description Returns track
* @param {?} trackId
* @return {?}
*/
SpotifyRgService.prototype.getTrack = /**
* \@description Returns track
* @param {?} trackId
* @return {?}
*/
function (trackId) {
return this.apiGet("/tracks/" + trackId, null, this.getHeaders());
};
/** @description Returns tracks
* @param artistId Comma separated list of track ID's in a string.
*/
/**
* \@description Returns tracks
* @param {?} trackIds
* @return {?}
*/
SpotifyRgService.prototype.getTracks = /**
* \@description Returns tracks
* @param {?} trackIds
* @return {?}
*/
function (trackIds) {
/** @type {?} */
var t = encodeURIComponent(trackIds);
return this.apiGet("/tracks/?ids=" + t, null, this.getHeaders());
};
/** @description Returns album
* @param artistId Albums Spotify ID.
*/
/**
* \@description Returns album
* @param {?} albumId
* @return {?}
*/
SpotifyRgService.prototype.getAlbum = /**
* \@description Returns album
* @param {?} albumId
* @return {?}
*/
function (albumId) {
return this.apiGet("/albums/" + albumId, null, this.getHeaders());
};
/** @description Returns albums
* @param artistId Comma separated list of album ID's in a string..
*/
/**
* \@description Returns albums
* @param {?} albumIds
* @return {?}
*/
SpotifyRgService.prototype.getAlbums = /**
* \@description Returns albums
* @param {?} albumIds
* @return {?}
*/
function (albumIds) {
/** @type {?} */
var a = encodeURIComponent(albumIds);
return this.apiGet("/albums/?ids=" + a, null, this.getHeaders());
};
/** @description Returns albums tracks
* @param artistId Albums Spotify ID.
*/
/**
* \@description Returns albums tracks
* @param {?} albumId
* @return {?}
*/
SpotifyRgService.prototype.getAlbumsTracks = /**
* \@description Returns albums tracks
* @param {?} albumId
* @return {?}
*/
function (albumId) {
return this.apiGet("/albums/" + albumId + "/tracks", null, this.getHeaders());
};
/** @description Returns artist
* @param artistId Artists spotify ID.
*/
/**
* \@description Returns artist
* @param {?} artistId Artists spotify ID.
* @return {?}
*/
SpotifyRgService.prototype.getArtist = /**
* \@description Returns artist
* @param {?} artistId Artists spotify ID.
* @return {?}
*/
function (artistId) {
return this.apiGet("/artists/" + artistId, null, this.getHeaders());
};
/** @description Returns artists
* @param artistId Comma separated list of artist ID's in a string.
*/
/**
* \@description Returns artists
* @param {?} artistIds
* @return {?}
*/
SpotifyRgService.prototype.getArtists = /**
* \@description Returns artists
* @param {?} artistIds
* @return {?}
*/
function (artistIds) {
/** @type {?} */
var a = encodeURIComponent(artistIds);
return this.apiGet("/artists/?ids=" + a, null, this.getHeaders());
};
/** @description Returns artists albums
* @param artistId Artists Spotify ID.
*/
/**
* \@description Returns artists albums
* @param {?} artistId Artists Spotify ID.
* @return {?}
*/
SpotifyRgService.prototype.getArtistsAlbums = /**
* \@description Returns artists albums
* @param {?} artistId Artists Spotify ID.
* @return {?}
*/
function (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.
*/
/**
* \@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 {?}
*/
SpotifyRgService.prototype.getArtistsTopTracks = /**
* \@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 {?}
*/
function (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
*/
/**
* \@description Returns the user assosiated with auth token
* @param {?} userId The Spotify User id
* @return {?}
*/
SpotifyRgService.prototype.getUserById = /**
* \@description Returns the user assosiated with auth token
* @param {?} userId The Spotify User id
* @return {?}
*/
function (userId) {
return this.apiGet('/users/' + userId, null, this.getHeaders());
};
/** @description Returns the user assosiated with auth token
*/
/**
* \@description Returns the user assosiated with auth token
* @return {?}
*/
SpotifyRgService.prototype.getUser = /**
* \@description Returns the user assosiated with auth token
* @return {?}
*/
function () {
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.
*/
/**
* \@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 {?}
*/
SpotifyRgService.prototype.getUsersTopTracks = /**
* \@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 {?}
*/
function (timeRange, count) {
/** @type {?} */
var 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
*/
/**
* \@description Returns users playlists
* @param {?} userId Users Spotify Id
* @return {?}
*/
SpotifyRgService.prototype.getUsersPlaylists = /**
* \@description Returns users playlists
* @param {?} userId Users Spotify Id
* @return {?}
*/
function (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
*/
/**
* \@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 {?}
*/
SpotifyRgService.prototype.getRecommendedTracks = /**
* \@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 {?}
*/
function (seedArtists, seedTracks) {
/** @type {?} */
var sA = encodeURIComponent(seedArtists);
/** @type {?} */
var 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.
*/
/**
* \@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 {?}
*/
SpotifyRgService.prototype.getNewReleases = /**
* \@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 {?}
*/
function (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.
*/
/**
* \@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 {?}
*/
SpotifyRgService.prototype.getFeaturedPlaylists = /**
* \@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 {?}
*/
function (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
*/
/**
* \@description Get a single category used to tag items in Spotify
* @param {?} categoryId Spotifys Category Id
* @return {?}
*/
SpotifyRgService.prototype.getCategory = /**
* \@description Get a single category used to tag items in Spotify
* @param {?} categoryId Spotifys Category Id
* @return {?}
*/
function (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.
*/
/**
* \@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 {?}
*/
SpotifyRgService.prototype.getCategories = /**
* \@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 {?}
*/
function (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.
*/
/**
* \@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 {?}
*/
SpotifyRgService.prototype.getCategorysPlaylists = /**
* \@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 {?}
*/
function (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
*/
/**
* \@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 {?}
*/
SpotifyRgService.prototype.createPlayList = /**
* \@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 {?}
*/
function (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 Playlist ID.
* @param trackUris Array of spotify track uris. Max 100 tracks can be added at a time.
*/
/**
* \@description Adds tracks to playlist
* @param {?} playListId
* @param {?} trackUris Array of spotify track uris. Max 100 tracks can be added at a time.
* @return {?}
*/
SpotifyRgService.prototype.addTracksToPlaylist = /**
* \@description Adds tracks to playlist
* @param {?} playListId
* @param {?} trackUris Array of spotify track uris. Max 100 tracks can be added at a time.
* @return {?}
*/
function (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 type A comma-separated list of item types to search across. Valid types are album, artist, playlist, and track.
* @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.
*/
/**
* \@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 {?}
*/
SpotifyRgService.prototype.search = /**
* \@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 {?}
*/
function (query, types, count) {
/** @type {?} */
var q = encodeURIComponent(query);
/** @type {?} */
var t = encodeURIComponent(types);
return this.apiGet('/search?q=' + q + '&type=' + t + '&limit=' + count, null, this.getHeaders());
};
/**
* @private
* @param {?} endpoint
* @param {?} params
* @param {?} headers
* @return {?}
*/
SpotifyRgService.prototype.apiGet = /**
* @private
* @param {?} endpoint
* @param {?} params
* @param {?} headers
* @return {?}
*/
function (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 {?}
*/
SpotifyRgService.prototype.apiPost = /**
* @private
* @param {?} endpoint
* @param {?} data
* @param {?} headers
* @return {?}
*/
function (endpoint, data, headers) {
return this.http.post(this.baseUrl + endpoint, data, {
headers: headers,
})
.toPromise();
};
/**
* @private
* @return {?}
*/
SpotifyRgService.prototype.getHeaders = /**
* @private
* @return {?}
*/
function () {
return {
'Authorization': 'Bearer ' + this.token,
'Accept': 'application/json',
'Content-Type': 'application/json'
};
};
SpotifyRgService.decorators = [
{ type: Injectable, args: [{
providedIn: 'root'
},] }
];
/** @nocollapse */
SpotifyRgService.ctorParameters = function () { return [
{ type: HttpClient }
]; };
/** @nocollapse */ SpotifyRgService.ngInjectableDef = defineInjectable({ factory: function SpotifyRgService_Factory() { return new SpotifyRgService(inject(HttpClient)); }, token: SpotifyRgService, providedIn: "root" });
return SpotifyRgService;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var SpotifyServiceRgModule = /** @class */ (function () {
function SpotifyServiceRgModule() {
}
SpotifyServiceRgModule.decorators = [
{ type: NgModule, args: [{
declarations: [],
imports: [
HttpClientModule
],
exports: []
},] }
];
return SpotifyServiceRgModule;
}());
/**
* @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