Skip to content

Commit

Permalink
Merge pull request #53 from calluswhatyouwant/v0.4.x
Browse files Browse the repository at this point in the history
Upgrade to v0.4.0
  • Loading branch information
JoseRenan committed Feb 28, 2019
2 parents d79a36e + f8a81f1 commit 06a5f68
Show file tree
Hide file tree
Showing 19 changed files with 388 additions and 186 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"license": "MIT",
"version": "0.3.0",
"dependencies": {
"axios": "^0.18.0"
"@types/lodash": "^4.14.121",
"axios": "^0.18.0",
"lodash": "^4.17.11"
},
"scripts": {
"start": "ts-node src/start.ts",
Expand Down
101 changes: 101 additions & 0 deletions src/lib/browse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import _ from 'lodash';

import { getAxiosSpotifyInstance } from './driver';
import {
Category,
PlaylistSimplified,
Page,
AlbumSimplified,
Recommendations,
} from './models';
import { propertiesToSnakeCase } from './util';

export const getCategory = async (
id: string,
params?: {
country?: string;
locale?: string;
}
) => {
const response = await getAxiosSpotifyInstance().get(
`/browse/categories/${id}`,
{ params }
);
return new Category(response.data);
};

export const getCategoryPlaylists = async (
id: string,
params?: {
country?: string;
limit?: number;
offset?: number;
}
) => {
const response = await getAxiosSpotifyInstance().get(
`/browse/categories/${id}/playlists`,
{ params }
);
return new Page<PlaylistSimplified>(
response.data,
PlaylistSimplified,
'playlists'
);
};

export const getCategories = async (params?: {
country?: string;
locale?: string;
limit?: number;
offset?: number;
}) => {
const response = await getAxiosSpotifyInstance().get('/browse/categories', {
params,
});
return new Page<Category>(response.data, Category, 'categories');
};

export const getFeaturedPlaylists = async (params?: {
country?: string;
locale?: string;
timestamp?: string;
limit?: number;
offset?: number;
}) => {
const response = await getAxiosSpotifyInstance().get(
'/browse/featured-playlists',
{ params }
);
return new Page<PlaylistSimplified>(
response.data,
PlaylistSimplified,
'playlists'
);
};

export const getNewReleases = async (params?: {
country?: string;
limit?: number;
offset?: number;
}) => {
const response = await getAxiosSpotifyInstance().get(
'/browse/new-releases',
{ params }
);
return new Page<AlbumSimplified>(response.data, AlbumSimplified, 'albums');
};

export const getRecommendations = async (params?: {
limit?: number;
market?: string;
seedArtists?: string[];
seedGenres?: string[];
seedTracks?: string[];
[rest: string]: any;
}) => {
const updatedParams = propertiesToSnakeCase(params);
const response = await getAxiosSpotifyInstance().get('/recommendations', {
params: updatedParams,
});
return new Recommendations(response.data);
};
3 changes: 3 additions & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
export * from './albums';
export * from './artists';
export * from './browse';
export * from './driver';
export * from './follow';
export * from './library';
export * from './personalization';
export * from './playlists';
export * from './player';
export * from './search';
export * from './tracks';
export * from './users-profile';
export * from './models';
36 changes: 36 additions & 0 deletions src/lib/library.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { getAxiosSpotifyInstance } from './driver';
import { Page, SavedAlbum, SavedTrack } from './models';

export const areSavedToCurrentUserLibrary = async (
ids: string[],
type: 'tracks' | 'albums'
) => {
const params = { ids: ids.join() };
const response = await getAxiosSpotifyInstance().get(
`/me/${type}/contains`,
{ params }
);
return response.data;
};

export const getCurrentUserSavedAlbums = async (params?: {
limit?: number;
offset?: number;
market?: string;
}) => {
const response = await getAxiosSpotifyInstance().get('/me/albums', {
params,
});
return new Page<SavedAlbum>(response.data, SavedAlbum);
};

export const getCurrentUserSavedTracks = async (params?: {
limit?: number;
offset?: number;
market?: string;
}) => {
const response = await getAxiosSpotifyInstance().get('/me/tracks', {
params,
});
return new Page<SavedTrack>(response.data, SavedTrack);
};
8 changes: 4 additions & 4 deletions src/lib/models/album/album-simplified.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ class AlbumSimplified {
this.uri = json.uri;
}

get stringArtists() {
get stringArtists(): string {
const artistNames = this.artists.map(artist => artist.name);
return artistNames.join(', ');
}

get releaseYear() {
return this.releaseDate.substring(0, 4);
get releaseYear(): number {
return Number(this.releaseDate.substring(0, 4));
}

get imageUrl() {
get imageUrl(): string {
return this.images[0].url;
}
}
Expand Down
71 changes: 23 additions & 48 deletions src/lib/models/album/album.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,57 @@
import ArtistSimplified from '../artist/artist-simplified';
import Image from '../common/image';
import _ from 'lodash';

import AlbumSimplified from './album-simplified';
import Page from '../paging/page';
import TrackSimplified from '../track/track-simplified';

class Album {
albumType: 'album' | 'single' | 'compilation';

artists: ArtistSimplified[];

availableMarkets: string[];

class Album extends AlbumSimplified {
copyrights: any[];

externalIds: any;

externalUrls: any;

genres: string[];

href: string;

id: string;

images: Image[];

label: string;

name: string;

popularity: number;

releaseDate: string;

releaseDatePrecision: 'year' | 'month' | 'day';

totalTracks: number;

tracks: Page<TrackSimplified>;

type: 'album';

uri: string;

constructor(json: any) {
this.albumType = json.album_type;
this.artists = json.artists.map(
(artistJson: any) => new ArtistSimplified(artistJson)
);
this.availableMarkets = json.available_markets;
super(json);
this.copyrights = json.copyrights;
this.externalIds = json.external_ids;
this.externalUrls = json.external_urls;
this.genres = json.genres;
this.href = json.href;
this.id = json.id;
this.images = json.images.map((imageJson: any) => new Image(imageJson));
this.label = json.label;
this.name = json.name;
this.popularity = json.popularity;
this.releaseDate = json.release_date;
this.releaseDatePrecision = json.release_date_precision;
this.totalTracks = json.total_tracks;
this.tracks = new Page<TrackSimplified>(json.tracks, TrackSimplified);
this.type = json.type;
this.uri = json.uri;
}

get stringArtists(): string {
const artistNames = this.artists.map(artist => artist.name);
return artistNames.join(', ');
async getAllTracks(): Promise<TrackSimplified[]> {
return this.getAllTracksRecursive([], this.tracks);
}

get releaseYear(): number {
return Number(this.releaseDate.substring(0, 4));
private async getAllTracksRecursive(
tracks: TrackSimplified[],
page: Page<TrackSimplified>
): Promise<TrackSimplified[]> {
if (page.hasNext()) {
const nextPage = await page.getNextPage();
return this.getAllTracksRecursive(
tracks.concat(page.items),
nextPage
);
}

return tracks.concat(page.items);
}

get imageUrl(): string {
return this.images[0].url;
async getDurationMs(): Promise<number> {
const tracks = await this.getAllTracks();
return _.sum(tracks.map(track => track.durationMs));
}
}

Expand Down
26 changes: 7 additions & 19 deletions src/lib/models/artist/artist.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,26 @@
import ArtistSimplified from './artist-simplified';
import Followers from '../common/followers';
import Image from '../common/image';

class Artist {
externalUrls: any;

class Artist extends ArtistSimplified {
followers: Followers;

genres: string[];

href: string;

id: string;

images: Image[];

name: string;

popularity: number;

type: 'artist';

uri: string;

constructor(json: any) {
this.externalUrls = json.external_urls;
super(json);
this.followers = new Followers(json.followers);
this.genres = json.genres;
this.href = json.href;
this.id = json.id;
this.images = json.images.map((imageJson: any) => new Image(imageJson));
this.name = json.name;
this.popularity = json.popularity;
this.type = json.type;
this.uri = json.uri;
}

get imageUrl(): string {
return this.images[0].url;
}
}

Expand Down
1 change: 0 additions & 1 deletion src/lib/models/playlist/playlist-simplified.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class PlaylistSimplified {

tracks: any;

// { href: string, total: number }
type: 'playlist';

uri: string;
Expand Down

0 comments on commit 06a5f68

Please sign in to comment.