mirror of
https://github.com/antebudimir/feishin.git
synced 2026-01-01 18:33:33 +00:00
Pass full server to controller
This commit is contained in:
parent
1cbd61888f
commit
8f042ad448
6 changed files with 144 additions and 181 deletions
|
|
@ -1,8 +1,8 @@
|
|||
import { initClient, initContract } from '@ts-rest/core';
|
||||
import axios, { Method, AxiosError, isAxiosError, AxiosResponse } from 'axios';
|
||||
import { ssType } from '/@/renderer/api/subsonic/subsonic-types';
|
||||
import { ServerListItem } from '/@/renderer/api/types';
|
||||
import { toast } from '/@/renderer/components';
|
||||
import { useAuthStore } from '/@/renderer/store';
|
||||
|
||||
const c = initContract();
|
||||
|
||||
|
|
@ -95,29 +95,24 @@ axiosClient.interceptors.response.use(
|
|||
},
|
||||
);
|
||||
|
||||
export const ssApiClient = (args: { serverId?: string; signal?: AbortSignal; url?: string }) => {
|
||||
const { serverId, url, signal } = args;
|
||||
export const ssApiClient = (args: {
|
||||
server?: ServerListItem;
|
||||
signal?: AbortSignal;
|
||||
url?: string;
|
||||
}) => {
|
||||
const { server, url, signal } = args;
|
||||
|
||||
return initClient(contract, {
|
||||
api: async ({ path, method, headers, body }) => {
|
||||
let baseUrl: string | undefined;
|
||||
const authParams: Record<string, any> = {};
|
||||
|
||||
if (serverId) {
|
||||
const selectedServer = useAuthStore.getState().actions.getServer(serverId);
|
||||
|
||||
if (!selectedServer) {
|
||||
return {
|
||||
body: { data: null, headers: null },
|
||||
status: 500,
|
||||
};
|
||||
}
|
||||
|
||||
baseUrl = `${selectedServer?.url}/rest`;
|
||||
const token = selectedServer.credential;
|
||||
if (server) {
|
||||
baseUrl = `${server.url}/rest`;
|
||||
const token = server.credential;
|
||||
const params = token.split(/&?\w=/gm);
|
||||
|
||||
authParams.u = selectedServer.username;
|
||||
authParams.u = server.username;
|
||||
if (params?.length === 4) {
|
||||
authParams.s = params[2];
|
||||
authParams.t = params[3];
|
||||
|
|
|
|||
|
|
@ -72,13 +72,9 @@ const authenticate = async (
|
|||
};
|
||||
|
||||
const getMusicFolderList = async (args: MusicFolderListArgs): Promise<MusicFolderListResponse> => {
|
||||
const { signal, serverId } = args;
|
||||
const { apiClientProps } = args;
|
||||
|
||||
if (!serverId) {
|
||||
throw new Error('No server id');
|
||||
}
|
||||
|
||||
const res = await ssApiClient({ serverId, signal }).getMusicFolderList({});
|
||||
const res = await ssApiClient(apiClientProps).getMusicFolderList({});
|
||||
|
||||
if (res.status !== 200) {
|
||||
throw new Error('Failed to get music folder list');
|
||||
|
|
@ -198,13 +194,9 @@ const getMusicFolderList = async (args: MusicFolderListArgs): Promise<MusicFolde
|
|||
// };
|
||||
|
||||
const createFavorite = async (args: FavoriteArgs): Promise<FavoriteResponse> => {
|
||||
const { serverId, query, signal } = args;
|
||||
const { query, apiClientProps } = args;
|
||||
|
||||
if (!serverId) {
|
||||
throw new Error('No server id');
|
||||
}
|
||||
|
||||
const res = await ssApiClient({ serverId, signal }).createFavorite({
|
||||
const res = await ssApiClient(apiClientProps).createFavorite({
|
||||
query: {
|
||||
albumId: query.type === LibraryItem.ALBUM ? query.id : undefined,
|
||||
artistId: query.type === LibraryItem.ALBUM_ARTIST ? query.id : undefined,
|
||||
|
|
@ -223,13 +215,9 @@ const createFavorite = async (args: FavoriteArgs): Promise<FavoriteResponse> =>
|
|||
};
|
||||
|
||||
const removeFavorite = async (args: FavoriteArgs): Promise<FavoriteResponse> => {
|
||||
const { serverId, query, signal } = args;
|
||||
const { query, apiClientProps } = args;
|
||||
|
||||
if (!serverId) {
|
||||
throw new Error('No server id');
|
||||
}
|
||||
|
||||
const res = await ssApiClient({ serverId, signal }).removeFavorite({
|
||||
const res = await ssApiClient(apiClientProps).removeFavorite({
|
||||
query: {
|
||||
albumId: query.type === LibraryItem.ALBUM ? query.id : undefined,
|
||||
artistId: query.type === LibraryItem.ALBUM_ARTIST ? query.id : undefined,
|
||||
|
|
@ -248,16 +236,12 @@ const removeFavorite = async (args: FavoriteArgs): Promise<FavoriteResponse> =>
|
|||
};
|
||||
|
||||
const setRating = async (args: RatingArgs): Promise<RatingResponse> => {
|
||||
const { serverId, query, signal } = args;
|
||||
|
||||
if (!serverId) {
|
||||
throw new Error('No server id');
|
||||
}
|
||||
const { query, apiClientProps } = args;
|
||||
|
||||
const itemIds = query.item.map((item) => item.id);
|
||||
|
||||
for (const id of itemIds) {
|
||||
await ssApiClient({ serverId, signal }).setRating({
|
||||
await ssApiClient(apiClientProps).setRating({
|
||||
query: {
|
||||
id,
|
||||
rating: query.rating,
|
||||
|
|
@ -269,13 +253,9 @@ const setRating = async (args: RatingArgs): Promise<RatingResponse> => {
|
|||
};
|
||||
|
||||
const getTopSongList = async (args: TopSongListArgs): Promise<SongListResponse> => {
|
||||
const { signal, serverId, query, server } = args;
|
||||
const { query, apiClientProps } = args;
|
||||
|
||||
if (!serverId || !server) {
|
||||
throw new Error('No server id');
|
||||
}
|
||||
|
||||
const res = await ssApiClient({ serverId, signal }).getTopSongsList({
|
||||
const res = await ssApiClient(apiClientProps).getTopSongsList({
|
||||
query: {
|
||||
artist: query.artist,
|
||||
count: query.limit,
|
||||
|
|
@ -287,7 +267,7 @@ const getTopSongList = async (args: TopSongListArgs): Promise<SongListResponse>
|
|||
}
|
||||
|
||||
return {
|
||||
items: res.body.topSongs.song.map((song) => ssNormalize.song(song, server, '')),
|
||||
items: res.body.topSongs.song.map((song) => ssNormalize.song(song, apiClientProps.server, '')),
|
||||
startIndex: 0,
|
||||
totalRecordCount: res.body.topSongs.song.length || 0,
|
||||
};
|
||||
|
|
@ -296,13 +276,9 @@ const getTopSongList = async (args: TopSongListArgs): Promise<SongListResponse>
|
|||
const getArtistInfo = async (
|
||||
args: ArtistInfoArgs,
|
||||
): Promise<z.infer<typeof ssType._response.artistInfo>> => {
|
||||
const { signal, serverId, query } = args;
|
||||
const { query, apiClientProps } = args;
|
||||
|
||||
if (!serverId) {
|
||||
throw new Error('No server id');
|
||||
}
|
||||
|
||||
const res = await ssApiClient({ serverId, signal }).getArtistInfo({
|
||||
const res = await ssApiClient(apiClientProps).getArtistInfo({
|
||||
query: {
|
||||
count: query.limit,
|
||||
id: query.artistId,
|
||||
|
|
@ -317,13 +293,9 @@ const getArtistInfo = async (
|
|||
};
|
||||
|
||||
const scrobble = async (args: ScrobbleArgs): Promise<ScrobbleResponse> => {
|
||||
const { signal, serverId, query } = args;
|
||||
const { query, apiClientProps } = args;
|
||||
|
||||
if (!serverId) {
|
||||
throw new Error('No server id');
|
||||
}
|
||||
|
||||
const res = await ssApiClient({ serverId, signal }).scrobble({
|
||||
const res = await ssApiClient(apiClientProps).scrobble({
|
||||
query: {
|
||||
id: query.id,
|
||||
submission: query.submission,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue