mirror of
https://github.com/antebudimir/feishin.git
synced 2026-01-01 10:23:33 +00:00
Additional refactor for navidrome api controller types
This commit is contained in:
parent
b9d5447b4f
commit
40aabd2217
7 changed files with 53 additions and 48 deletions
|
|
@ -1,8 +1,9 @@
|
|||
import { initClient, initContract } from '@ts-rest/core';
|
||||
import axios, { Method, AxiosError, isAxiosError, AxiosResponse } from 'axios';
|
||||
import { z } from 'zod';
|
||||
import { ssType } from '/@/renderer/api/subsonic/subsonic-types';
|
||||
import { ServerListItem } from '/@/renderer/api/types';
|
||||
import { toast } from '/@/renderer/components';
|
||||
import { toast } from '/@/renderer/components/toast/index';
|
||||
|
||||
const c = initContract();
|
||||
|
||||
|
|
@ -88,7 +89,7 @@ axiosClient.interceptors.response.use(
|
|||
}
|
||||
}
|
||||
|
||||
return data['subsonic-response'];
|
||||
return response;
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
|
|
@ -96,7 +97,7 @@ axiosClient.interceptors.response.use(
|
|||
);
|
||||
|
||||
export const ssApiClient = (args: {
|
||||
server?: ServerListItem;
|
||||
server: ServerListItem | null;
|
||||
signal?: AbortSignal;
|
||||
url?: string;
|
||||
}) => {
|
||||
|
|
@ -124,7 +125,7 @@ export const ssApiClient = (args: {
|
|||
}
|
||||
|
||||
try {
|
||||
const result = await axiosClient.request({
|
||||
const result = await axiosClient.request<z.infer<typeof ssType._response.baseResponse>>({
|
||||
data: body,
|
||||
headers,
|
||||
method: method as Method,
|
||||
|
|
@ -137,8 +138,9 @@ export const ssApiClient = (args: {
|
|||
signal,
|
||||
url: `${baseUrl}/${path}`,
|
||||
});
|
||||
|
||||
return {
|
||||
body: { data: result.data, headers: result.headers },
|
||||
body: result.data['subsonic-response'],
|
||||
status: result.status,
|
||||
};
|
||||
} catch (e: Error | AxiosError | any) {
|
||||
|
|
@ -146,7 +148,7 @@ export const ssApiClient = (args: {
|
|||
const error = e as AxiosError;
|
||||
const response = error.response as AxiosResponse;
|
||||
return {
|
||||
body: { data: response.data, headers: response.headers },
|
||||
body: response.data,
|
||||
status: response.status,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ const authenticate = async (
|
|||
};
|
||||
}
|
||||
|
||||
await ssApiClient({ url: cleanServerUrl }).authenticate({
|
||||
await ssApiClient({ server: null, url: cleanServerUrl }).authenticate({
|
||||
query: {
|
||||
c: 'Feishin',
|
||||
f: 'json',
|
||||
|
|
@ -261,9 +261,11 @@ const getTopSongList = async (args: TopSongListArgs): Promise<SongListResponse>
|
|||
}
|
||||
|
||||
return {
|
||||
items: res.body.topSongs.song.map((song) => ssNormalize.song(song, apiClientProps.server, '')),
|
||||
items:
|
||||
res.body.topSongs?.song?.map((song) => ssNormalize.song(song, apiClientProps.server, '')) ||
|
||||
[],
|
||||
startIndex: 0,
|
||||
totalRecordCount: res.body.topSongs.song.length || 0,
|
||||
totalRecordCount: res.body.topSongs?.song?.length || 0,
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -5,9 +5,9 @@ import { QueueSong, LibraryItem } from '/@/renderer/api/types';
|
|||
import { ServerListItem, ServerType } from '/@/renderer/types';
|
||||
|
||||
const getCoverArtUrl = (args: {
|
||||
baseUrl: string;
|
||||
baseUrl: string | undefined;
|
||||
coverArtId?: string;
|
||||
credential: string;
|
||||
credential: string | undefined;
|
||||
size: number;
|
||||
}) => {
|
||||
const size = args.size ? args.size : 150;
|
||||
|
|
@ -28,18 +28,18 @@ const getCoverArtUrl = (args: {
|
|||
|
||||
const normalizeSong = (
|
||||
item: z.infer<typeof ssType._response.song>,
|
||||
server: ServerListItem,
|
||||
server: ServerListItem | null,
|
||||
deviceId: string,
|
||||
): QueueSong => {
|
||||
const imageUrl =
|
||||
getCoverArtUrl({
|
||||
baseUrl: server.url,
|
||||
baseUrl: server?.url,
|
||||
coverArtId: item.coverArt,
|
||||
credential: server.credential,
|
||||
size: 300,
|
||||
credential: server?.credential,
|
||||
size: 100,
|
||||
}) || null;
|
||||
|
||||
const streamUrl = `${server.url}/rest/stream.view?id=${item.id}&v=1.13.0&c=feishin_${deviceId}&${server.credential}`;
|
||||
const streamUrl = `${server?.url}/rest/stream.view?id=${item.id}&v=1.13.0&c=feishin_${deviceId}&${server?.credential}`;
|
||||
|
||||
return {
|
||||
album: item.album || '',
|
||||
|
|
@ -86,7 +86,7 @@ const normalizeSong = (
|
|||
playCount: item?.playCount || 0,
|
||||
releaseDate: null,
|
||||
releaseYear: item.year ? String(item.year) : null,
|
||||
serverId: server.id,
|
||||
serverId: server?.id || 'unknown',
|
||||
serverType: ServerType.SUBSONIC,
|
||||
size: item.size,
|
||||
streamUrl,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue