diff --git a/src/api/subsonic.ts b/src/api/subsonic.ts index f58a7c47..16bd33ad 100644 --- a/src/api/subsonic.ts +++ b/src/api/subsonic.ts @@ -9,7 +9,7 @@ import { import { SUBSONIC_CLIENT, api, - getAuthParams, + apiWithCredentials, secureRandomSalt, } from './subsonicClient'; import type { PingWithCredentialsResult, SubsonicSong } from './subsonicTypes'; @@ -53,31 +53,6 @@ export async function pingWithCredentials( } } -function restBaseFromUrl(serverUrl: string): string { - const base = serverUrl.startsWith('http') ? serverUrl.replace(/\/$/, '') : `http://${serverUrl.replace(/\/$/, '')}`; - return `${base}/rest`; -} - -async function apiWithCredentials( - serverUrl: string, - username: string, - password: string, - endpoint: string, - extra: Record = {}, - timeout = 15000, -): Promise { - const params = { ...getAuthParams(username, password), ...extra }; - const resp = await axios.get(`${restBaseFromUrl(serverUrl)}/${endpoint}`, { - params, - paramsSerializer: { indexes: null }, - timeout, - }); - const data = resp.data?.['subsonic-response']; - if (!data) throw new Error('Invalid response from server (possibly not a Subsonic server)'); - if (data.status !== 'ok') throw new Error(data.error?.message ?? 'Subsonic API error'); - return data as T; -} - const INSTANT_MIX_PROBE_RANDOM_SIZE = 8; const INSTANT_MIX_PROBE_SIMILAR_COUNT = 12; const INSTANT_MIX_PROBE_MAX_TRACKS = 4; diff --git a/src/api/subsonicClient.ts b/src/api/subsonicClient.ts index 32bda47a..7b000ced 100644 --- a/src/api/subsonicClient.ts +++ b/src/api/subsonicClient.ts @@ -17,6 +17,31 @@ export function getAuthParams(username: string, password: string) { return { u: username, t: token, s: salt, v: '1.16.1', c: SUBSONIC_CLIENT, f: 'json' }; } +export function restBaseFromUrl(serverUrl: string): string { + const base = serverUrl.startsWith('http') ? serverUrl.replace(/\/$/, '') : `http://${serverUrl.replace(/\/$/, '')}`; + return `${base}/rest`; +} + +export async function apiWithCredentials( + serverUrl: string, + username: string, + password: string, + endpoint: string, + extra: Record = {}, + timeout = 15000, +): Promise { + const params = { ...getAuthParams(username, password), ...extra }; + const resp = await axios.get(`${restBaseFromUrl(serverUrl)}/${endpoint}`, { + params, + paramsSerializer: { indexes: null }, + timeout, + }); + const data = resp.data?.['subsonic-response']; + if (!data) throw new Error('Invalid response from server (possibly not a Subsonic server)'); + if (data.status !== 'ok') throw new Error(data.error?.message ?? 'Subsonic API error'); + return data as T; +} + export function getClient() { const { getBaseUrl, getActiveServer } = useAuthStore.getState(); const server = getActiveServer();