refactor: Make response helpers for JSON and XML
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
parent
4f3ebb042d
commit
39c139be7b
22
lib/utils.ts
22
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> = 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}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<Response> => {
|
||||
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);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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<Respons
|
|||
const episode = await nrkRadio.getEpisode(seriesId, episodeId);
|
||||
|
||||
if (!episode) {
|
||||
return new Response(JSON.stringify({ message: `Episode ${episodeId} is missing` }), {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
status: STATUS_CODE.NotFound,
|
||||
});
|
||||
return responseJSON({ message: `Episode ${episodeId} is missing` }, STATUS_CODE.NotFound);
|
||||
}
|
||||
const chapters = toChapters(episode);
|
||||
const body = {
|
||||
|
|
@ -38,9 +34,5 @@ export const handler = async (_req: Request, ctx: FreshContext): Promise<Respons
|
|||
chapters,
|
||||
};
|
||||
|
||||
return new Response(JSON.stringify(body), {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
return responseJSON(body, STATUS_CODE.OK);
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue