Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine the Spotify and Statsfm get and list endpoints #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@statsfm/statsfm.js",
"version": "1.13.12",
"version": "2.0.0",
"description": "",
"main": "dist/index.js",
"private": false,
Expand Down
4 changes: 4 additions & 0 deletions src/interfaces/statsfm/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ export interface QueryWithDates {
before?: number;
after?: number;
}

export interface QueryWithIdType {
type: 'spotify';
}
56 changes: 23 additions & 33 deletions src/lib/albums/AlbumsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,46 @@ import Manager from '../Manager';

export default class AlbumsManager extends Manager {
/**
* @description Get a album by ID.
* @param {number} id The ID of the album.
* @returns {Promise<Album>} Returns a promise with a single {@link Album}.
* @description Get an album by ID.
* @param {T} id The ID of the album.
* @param {statsfm.QueryWithIdType} options Request options.
* @returns {Promise<statsfm.Album>} Returns a promise with a single {@link statsfm.Album Album}.
*/
async get(id: number): Promise<statsfm.Album> {
const res = await this.http.get(`/albums/${id}`);
async get<T extends number | string>(
id: T,
...options: T extends number ? [options?: undefined] : [options: statsfm.QueryWithIdType]
): Promise<statsfm.Album> {
const res = await this.http.get(`/albums/${id}`, {
query: options[0]
});

return res.data.item as statsfm.Album;
return res.data.item;
}

/**
* @description Get a list of albums by IDs.
* @param {string} ids The IDs of the albums
* * @returns {Promise<Album[]>} Returns a promise with a {@link Album}s.
* @param {T} ids The IDs of the albums.
* @param {statsfm.QueryWithIdType} options Request options.
* @returns {Promise<statsfm.Album[]>} Returns a promise with {@link statsfm.Album Albums}.
*/
async list(ids: number[]): Promise<statsfm.Album[]> {
const res = await this.http.get(`/albums/list`, {
query: {
ids: ids.join(',')
}
});

return res.data.items as statsfm.Album[];
}

async getSpotify(id: string): Promise<statsfm.Album> {
const res = await this.http.get(`/albums/${id}`, {
query: {
type: 'spotify'
}
});

return res.data.item as statsfm.Album;
}

async listSpotify(ids: string[]): Promise<statsfm.Album[]> {
async list<T extends number[] | string[]>(
ids: T,
...options: T extends number ? [options?: undefined] : [options: statsfm.QueryWithIdType]
): Promise<statsfm.Album[]> {
const res = await this.http.get(`/albums/list`, {
query: {
ids: ids.join(','),
type: 'spotify'
...options[0]
}
});

return res.data.items as statsfm.Album[];
return res.data.items;
}

/**
* @description Get a list of tracks on the album.
* @param {number} id The IDs of the album.
* @returns {Promise<Track[]>} Returns a promise with a {@link Track[]}s.
* @param {number} id The ID of the album.
* @returns {Promise<Track[]>} Returns a promise with {@link statsfm.Track Tracks}.
*/
async tracks(id: number): Promise<statsfm.Track[]> {
const res = await this.http.get(`/albums/${id}/tracks`);
Expand Down
56 changes: 23 additions & 33 deletions src/lib/artists/ArtistsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,46 @@ import Manager from '../Manager';

export default class ArtistsManager extends Manager {
/**
* @description Get a artist by ID.
* @param {number} id The ID of the artist.
* @returns {Promise<Artist>} Returns a promise with a single {@link Artist}.
* @description Get an artist by ID.
* @param {T} id The ID of the artist.
* @param {statsfm.QueryWithIdType} options Request options.
* @returns {Promise<statsfm.Artist>} Returns a promise with a single {@link statsfm.Artist Artist}.
*/
async get(id: number): Promise<statsfm.Artist> {
const res = await this.http.get(`/artists/${id}`);
async get<T extends number | string>(
id: T,
...options: T extends number ? [options?: undefined] : [options: statsfm.QueryWithIdType]
): Promise<statsfm.Artist> {
const res = await this.http.get(`/artists/${id}`, {
query: options[0]
});

return res.data.item as statsfm.Artist;
return res.data.item;
}

/**
* @description Get a list of artists by IDs.
* @param {number} ids The IDs of the track.
* @returns {Promise<Artist[]>} Returns a promise with a {@link Artist}s.
* @param {T} ids The IDs of the artists.
* @param {statsfm.QueryWithIdType} options Request options.
* @returns {Promise<statsfm.Artist[]>} Returns a promise with {@link statsfm.Artist Artists}.
*/
async list(ids: number[]): Promise<statsfm.Artist[]> {
const res = await this.http.get(`/artists/list`, {
query: {
ids: ids.join(',')
}
});

return res.data.items as statsfm.Artist[];
}

async getSpotify(id: string): Promise<statsfm.Artist> {
const res = await this.http.get(`/artists/${id}`, {
query: {
type: 'spotify'
}
});

return res.data.item as statsfm.Artist;
}

async listSpotify(ids: string[]): Promise<statsfm.Artist[]> {
async list<T extends number[] | string[]>(
ids: T,
...options: T extends number ? [options?: undefined] : [options: statsfm.QueryWithIdType]
): Promise<statsfm.Artist[]> {
const res = await this.http.get(`/artists/list`, {
query: {
ids: ids.join(','),
type: 'spotify'
...options[0]
}
});

return res.data.items as statsfm.Artist[];
return res.data.items;
}

/**
* @description Get a list of tracks by the artist ID.
* @param {number} id The IDs of the artist.
* @returns {Promise<Track[]>} Returns a promise with a {@link Track[]}s.
* @param {number} id The ID of the artist.
* @returns {Promise<statsfm.Track[]>} Returns a promise with {@link statsfm.Track Tracks}.
*/
async tracks(id: number): Promise<statsfm.Track[]> {
const res = await this.http.get(`/artists/${id}/tracks`);
Expand Down
46 changes: 18 additions & 28 deletions src/lib/tracks/TracksManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,35 @@ import Manager from '../Manager';
export default class TracksManager extends Manager {
/**
* @description Get a track by ID.
* @param {number} id The ID of the track.
* @returns {Promise<Track>} Returns a promise with a single {@link Track}.
* @param {T} id The ID of the track.
* @param {statsfm.QueryWithIdType} options Request options.
* @returns {Promise<statsfm.Track>} Returns a promise with a single {@link statsfm.Track Track}.
*/
async get(id: number): Promise<statsfm.Track> {
const res = await this.http.get(`/tracks/${id}`);
async get<T extends number | string>(
id: T,
...options: T extends number ? [options?: undefined] : [options: statsfm.QueryWithIdType]
): Promise<statsfm.Track> {
const res = await this.http.get(`/tracks/${id}`, {
query: options[0]
});

return res.data.item;
}

/**
* @description Get a list of tracks by IDs.
* @param {number} ids The IDs of the tracks.
* @returns {Promise<Track[]>} Returns a promise with a single {@link Track}.
* @param {T} ids The IDs of the tracks.
* @param {statsfm.QueryWithIdType} options Request options.
* @returns {Promise<statsfm.Track[]>} Returns a promise with {@link statsfm.Track Tracks}.
*/
async list(ids: number[]): Promise<statsfm.Track[]> {
const res = await this.http.get(`/tracks/list`, {
query: {
ids: ids.join(',')
}
});

return res.data.items;
}

async getSpotify(id: string): Promise<statsfm.Track> {
const res = await this.http.get(`/tracks/${id}`, {
query: {
type: 'spotify'
}
});

return res.data.item;
}

async listSpotify(ids: string[]): Promise<statsfm.Track[]> {
async list<T extends number[] | string[]>(
ids: T,
...options: T extends number ? [options?: undefined] : [options: statsfm.QueryWithIdType]
): Promise<statsfm.Track[]> {
const res = await this.http.get(`/tracks/list`, {
query: {
ids: ids.join(','),
type: 'spotify'
...options[0]
}
});

Expand Down