From 39c139be7b73ef3c8d69df7bec4b2f19ab782774 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20H=C3=A5rek=20Andreassen?= Date: Mon, 8 Apr 2024 18:46:04 +0200 Subject: [PATCH] refactor: Make response helpers for JSON and XML MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tim HÄrek Andreassen --- lib/utils.ts | 22 +++++++++++++++++++ routes/api/feeds/[seriesId].ts | 14 +++--------- .../feeds/[seriesId]/[episodeId]/chapters.ts | 14 +++--------- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/lib/utils.ts b/lib/utils.ts index 654e90a..f75af7a 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -1,3 +1,5 @@ +import { STATUS_CODE } from "$fresh/server.ts"; + export function getHostName() { const deploymentId = Deno.env.get("DENO_DEPLOYMENT_ID"); if (deploymentId) { @@ -7,3 +9,23 @@ export function getHostName() { return "http://localhost:8000"; } } + +type EnumValues = T[keyof T]; +type Status = EnumValues; + +export function responseJSON(body: unknown | null, status: Status) { + return response(body, status, "json"); +} + +export function responseXML(body: unknown | null, status: Status) { + return response(body, status, "xml"); +} + +function response(body: unknown | null, status: number, type: "json" | "xml") { + return new Response(JSON.stringify(body), { + status, + headers: { + "Content-Type": `application/${type}`, + }, + }); +} diff --git a/routes/api/feeds/[seriesId].ts b/routes/api/feeds/[seriesId].ts index e346a03..39e5161 100644 --- a/routes/api/feeds/[seriesId].ts +++ b/routes/api/feeds/[seriesId].ts @@ -1,24 +1,16 @@ import { FreshContext, STATUS_CODE } from "$fresh/server.ts"; import { caching } from "../../../lib/caching.ts"; import { rss } from "../../../lib/rss.ts"; +import { responseJSON, responseXML } from "../../../lib/utils.ts"; export const handler = async (_req: Request, ctx: FreshContext): Promise => { const seriesId = ctx.params.seriesId; const series = await caching.getSeries({ id: seriesId }); if (!series) { - return new Response(JSON.stringify({ message: "Series not found" }), { - status: STATUS_CODE.NotFound, - headers: { - "Content-Type": "application/xml", - }, - }); + return responseJSON({ message: "Series not found" }, STATUS_CODE.NotFound); } const feed = rss.assembleFeed(series); - return new Response(feed, { - headers: { - "Content-Type": "application/xml", - }, - }); + return responseXML(feed, STATUS_CODE.OK); }; diff --git a/routes/api/feeds/[seriesId]/[episodeId]/chapters.ts b/routes/api/feeds/[seriesId]/[episodeId]/chapters.ts index d515d04..cf2bfbb 100644 --- a/routes/api/feeds/[seriesId]/[episodeId]/chapters.ts +++ b/routes/api/feeds/[seriesId]/[episodeId]/chapters.ts @@ -1,6 +1,7 @@ import { FreshContext, STATUS_CODE } from "$fresh/server.ts"; import { parse, toSeconds } from "https://esm.sh/iso8601-duration@2.1.1"; import { NrkPodcastEpisode, nrkRadio } from "../../../../../lib/nrk/nrk.ts"; +import { responseJSON } from "../../../../../lib/utils.ts"; type Chapter = { title: string | undefined; @@ -25,12 +26,7 @@ export const handler = async (_req: Request, ctx: FreshContext): Promise