From 17d48434c0adb9e4201502c73e478ac366855596 Mon Sep 17 00:00:00 2001 From: simojenki Date: Fri, 5 Mar 2021 15:54:41 +1100 Subject: [PATCH] Represent each subsonic api call as a method on Navidrome --- src/navidrome.ts | 74 +++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 38 deletions(-) diff --git a/src/navidrome.ts b/src/navidrome.ts index 3d3db8e..a4fe7b5 100644 --- a/src/navidrome.ts +++ b/src/navidrome.ts @@ -142,7 +142,17 @@ export class Navidrome implements MusicService { ) ); - artistInfo = (credentials: Credentials, id: string): Promise => + getArtists = (credentials: Credentials): Promise => + this.get(credentials, "/rest/getArtists") + .then((it) => it.artists.index.flatMap((it) => it.artist || [])) + .then((artists) => + artists.map((artist) => ({ + id: artist._id, + name: artist._name, + })) + ); + + getArtistInfo = (credentials: Credentials, id: string): Promise => this.get(credentials, "/rest/getArtistInfo", { id, }).then((it) => ({ @@ -153,6 +163,11 @@ export class Navidrome implements MusicService { }, })); + getArtist = (credentials: Credentials, id: string): Promise => + this.get(credentials, "/rest/getArtist", { + id, + }).then((it) => ({ id: it.artist._id, name: it.artist._name })); + async login(token: string) { const navidrome = this; const credentials: Credentials = this.parseToken(token); @@ -160,27 +175,22 @@ export class Navidrome implements MusicService { const musicLibrary: MusicLibrary = { artists: (q: ArtistQuery): Promise> => navidrome - .get(credentials, "/rest/getArtists") - .then((it) => it.artists.index.flatMap((it) => it.artist || [])) - .then((artists) => - artists.map((artist) => ({ - id: artist._id, - name: artist._name, - })) - ) + .getArtists(credentials) .then(slice2(q)) .then(asResult) .then((result) => Promise.all( result.results.map((idName: IdName) => - navidrome.artistInfo(credentials, idName.id).then((artist) => ({ - total: result.total, - result: { - id: idName.id, - name: idName.name, - image: artist.image, - }, - })) + navidrome + .getArtistInfo(credentials, idName.id) + .then((artistInfo) => ({ + total: result.total, + result: { + id: idName.id, + name: idName.name, + image: artistInfo.image, + }, + })) ) ) ) @@ -190,27 +200,15 @@ export class Navidrome implements MusicService { results: resultWithInfo.map((it) => it.result), }; }), - artist: async (id: string): Promise => { - return navidrome - .get(credentials, "/rest/getArtist", { - id, - }) - .then(async (artist: GetArtistResponse) => { - return navidrome - .get(credentials, "/rest/getArtistInfo", { - id, - }) - .then((artistInfo: GetArtistInfoResponse) => ({ - id: artist.artist._id, - name: artist.artist._name, - image: { - small: artistInfo.artistInfo.smallImageUrl, - medium: artistInfo.artistInfo.mediumImageUrl, - large: artistInfo.artistInfo.largeImageUrl, - }, - })); - }); - }, + artist: async (id: string): Promise => + Promise.all([ + navidrome.getArtist(credentials, id), + navidrome.getArtistInfo(credentials, id), + ]).then(([artist, artistInfo]) => ({ + id: artist.id, + name: artist.name, + image: artistInfo.image, + })), albums: (_: AlbumQuery): Promise> => { return Promise.resolve({ results: [], total: 0 }); },