-
Notifications
You must be signed in to change notification settings - Fork 10
/
ArtistsApi.ts
160 lines (153 loc) · 4.08 KB
/
ArtistsApi.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { Http } from '../helpers/Http';
import { Artist, Track } from '../types/SpotifyObjects';
import { GetArtistAlbumsOptions } from '../types/SpotifyOptions';
import {
GetArtistAlbumsResponse,
GetArtistsResponse,
GetArtistTopTracksResponse,
GetRelatedArtistsResponse,
} from '../types/SpotifyResponses';
export class ArtistsApi {
private http: Http;
constructor(http: Http) {
this.http = http;
}
/**
* ### Get an Artist
*
* Get Spotify catalog information for a single artist identified by their
* unique Spotify ID.
*
* **Required Scopes:** None.
*
* @example
* ```ts
* const artist = await spotify.artists.getArtist('4Ge8xMJNwt6EEXOzVXju9a');
* console.log(artist.name);
* // "Caroline Polachek"
* ```
*
* @param artistId The Spotify ID for the artist.
*/
getArtist(artistId: string): Promise<Artist> {
return this.http.get<Artist>(`/artists/${artistId}`);
}
/**
* ### Get an Artist's Albums
*
* Get Spotify catalog information about an artist's albums.
*
* **Required Scopes:** None.
*
* @example
* ```ts
* const albums = await spotify.artists.getArtistAlbums('2mHjhKyKCLh6MZELuCe1Es', {
* country: ['US'],
* include_groups: ['album'],
* });
* console.log(albums.items.map(album => album.name));
* // Array [ "Beware of the Dogs" ]
* ```
*
* @param artistId The Spotify ID for the artist.
* @param options Optional request information.
*/
getArtistAlbums(
artistId: string,
options?: GetArtistAlbumsOptions,
): Promise<GetArtistAlbumsResponse> {
return this.http.get<GetArtistAlbumsResponse>(
`/artists/${artistId}/albums`,
options && { params: options },
);
}
/**
* ### Get Several Artists
*
* Get Spotify catalog information for several artists based on their
* Spotify IDs.
*
* **Required Scopes:** None.
*
* @example
* ```ts
* const artists = await spotify.artists.getArtists([
* '4iCwCMnqsNZ6atvRiADgtn',
* '6ns6XAOsw4B0nDUIovAOUO',
* '2yQf6b8hxahZaT5dHlWaB1',
* ]);
* console.log(artists.map(artist => artist.name));
* // Array [ "RZA", "GZA", "Raekwon" ]
* ```
*
* @param artistIds The Spotify IDs for the artists.
*/
async getArtists(artistIds: string[]): Promise<Artist[]> {
const response = await this.http.get<GetArtistsResponse>('/artists', {
params: {
ids: artistIds,
},
});
return response.artists;
}
/**
* ### Get an Artist's Top Tracks
*
* Get Spotify catalog information about an artist's top tracks by country.
*
* **Required Scopes:** None.
*
* @example
* ```ts
* const tracks = await spotify.artists.getArtistTopTracks(
* '2h93pZq0e7k5yf4dywlkpM',
* 'US',
* );
* console.log(tracks.map(track => track.name));
* // Array [ "Chanel", "Nights", "Ivy", ... ]
* ```
*
* @param artistId The Spotify ID for the artist.
* @param country An ISO 3166-1 alpha-2 country code or the string `from_token`.
*/
async getArtistTopTracks(
artistId: string,
country: string,
): Promise<Track[]> {
const response = await this.http.get<GetArtistTopTracksResponse>(
`/artists/${artistId}/top-tracks`,
{
params: {
country,
},
},
);
return response.tracks;
}
/**
* ### Get an Artist's Related Artists
*
* Get Spotify catalog information about artists similar to a given artist.
* Similarity is based on analysis of the Spotify community's listening
* history.
*
* **Required Scopes:** None.
*
* @example
* ```ts
* const artists = await spotify.artists.getRelatedArtists(
* '6kBDZFXuLrZgHnvmPu9NsG',
* );
* console.log(artists.map(artist => artist.name));
* // Array [ "AFX", "Squarepusher", "Boards of Canada", ... ]
* ```
*
* @param artistId The Spotify ID for the artist.
*/
async getRelatedArtists(artistId: string): Promise<Artist[]> {
const response = await this.http.get<GetRelatedArtistsResponse>(
`/artists/${artistId}/related-artists`,
);
return response.artists;
}
}