refactor: Split up functions
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
parent
0434448d45
commit
f48a4145b4
|
|
@ -20,17 +20,17 @@ export type SearchResult = ArrayElement<NrkSearchResultList> & {
|
|||
};
|
||||
export type SeriesData = { episodes: NrkOriginalEpisode[] } & (RadioSeries["series"] | Podcast["series"]);
|
||||
|
||||
function parseSeries(nrkSeries: SeriesData): Series {
|
||||
const imageUrl = nrkSeries.squareImage?.at(-1)?.url ?? "";
|
||||
function parseSeries(nrkSeriesData: SeriesData): Series {
|
||||
const imageUrl = nrkSeriesData.squareImage?.at(-1)?.url ?? "";
|
||||
|
||||
return {
|
||||
id: nrkSeries.id,
|
||||
title: nrkSeries.titles.title,
|
||||
subtitle: nrkSeries.titles.subtitle ?? null,
|
||||
link: `https://radio.nrk.no/podkast/${nrkSeries.id}`,
|
||||
id: nrkSeriesData.id,
|
||||
title: nrkSeriesData.titles.title,
|
||||
subtitle: nrkSeriesData.titles.subtitle ?? null,
|
||||
link: `https://radio.nrk.no/podkast/${nrkSeriesData.id}`,
|
||||
imageUrl: imageUrl,
|
||||
lastFetchedAt: new Date(),
|
||||
episodes: nrkSeries.episodes.map((episode) => {
|
||||
episodes: nrkSeriesData.episodes.map((episode) => {
|
||||
return {
|
||||
id: episode.id,
|
||||
title: episode.titles.title,
|
||||
|
|
@ -62,7 +62,7 @@ async function search(query: string): Promise<NrkSearchResultList | null> {
|
|||
return null;
|
||||
}
|
||||
|
||||
async function getSeries(seriesId: string): Promise<Series | null> {
|
||||
async function getSeriesData(seriesId: string): Promise<SeriesData | null> {
|
||||
let [
|
||||
{ status: episodeStatus, body: episodeResponse },
|
||||
{ status: seriesStatus, body: serieResponse },
|
||||
|
|
@ -100,8 +100,7 @@ async function getSeries(seriesId: string): Promise<Series | null> {
|
|||
...serieResponse.series,
|
||||
episodes,
|
||||
};
|
||||
const parsedSeries = parseSeries(seriesData);
|
||||
return parsedSeries;
|
||||
return seriesData;
|
||||
}
|
||||
console.error(
|
||||
`Error getting episodes for ${seriesId}: EpisodeStatus: ${episodeStatus}. SerieStatus: ${seriesStatus}`,
|
||||
|
|
@ -109,6 +108,15 @@ async function getSeries(seriesId: string): Promise<Series | null> {
|
|||
return null;
|
||||
}
|
||||
|
||||
async function getSeries(seriesId: string): Promise<Series | null> {
|
||||
const seriesData = await getSeriesData(seriesId);
|
||||
if (!seriesData) {
|
||||
return null;
|
||||
}
|
||||
const parsedSeries = parseSeries(seriesData);
|
||||
return parsedSeries;
|
||||
}
|
||||
|
||||
async function getEpisode(seriesId: string, episodeId: string): Promise<NrkPodcastEpisode | null> {
|
||||
const url = `${nrkAPI}/radio/catalog/podcast/${seriesId}/episodes/${episodeId}`;
|
||||
const { status, body: episode } = await get<NrkPodcastEpisode>(url);
|
||||
|
|
@ -151,3 +159,7 @@ export const nrkRadio = {
|
|||
getEpisode,
|
||||
parseSeries,
|
||||
};
|
||||
|
||||
export const forTestingOnly = {
|
||||
getSeriesData,
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue