refactor: remove some duplicated code

This commit is contained in:
2023-08-01 17:50:00 +02:00
parent 01697a6686
commit c5cf629482
30 changed files with 377 additions and 321 deletions

View File

@ -1,6 +1,7 @@
import { HandlerContext } from "$fresh/server.ts";
import { HandlerContext, Handlers } from "$fresh/server.ts";
import { getMovie } from "@lib/tmdb.ts";
import * as cache from "@lib/cache/cache.ts";
import { json } from "@lib/helpers.ts";
type CachedMovieCredits = {
lastUpdated: number;
@ -9,7 +10,7 @@ type CachedMovieCredits = {
const CACHE_INTERVAL = 1000 * 60 * 24 * 30;
export const handler = async (
const GET = async (
_req: Request,
_ctx: HandlerContext,
) => {
@ -21,16 +22,13 @@ export const handler = async (
});
}
const headers = new Headers();
headers.append("Content-Type", "application/json");
const cacheId = `/movie/${id}`;
const cachedResponse = await cache.get<CachedMovieCredits>(cacheId);
if (
cachedResponse && Date.now() < (cachedResponse.lastUpdated + CACHE_INTERVAL)
) {
return new Response(JSON.stringify(cachedResponse.data), { headers });
return json(cachedResponse.data);
}
const res = await getMovie(+id);
@ -43,5 +41,9 @@ export const handler = async (
}),
);
return new Response(JSON.stringify(res));
return json(res);
};
export const handler: Handlers = {
GET,
};