Add slice2 and asResult functions

This commit is contained in:
simojenki
2021-03-02 10:16:56 +11:00
parent 3aa1056aa5
commit bdee01d2c7
6 changed files with 86 additions and 74 deletions

View File

@@ -42,8 +42,21 @@ export type Paging = {
_count?: number;
};
export type Result<T> = {
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]
}
export const asResult = <T>([results, total]: [T[], number]) => ({ results, total })
export interface MusicLibrary {
artists({ _index, _count }: Paging): Promise<[Artist[], number]>;
artists({ _index, _count }: Paging): Promise<Result<Artist>>;
artist(id: string): Artist;
albums({
artistId,
@@ -51,5 +64,5 @@ export interface MusicLibrary {
_count,
}: {
artistId?: string;
} & Paging): Promise<[Album[], number]>;
} & Paging): Promise<Result<Album>>;
}

View File

@@ -1,5 +1,5 @@
import { Md5 } from "ts-md5/dist/md5";
import { Credentials, MusicService, Paging, Album, Artist } from "./music_service";
import { Credentials, MusicService, Paging, Album, Artist, Result, slice2, asResult } from "./music_service";
import X2JS from "x2js";
import axios from "axios";
@@ -98,18 +98,15 @@ export class Navidrome implements MusicService {
const navidrome = this;
const credentials: Credentials = this.parseToken(token);
return Promise.resolve({
artists: ({ _index, _count }: Paging): Promise<[Artist[], number]> =>
artists: (paging: Paging): 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((artists) => {
const i0 = _index || 0;
const i1 = _count ? i0 + _count : undefined;
return [artists.slice(i0, i1), artists.length];
}),
.then(slice2(paging))
.then(asResult),
artist: (id: string) => ({
id,
name: id,
@@ -118,9 +115,9 @@ export class Navidrome implements MusicService {
artistId,
}: {
artistId?: string;
} & Paging): Promise<[Album[], number]> => {
} & Paging): Promise<Result<Album>> => {
console.log(artistId);
return Promise.resolve([[], 0]);
return Promise.resolve({ results: [], total: 0});
},
});
}

View File

@@ -6,7 +6,7 @@ import path from "path";
import logger from "./logger";
import { LinkCodes } from "./link_codes";
import { MusicLibrary, MusicService } from "./music_service";
import { Artist, MusicLibrary, MusicService } from "./music_service";
export const LOGIN_ROUTE = "/login";
export const SOAP_PATH = "/ws/sonos";
@@ -227,9 +227,9 @@ function bindSmapiSoapServiceToExpress(
case "artists":
return await musicLibrary
.artists(paging)
.then(([artists, total]) =>
.then(({ results, total }: { results: Artist[], total: number}) =>
getMetadataResult({
mediaCollection: artists.map((it) =>
mediaCollection: results.map((it) =>
container({
id: `artist:${it.id}`,
title: it.name,
@@ -242,9 +242,9 @@ function bindSmapiSoapServiceToExpress(
case "albums":
return await musicLibrary
.albums(paging)
.then(([albums, total]) =>
.then(({ results, total }: { results: Artist[], total: number}) =>
getMetadataResult({
mediaCollection: albums.map((it) =>
mediaCollection: results.map((it) =>
container({
id: `album:${it.id}`,
title: it.name,