From 6c10e738ec532407b2083b0e3286c999bd4a06c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20H=C3=A5rek=20Andreassen?= Date: Sun, 7 Apr 2024 00:19:09 +0200 Subject: [PATCH] refactor: Move responsibility of where fetch should happen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tim HÄrek Andreassen --- lib/nrk.ts | 5 ++--- routes/api/feeds/[seriesId].ts | 22 +++++++++++++--------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/nrk.ts b/lib/nrk.ts index f751cec..09100ae 100644 --- a/lib/nrk.ts +++ b/lib/nrk.ts @@ -37,9 +37,8 @@ async function search(query: string): Promise { return null; } -async function getSerieData( - seriesId: string, -): Promise<{ episodes: OriginalEpisode[] } & (RadioSeries["series"] | Podcast["series"]) | null> { +export type SeriesData = { episodes: OriginalEpisode[] } & (RadioSeries["series"] | Podcast["series"]); +async function getSerieData(seriesId: string): Promise { let [ { status: episodeStatus, body: episodeResponse }, { status: seriesStatus, body: serieResponse }, diff --git a/routes/api/feeds/[seriesId].ts b/routes/api/feeds/[seriesId].ts index 8729249..9a66ffd 100644 --- a/routes/api/feeds/[seriesId].ts +++ b/routes/api/feeds/[seriesId].ts @@ -2,6 +2,7 @@ import { nrkRadio, OriginalEpisode } from "../../../lib/nrk.ts"; import { FreshContext } from "$fresh/server.ts"; import { declaration, serialize, tag } from "https://raw.githubusercontent.com/olaven/serialize-xml/v0.4.0/mod.ts"; import { getHostName } from "../../../utils.ts"; +import { SeriesData } from "../../../lib/nrk.ts"; function toItemTag(seriesId: string, episode: OriginalEpisode) { const description = episode.titles.subtitle || ""; @@ -28,10 +29,9 @@ function toItemTag(seriesId: string, episode: OriginalEpisode) { ]); } -async function buildFeed(seriesId: string) { - const serie = await nrkRadio.getSerieData(seriesId); - const imageUrl = serie.squareImage?.at(-1)?.url ?? ""; - const linkValue = `https://radio.nrk.no/podkast/${serie.id}`; +function buildFeed(series: SeriesData) { + const imageUrl = series.squareImage?.at(-1)?.url ?? ""; + const linkValue = `https://radio.nrk.no/podkast/${series.id}`; // Quickly adapted from https://raw.githubusercontent.com/olaven/paperpod/1cde9abd3174b26e126aa74fc5a3b63fd078c0fd/packages/converter/src/rss.ts return serialize( @@ -43,7 +43,7 @@ async function buildFeed(seriesId: string) { "rss", [ tag("channel", [ - tag("title", serie.titles.title), + tag("title", series.titles.title), tag("link", linkValue), tag("itunes:author", "NRK"), /* serie.category.id does not overlap with Apple's supported categories.. @@ -58,7 +58,7 @@ async function buildFeed(seriesId: string) { ]), tag( "description", - serie.titles.subtitle || "", + series.titles.subtitle || "", ), tag("ttl", "60"), //60 minutes ...(imageUrl @@ -68,12 +68,12 @@ async function buildFeed(seriesId: string) { ]), tag("image", [ tag("url", imageUrl), - tag("title", serie.titles.title), + tag("title", series.titles.title), tag("link", linkValue), ]), ] : []), - ...serie.episodes.map((episode) => toItemTag(seriesId, episode)), + ...series.episodes.map((episode) => toItemTag(series.id, episode)), ]), ], [ @@ -88,8 +88,12 @@ async function buildFeed(seriesId: string) { export const handler = async (_req: Request, ctx: FreshContext): Promise => { const seriesId = ctx.params.seriesId; + const series = await nrkRadio.getSerieData(seriesId); + if (!series) { + return new Response(`Couldn't find series with seriesId: ${seriesId}`, { status: 404 }); + } try { - const feedContent = await buildFeed(seriesId); + const feedContent = buildFeed(series); return new Response(feedContent, { headers: {