From dd015dafdb66329d17ac509e3c5a0bc1e040fdfb Mon Sep 17 00:00:00 2001 From: Martijn-Faber Date: Thu, 5 May 2022 19:00:09 +0200 Subject: [PATCH 1/2] refactor: combine the spotify and statsfm `get` and `list` endpoints --- src/interfaces/statsfm/query.ts | 4 +++ src/lib/albums/AlbumsManager.ts | 56 +++++++++++++------------------ src/lib/artists/ArtistsManager.ts | 56 +++++++++++++------------------ src/lib/tracks/TracksManager.ts | 46 ++++++++++--------------- 4 files changed, 68 insertions(+), 94 deletions(-) diff --git a/src/interfaces/statsfm/query.ts b/src/interfaces/statsfm/query.ts index d395aa2..399d196 100644 --- a/src/interfaces/statsfm/query.ts +++ b/src/interfaces/statsfm/query.ts @@ -29,3 +29,7 @@ export interface QueryWithDates { before?: number; after?: number; } + +export interface QueryWithIdType { + type: 'spotify'; +} diff --git a/src/lib/albums/AlbumsManager.ts b/src/lib/albums/AlbumsManager.ts index 8fa46d5..52694cb 100644 --- a/src/lib/albums/AlbumsManager.ts +++ b/src/lib/albums/AlbumsManager.ts @@ -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} 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} Returns a promise with a single {@link statsfm.Album Album}. */ - async get(id: number): Promise { - const res = await this.http.get(`/albums/${id}`); + async get( + id: T, + ...options: T extends number ? [options?: undefined] : [options: statsfm.QueryWithIdType] + ): Promise { + 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} Returns a promise with a {@link Album}s. + * @param {T} ids The IDs of the albums. + * @param {statsfm.QueryWithIdType} options Request options. + * @returns {Promise} Returns a promise with {@link statsfm.Album Albums}. */ - async list(ids: number[]): Promise { - const res = await this.http.get(`/albums/list`, { - query: { - ids: ids.join(',') - } - }); - - return res.data.items as statsfm.Album[]; - } - - async getSpotify(id: string): Promise { - const res = await this.http.get(`/albums/${id}`, { - query: { - type: 'spotify' - } - }); - - return res.data.item as statsfm.Album; - } - - async listSpotify(ids: string[]): Promise { + async list( + ids: T, + ...options: T extends number ? [options?: undefined] : [options: statsfm.QueryWithIdType] + ): Promise { 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} Returns a promise with a {@link Track[]}s. + * @param {number} id The ID of the album. + * @returns {Promise} Returns a promise with {@link statsfm.Track Tracks}. */ async tracks(id: number): Promise { const res = await this.http.get(`/albums/${id}/tracks`); diff --git a/src/lib/artists/ArtistsManager.ts b/src/lib/artists/ArtistsManager.ts index 2130b4c..de4a043 100644 --- a/src/lib/artists/ArtistsManager.ts +++ b/src/lib/artists/ArtistsManager.ts @@ -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} 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} Returns a promise with a single {@link statsfm.Artist Artist}. */ - async get(id: number): Promise { - const res = await this.http.get(`/artists/${id}`); + async get( + id: T, + ...options: T extends number ? [options?: undefined] : [options: statsfm.QueryWithIdType] + ): Promise { + 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} Returns a promise with a {@link Artist}s. + * @param {T} ids The IDs of the artists. + * @param {statsfm.QueryWithIdType} options Request options. + * @returns {Promise} Returns a promise with {@link statsfm.Artist Artists}. */ - async list(ids: number[]): Promise { - const res = await this.http.get(`/artists/list`, { - query: { - ids: ids.join(',') - } - }); - - return res.data.items as statsfm.Artist[]; - } - - async getSpotify(id: string): Promise { - const res = await this.http.get(`/artists/${id}`, { - query: { - type: 'spotify' - } - }); - - return res.data.item as statsfm.Artist; - } - - async listSpotify(ids: string[]): Promise { + async list( + ids: T, + ...options: T extends number ? [options?: undefined] : [options: statsfm.QueryWithIdType] + ): Promise { 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} Returns a promise with a {@link Track[]}s. + * @param {number} id The ID of the artist. + * @returns {Promise} Returns a promise with {@link statsfm.Track Tracks}. */ async tracks(id: number): Promise { const res = await this.http.get(`/artists/${id}/tracks`); diff --git a/src/lib/tracks/TracksManager.ts b/src/lib/tracks/TracksManager.ts index 6acd73b..f291d98 100644 --- a/src/lib/tracks/TracksManager.ts +++ b/src/lib/tracks/TracksManager.ts @@ -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} Returns a promise with a single {@link Track}. + * @param {T} id The ID of the track. + * @param {statsfm.QueryWithIdType} options Request options. + * @returns {Promise} Returns a promise with a single {@link statsfm.Track Track}. */ - async get(id: number): Promise { - const res = await this.http.get(`/tracks/${id}`); + async get( + id: T, + ...options: T extends number ? [options?: undefined] : [options: statsfm.QueryWithIdType] + ): Promise { + 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} Returns a promise with a single {@link Track}. + * @param {T} ids The IDs of the tracks. + * @param {statsfm.QueryWithIdType} options Request options. + * @returns {Promise} Returns a promise with {@link statsfm.Track Tracks}. */ - async list(ids: number[]): Promise { - const res = await this.http.get(`/tracks/list`, { - query: { - ids: ids.join(',') - } - }); - - return res.data.items; - } - - async getSpotify(id: string): Promise { - const res = await this.http.get(`/tracks/${id}`, { - query: { - type: 'spotify' - } - }); - - return res.data.item; - } - - async listSpotify(ids: string[]): Promise { + async list( + ids: T, + ...options: T extends number ? [options?: undefined] : [options: statsfm.QueryWithIdType] + ): Promise { const res = await this.http.get(`/tracks/list`, { query: { ids: ids.join(','), - type: 'spotify' + ...options[0] } }); From 90705307835171074b0c79796e30b148106df302 Mon Sep 17 00:00:00 2001 From: Martijn-Faber Date: Thu, 5 May 2022 19:15:49 +0200 Subject: [PATCH 2/2] v2.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 35a842c..4d745a9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@statsfm/statsfm.js", - "version": "1.13.12", + "version": "2.0.0", "description": "", "main": "dist/index.js", "private": false,