refactor: Make response helpers for JSON and XML

Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
Tim Hårek Andreassen 2024-04-08 18:46:04 +02:00
parent 4f3ebb042d
commit 39c139be7b
No known key found for this signature in database
GPG Key ID: E59C7734F0E10EB5
3 changed files with 28 additions and 22 deletions

View File

@ -1,3 +1,5 @@
import { STATUS_CODE } from "$fresh/server.ts";
export function getHostName() { export function getHostName() {
const deploymentId = Deno.env.get("DENO_DEPLOYMENT_ID"); const deploymentId = Deno.env.get("DENO_DEPLOYMENT_ID");
if (deploymentId) { if (deploymentId) {
@ -7,3 +9,23 @@ export function getHostName() {
return "http://localhost:8000"; return "http://localhost:8000";
} }
} }
type EnumValues<T> = T[keyof T];
type Status = EnumValues<typeof STATUS_CODE>;
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}`,
},
});
}

View File

@ -1,24 +1,16 @@
import { FreshContext, STATUS_CODE } from "$fresh/server.ts"; import { FreshContext, STATUS_CODE } from "$fresh/server.ts";
import { caching } from "../../../lib/caching.ts"; import { caching } from "../../../lib/caching.ts";
import { rss } from "../../../lib/rss.ts"; import { rss } from "../../../lib/rss.ts";
import { responseJSON, responseXML } from "../../../lib/utils.ts";
export const handler = async (_req: Request, ctx: FreshContext): Promise<Response> => { export const handler = async (_req: Request, ctx: FreshContext): Promise<Response> => {
const seriesId = ctx.params.seriesId; const seriesId = ctx.params.seriesId;
const series = await caching.getSeries({ id: seriesId }); const series = await caching.getSeries({ id: seriesId });
if (!series) { if (!series) {
return new Response(JSON.stringify({ message: "Series not found" }), { return responseJSON({ message: "Series not found" }, STATUS_CODE.NotFound);
status: STATUS_CODE.NotFound,
headers: {
"Content-Type": "application/xml",
},
});
} }
const feed = rss.assembleFeed(series); const feed = rss.assembleFeed(series);
return new Response(feed, { return responseXML(feed, STATUS_CODE.OK);
headers: {
"Content-Type": "application/xml",
},
});
}; };

View File

@ -1,6 +1,7 @@
import { FreshContext, STATUS_CODE } from "$fresh/server.ts"; import { FreshContext, STATUS_CODE } from "$fresh/server.ts";
import { parse, toSeconds } from "https://esm.sh/iso8601-duration@2.1.1"; import { parse, toSeconds } from "https://esm.sh/iso8601-duration@2.1.1";
import { NrkPodcastEpisode, nrkRadio } from "../../../../../lib/nrk/nrk.ts"; import { NrkPodcastEpisode, nrkRadio } from "../../../../../lib/nrk/nrk.ts";
import { responseJSON } from "../../../../../lib/utils.ts";
type Chapter = { type Chapter = {
title: string | undefined; title: string | undefined;
@ -25,12 +26,7 @@ export const handler = async (_req: Request, ctx: FreshContext): Promise<Respons
const episode = await nrkRadio.getEpisode(seriesId, episodeId); const episode = await nrkRadio.getEpisode(seriesId, episodeId);
if (!episode) { if (!episode) {
return new Response(JSON.stringify({ message: `Episode ${episodeId} is missing` }), { return responseJSON({ message: `Episode ${episodeId} is missing` }, STATUS_CODE.NotFound);
headers: {
"Content-Type": "application/json",
},
status: STATUS_CODE.NotFound,
});
} }
const chapters = toChapters(episode); const chapters = toChapters(episode);
const body = { const body = {
@ -38,9 +34,5 @@ export const handler = async (_req: Request, ctx: FreshContext): Promise<Respons
chapters, chapters,
}; };
return new Response(JSON.stringify(body), { return responseJSON(body, STATUS_CODE.OK);
headers: {
"Content-Type": "application/json",
},
});
}; };