diff --git a/src/lib/apis/sonarr/sonarrApi.ts b/src/lib/apis/sonarr/sonarrApi.ts index f17ba2d..d1e289d 100644 --- a/src/lib/apis/sonarr/sonarrApi.ts +++ b/src/lib/apis/sonarr/sonarrApi.ts @@ -7,7 +7,6 @@ import { get } from 'svelte/store'; import { settings } from '$lib/stores/settings.store'; export type SonarrSeries = components['schemas']['SeriesResource']; -// export type MovieFileResource = components['schemas']['MovieFileResource']; export type SonarrReleaseResource = components['schemas']['ReleaseResource']; export type SonarrDownload = components['schemas']['QueueResource'] & { series: SonarrSeries }; export type DiskSpaceInfo = components['schemas']['DiskSpaceResource']; @@ -39,32 +38,38 @@ export interface SonarrSeriesOptions { }; } -export const SonarrApi = createClient({ - baseUrl: env.PUBLIC_SONARR_BASE_URL, - headers: { - 'X-Api-Key': env.PUBLIC_SONARR_API_KEY - } -}); +export const sonarrAvailable = !!env.PUBLIC_SONARR_BASE_URL && !!env.PUBLIC_SONARR_API_KEY; + +export const SonarrApi = + env.PUBLIC_SONARR_BASE_URL && env.PUBLIC_SONARR_API_KEY + ? createClient({ + baseUrl: env.PUBLIC_SONARR_BASE_URL, + headers: { + 'X-Api-Key': env.PUBLIC_SONARR_API_KEY + } + }) + : undefined; export const getSonarrSeries = (): Promise => - SonarrApi.get('/api/v3/series', { + SonarrApi?.get('/api/v3/series', { params: {} - }).then((r) => r.data || []); + }).then((r) => r.data || []) || Promise.resolve([]); -export const getSonarrSeriesByTvdbId = (tvdbId: number): Promise => - SonarrApi.get('/api/v3/series', { - params: { - query: { - tvdbId: tvdbId - } - } - }).then((r) => r.data?.find((m) => m.tvdbId === tvdbId)); +// export const getSonarrSeriesByTvdbId = (tvdbId: number): Promise => +// SonarrApi?.get('/api/v3/series', { +// params: { +// query: { +// tvdbId: tvdbId +// } +// } +// }).then((r) => r.data?.find((m) => m.tvdbId === tvdbId)) || Promise.resolve(undefined); -export const getRadarrDownloadById = (sonarrId: number) => - getSonarrDownloads().then((downloads) => downloads.find((d) => d.series.id === sonarrId)); +// export const getRadarrDownloadById = (sonarrId: number) => +// getSonarrDownloads().then((downloads) => downloads.find((d) => d.series.id === sonarrId)) || +// Promise.resolve(undefined); export const getDiskSpace = (): Promise => - SonarrApi.get('/api/v3/diskspace', {}).then((d) => d.data || []); + SonarrApi?.get('/api/v3/diskspace', {}).then((d) => d.data || []) || Promise.resolve([]); export const addSeriesToSonarr = async (tmdbId: number) => { const tmdbSeries = await getTmdbSeries(tmdbId); @@ -87,48 +92,48 @@ export const addSeriesToSonarr = async (tmdbId: number) => { seasonFolder: true }; - return SonarrApi.post('/api/v3/series', { + return SonarrApi?.post('/api/v3/series', { params: {}, body: options }).then((r) => r.data); }; -export const cancelDownloadSonarrEpisode = async (downloadId: number) => { - const deleteResponse = await SonarrApi.del('/api/v3/queue/{id}', { - params: { - path: { - id: downloadId - }, - query: { - blocklist: false, - removeFromClient: true - } - } - }).then((r) => log(r)); +// export const cancelDownloadSonarrEpisode = async (downloadId: number) => { +// const deleteResponse = await SonarrApi?.del('/api/v3/queue/{id}', { +// params: { +// path: { +// id: downloadId +// }, +// query: { +// blocklist: false, +// removeFromClient: true +// } +// } +// }).then((r) => log(r)); - return deleteResponse.response.ok; -}; +// return !!deleteResponse?.response.ok; +// }; export const downloadSonarrEpisode = (guid: string) => - SonarrApi.post('/api/v3/release', { + SonarrApi?.post('/api/v3/release', { params: {}, body: { indexerId: 2, guid } - }); + }).then((res) => res.response.ok) || Promise.resolve(false); -export const deleteSonarrEpisode = (id: number) => - SonarrApi.del('/api/v3/episodefile/{id}', { - params: { - path: { - id - } - } - }).then((res) => res.response.ok); +// export const deleteSonarrEpisode = (id: number) => +// SonarrApi?.del('/api/v3/episodefile/{id}', { +// params: { +// path: { +// id +// } +// } +// }).then((res) => res.response.ok) || Promise.resolve(false); export const getSonarrDownloads = (): Promise => - SonarrApi.get('/api/v3/queue', { + SonarrApi?.get('/api/v3/queue', { params: { query: { includeEpisode: true, @@ -139,70 +144,76 @@ export const getSonarrDownloads = (): Promise => (r) => (r.data?.records?.filter((record) => record.episode && record.series) as SonarrDownload[]) || [] - ); + ) || Promise.resolve([]); -export const getSonarrDownloadsById = (sonarrId: number) => - getSonarrDownloads().then((downloads) => downloads.filter((d) => d.seriesId === sonarrId)); +// export const getSonarrDownloadsById = (sonarrId: number) => +// getSonarrDownloads().then((downloads) => downloads.filter((d) => d.seriesId === sonarrId)) || +// Promise.resolve([]); -export const removeFromSonarr = (id: number) => - SonarrApi.del('/api/v3/series/{id}', { - params: { - path: { - id - } - } - }).then((res) => res.response.ok); +// export const removeFromSonarr = (id: number): Promise => +// SonarrApi?.del('/api/v3/series/{id}', { +// params: { +// path: { +// id +// } +// } +// }).then((res) => res.response.ok) || Promise.resolve(false); -export const getSonarrEpisodes = async (seriesId: number) => { - const episodesPromise = SonarrApi.get('/api/v3/episode', { - params: { - query: { - seriesId - } - } - }).then((r) => r.data || []); +// export const getSonarrEpisodes = async (seriesId: number) => { +// const episodesPromise = +// SonarrApi?.get('/api/v3/episode', { +// params: { +// query: { +// seriesId +// } +// } +// }).then((r) => r.data || []) || Promise.resolve([]); - const episodeFilesPromise = SonarrApi.get('/api/v3/episodefile', { - params: { - query: { - seriesId - } - } - }).then((r) => r.data || []); +// const episodeFilesPromise = +// SonarrApi?.get('/api/v3/episodefile', { +// params: { +// query: { +// seriesId +// } +// } +// }).then((r) => r.data || []) || Promise.resolve([]); - const [episodes, episodeFiles] = await Promise.all([episodesPromise, episodeFilesPromise]); +// const episodes = await episodesPromise; +// const episodeFiles = await episodeFilesPromise; - return episodes.map((episode) => ({ - episode, - episodeFile: episodeFiles.find((file) => file.id === episode.episodeFileId) - })); -}; +// return episodes.map((episode) => ({ +// episode, +// episodeFile: episodeFiles.find((file) => file.id === episode.episodeFileId) +// })); +// }; export const fetchSonarrReleases = async (episodeId: number) => - SonarrApi.get('/api/v3/release', { + SonarrApi?.get('/api/v3/release', { params: { query: { episodeId } } - }).then((r) => r.data || []); + }).then((r) => r.data || []) || Promise.resolve([]); export const fetchSonarrSeasonReleases = async (seriesId: number, seasonNumber: number) => - SonarrApi.get('/api/v3/release', { + SonarrApi?.get('/api/v3/release', { params: { query: { seriesId, seasonNumber } } - }).then((r) => r.data || []); + }).then((r) => r.data || []) || Promise.resolve([]); export const fetchSonarrEpisodes = async (seriesId: number): Promise => { - return SonarrApi.get('/api/v3/episode', { - params: { - query: { - seriesId + return ( + SonarrApi?.get('/api/v3/episode', { + params: { + query: { + seriesId + } } - } - }).then((r) => r.data || []); + }).then((r) => r.data || []) || Promise.resolve([]) + ); }; diff --git a/src/lib/components/RequestModal/RequestModal.svelte b/src/lib/components/RequestModal/RequestModal.svelte index ce1313a..c5e398b 100644 --- a/src/lib/components/RequestModal/RequestModal.svelte +++ b/src/lib/components/RequestModal/RequestModal.svelte @@ -77,10 +77,10 @@ } }); } else { - downloadSonarrEpisode(guid).then((res) => { + downloadSonarrEpisode(guid).then((ok) => { dispatch('download'); downloadFetchingGuid = undefined; - if (res.response?.ok) { + if (ok) { downloadingGuid = guid; } }); diff --git a/src/lib/components/SourceStats/SonarrStats.svelte b/src/lib/components/SourceStats/SonarrStats.svelte index c072e4a..68e9b09 100644 --- a/src/lib/components/SourceStats/SonarrStats.svelte +++ b/src/lib/components/SourceStats/SonarrStats.svelte @@ -1,12 +1,11 @@ diff --git a/src/routes/series/[id]/SeriesPage.svelte b/src/routes/series/[id]/SeriesPage.svelte index 20a038c..1deb40b 100644 --- a/src/routes/series/[id]/SeriesPage.svelte +++ b/src/routes/series/[id]/SeriesPage.svelte @@ -1,6 +1,6 @@