mirror of
https://github.com/wkulhanek/bonob.git
synced 2025-12-21 17:33:29 +01:00
AlbumQuery and ArtistQuery types on MusicService
This commit is contained in:
@@ -38,31 +38,35 @@ export type Album = {
|
||||
};
|
||||
|
||||
export type Paging = {
|
||||
_index?: number;
|
||||
_count?: number;
|
||||
_index: number;
|
||||
_count: number;
|
||||
};
|
||||
|
||||
export type Result<T> = {
|
||||
results: T[],
|
||||
total: number
|
||||
}
|
||||
results: T[];
|
||||
total: number;
|
||||
};
|
||||
|
||||
export function slice2<T>({ _index, _count }: Paging) {
|
||||
const i0 = _index || 0;
|
||||
const i1 = _count ? i0 + _count : undefined;
|
||||
return (things: T[]): [T[], number] => [things.slice(i0, i1), things.length]
|
||||
return (things: T[]): [T[], number] => [
|
||||
things.slice(_index, _index + _count),
|
||||
things.length,
|
||||
];
|
||||
}
|
||||
|
||||
export const asResult = <T>([results, total]: [T[], number]) => ({ results, total })
|
||||
export const asResult = <T>([results, total]: [T[], number]) => ({
|
||||
results,
|
||||
total,
|
||||
});
|
||||
|
||||
export type ArtistQuery = Paging
|
||||
|
||||
export type AlbumQuery = Paging & {
|
||||
artistId?: string
|
||||
}
|
||||
|
||||
export interface MusicLibrary {
|
||||
artists({ _index, _count }: Paging): Promise<Result<Artist>>;
|
||||
artists(q: ArtistQuery): Promise<Result<Artist>>;
|
||||
artist(id: string): Artist;
|
||||
albums({
|
||||
artistId,
|
||||
_index,
|
||||
_count,
|
||||
}: {
|
||||
artistId?: string;
|
||||
} & Paging): Promise<Result<Album>>;
|
||||
albums(q: AlbumQuery): Promise<Result<Album>>;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Md5 } from "ts-md5/dist/md5";
|
||||
import { Credentials, MusicService, Paging, Album, Artist, Result, slice2, asResult } from "./music_service";
|
||||
import { Credentials, MusicService, Album, Artist, Result, slice2, asResult, AlbumQuery, ArtistQuery } from "./music_service";
|
||||
import X2JS from "x2js";
|
||||
|
||||
import axios from "axios";
|
||||
@@ -98,25 +98,20 @@ export class Navidrome implements MusicService {
|
||||
const navidrome = this;
|
||||
const credentials: Credentials = this.parseToken(token);
|
||||
return Promise.resolve({
|
||||
artists: (paging: Paging): Promise<Result<Artist>> =>
|
||||
artists: (q: ArtistQuery): Promise<Result<Artist>> =>
|
||||
navidrome
|
||||
.get<GetArtistsResponse>(credentials, "/rest/getArtists")
|
||||
.then((it) => it.artists.index.flatMap((it) => it.artist))
|
||||
.then((artists) =>
|
||||
artists.map((it) => ({ id: it._id, name: it._name }))
|
||||
)
|
||||
.then(slice2(paging))
|
||||
.then(slice2(q))
|
||||
.then(asResult),
|
||||
artist: (id: string) => ({
|
||||
id,
|
||||
name: id,
|
||||
}),
|
||||
albums: ({
|
||||
artistId,
|
||||
}: {
|
||||
artistId?: string;
|
||||
} & Paging): Promise<Result<Album>> => {
|
||||
console.log(artistId);
|
||||
albums: (_: AlbumQuery): Promise<Result<Album>> => {
|
||||
return Promise.resolve({ results: [], total: 0});
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user