Skip to content

Commit

Permalink
Merge pull request #45 from calluswhatyouwant/v0.3.x
Browse files Browse the repository at this point in the history
Upgrade to v0.3.1
  • Loading branch information
JRobsonJr committed Feb 20, 2019
2 parents 3356f4b + f195b5a commit d79a36e
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 54 deletions.
30 changes: 16 additions & 14 deletions src/lib/albums.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { getAxiosSpotifyInstance } from './driver';
import { Album, TrackSimplified, Page } from './models';

export const getAlbum = async (id: string, market?: string): Promise<Album> => {
const params = { params: { market } };
const response = await getAxiosSpotifyInstance().get(
`/albums/${id}`,
params
);
export const getAlbum = async (
id: string,
params?: { market?: string }
): Promise<Album> => {
const response = await getAxiosSpotifyInstance().get(`/albums/${id}`, {
params,
});

return new Album(response.data);
};

export const getSeveralAlbums = async (
ids: string[],
market?: string
params?: { market?: string }
): Promise<Album[]> => {
if (ids.length > 20) {
const exceptionLink =
Expand All @@ -22,22 +23,23 @@ export const getSeveralAlbums = async (
`The maximum number of albums is 20. See ${exceptionLink} for details`
);
}
const params = { params: { market, ids: ids.join(',') } };
const response = await getAxiosSpotifyInstance().get('/albums', params);
const config = { params: { ...params, ids: ids.join(',') } };
const response = await getAxiosSpotifyInstance().get('/albums', config);

return response.data.albums.map((albumJson: any) => new Album(albumJson));
};

export const getAlbumTracks = async (
id: string,
offset = 0,
limit = 20,
market?: string
params?: {
offset?: number;
limit?: number;
market?: string;
}
): Promise<Page<TrackSimplified>> => {
const params = { params: { offset, limit, market } };
const response = await getAxiosSpotifyInstance().get(
`/albums/${id}/tracks`,
params
{ params }
);
return new Page<TrackSimplified>(response.data, TrackSimplified);
};
31 changes: 20 additions & 11 deletions src/lib/artists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,34 @@ export const getSeveralArtists = async (ids: string[]): Promise<Artist[]> => {
`The maximum number of artists is 50. See ${exceptionLink} for details`
);
}
const params = { params: { ids } };
const response = await getAxiosSpotifyInstance().get('/artists', params);
const config = { params: { ids } };
const response = await getAxiosSpotifyInstance().get('/artists', config);
return response.data.artists.map(
(artistJson: any) => new Artist(artistJson)
);
};

export const getArtistAlbums = async (
id: string,
offset = 0,
limit = 20,
includeGroups?: string[],
market?: string
params?: {
offset?: number;
limit?: number;
includeGroups?: string[];
market?: string;
}
): Promise<Page<AlbumSimplified>> => {
const params: any = { params: { offset, limit, market } };
if (includeGroups) params.params.include_groups = includeGroups.join(',');
const config = {
params: {
...params,
include_groups:
params && params.includeGroups
? params.includeGroups.join(',')
: '',
},
};
const response = await getAxiosSpotifyInstance().get(
`/artists/${id}/albums`,
params
config
);
return new Page<AlbumSimplified>(response.data, AlbumSimplified);
};
Expand All @@ -52,10 +61,10 @@ export const getArtistTopTracks = async (
id: string,
market: string
): Promise<Track[]> => {
const params = { params: { market } };
const config = { params: { market } };
const response = await getAxiosSpotifyInstance().get(
`/artists/${id}/top-tracks`,
params
config
);
return response.data.tracks.map((trackJson: any) => new Track(trackJson));
};
23 changes: 11 additions & 12 deletions src/lib/follow.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { getAxiosSpotifyInstance } from './driver';
import { Artist } from './models';

export const getFollowedArtists = async (
limit: number = 20,
after?: string
): Promise<Artist[]> => {
if (limit < 1 || limit > 50) {
export const getFollowedArtists = async (params?: {
limit?: number;
after?: string;
}): Promise<Artist[]> => {
if (params && params.limit && (params.limit < 1 || params.limit > 50)) {
const exceptionLink =
'https://developer.spotify.com/documentation/web-api/reference/follow/get-followed/';
throw new Error(
`The limit must be between 1 and 50 (inclusive). See ${exceptionLink} for details`
);
}

const afterQuery = after ? after : null;
const params = { params: { limit, type: 'artist', after: afterQuery } };
const config = { params: { ...params, type: 'artist' } };
const response = await getAxiosSpotifyInstance().get(
'/me/following',
params
config
);

return response.data.artists.items.map(
Expand All @@ -41,10 +40,10 @@ export const isFollowing = async (
);
}

const params = { params: { type, ids: ids.join() } };
const config = { params: { type, ids: ids.join() } };
const response = await getAxiosSpotifyInstance().get(
'/me/following/contains',
params
config
);

return response.data;
Expand All @@ -62,10 +61,10 @@ export const checkUsersFollowingPlaylist = async (
);
}

const params = { params: { ids: ids.join() } };
const config = { params: { ids: ids.join() } };
const response = await getAxiosSpotifyInstance().get(
`/playlists/${playlistId}/followers/contains`,
params
config
);

return response.data;
Expand Down
24 changes: 18 additions & 6 deletions src/lib/playlists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,32 @@ export const getPlaylist = async (id: string) => {
return new Playlist(response.data);
};

export const getPlaylistTracks = async (id: string, offset = 0, limit = 20) => {
const params = { params: { offset, limit } };
export const getPlaylistTracks = async (
id: string,
params?: {
fields?: string;
limit?: number;
offset?: number;
market?: string;
}
) => {
const response = await getAxiosSpotifyInstance().get(
`/playlists/${id}/tracks`,
params
{ params }
);
return new Page<PlaylistTrack>(response.data, PlaylistTrack);
};

export const getUserPlaylists = async (id: string, offset = 0, limit = 20) => {
const params = { params: { offset, limit } };
export const getUserPlaylists = async (
id: string,
params?: {
limit?: number;
offset?: number;
}
) => {
const response = await getAxiosSpotifyInstance().get(
`/users/${id}/playlists`,
params
{ params }
);
return new Page<PlaylistSimplified>(response.data, PlaylistSimplified);
};
9 changes: 5 additions & 4 deletions src/lib/tracks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ export const getAudioFeaturesForTrack = async (id: string) => {
};

export const getAudioFeaturesForSeveralTracks = async (ids: string[]) => {
const params = { ids: ids.join(',') };
const response = await getAxiosSpotifyInstance().get(`/audio-features`, {
params,
});
const config = { params: { ids: ids.join(',') } };
const response = await getAxiosSpotifyInstance().get(
`/audio-features`,
config
);
return response.data.audio_features.map(
(audioFeaturesJson: any) => new AudioFeatures(audioFeaturesJson)
);
Expand Down
6 changes: 2 additions & 4 deletions test/albums.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ describe('Album requests', () => {
it('response should match all paging object attributes', async () => {
const albumTracksResponse = await getAlbumTracks(
'3yGwYUrWqe6PHf0IcUdkbZ',
0,
2
{ offset: 0, limit: 2 }
);
checkMatchingPagingObjectAttributes(
albumTracksResponse,
Expand All @@ -84,8 +83,7 @@ describe('Album requests', () => {
it('response should match all custom paging object attributes', async () => {
const albumTracksResponse = await getAlbumTracks(
'3yGwYUrWqe6PHf0IcUdkbZ',
0,
2
{ offset: 0, limit: 2 }
);
expect(albumTracksResponse.hasNext()).to.be.true;
expect(albumTracksResponse.hasPrevious()).to.be.false;
Expand Down
5 changes: 2 additions & 3 deletions test/artists.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,14 @@ describe('Artist requests', () => {
beforeEach(() => {
nock('https://api.spotify.com/v1')
.get('/artists/1WgXqy2Dd70QQOU7Ay074N/albums')
.query({ offset: 0, limit: 5 })
.query({ offset: 0, limit: 5, include_groups: '' })
.reply(200, artistAlbumsMock);
});

it('response should match all albums attributes', async () => {
const artistAlbumsResponse = await getArtistAlbums(
'1WgXqy2Dd70QQOU7Ay074N',
0,
5
{ offset: 0, limit: 5 }
);
checkMatchingPagingObjectAttributes(
artistAlbumsResponse,
Expand Down

0 comments on commit d79a36e

Please sign in to comment.