2023-07-31 04:19:04 +02:00
|
|
|
import { HandlerContext } from "$fresh/server.ts";
|
|
|
|
import { getMovieCredits } from "@lib/tmdb.ts";
|
|
|
|
import * as cache from "@lib/cache/cache.ts";
|
2023-08-01 17:50:00 +02:00
|
|
|
import { json } from "@lib/helpers.ts";
|
2023-07-31 04:19:04 +02:00
|
|
|
|
|
|
|
type CachedMovieCredits = {
|
|
|
|
lastUpdated: number;
|
|
|
|
data: unknown;
|
|
|
|
};
|
|
|
|
|
|
|
|
const CACHE_INTERVAL = 1000 * 60 * 24 * 30;
|
|
|
|
|
|
|
|
export const handler = async (
|
|
|
|
_req: Request,
|
|
|
|
_ctx: HandlerContext,
|
|
|
|
) => {
|
|
|
|
const id = _ctx.params.id;
|
|
|
|
|
|
|
|
if (!id) {
|
|
|
|
return new Response("Bad Request", {
|
|
|
|
status: 400,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-01 03:15:15 +02:00
|
|
|
console.log("[api] getting movie credits");
|
|
|
|
|
2023-07-31 04:19:04 +02:00
|
|
|
const cacheId = `/movie/credits/${id}`;
|
|
|
|
|
|
|
|
const cachedResponse = await cache.get<CachedMovieCredits>(cacheId);
|
|
|
|
if (
|
|
|
|
cachedResponse && Date.now() < (cachedResponse.lastUpdated + CACHE_INTERVAL)
|
|
|
|
) {
|
2023-08-01 17:50:00 +02:00
|
|
|
return json(cachedResponse.data);
|
2023-07-31 04:19:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const res = await getMovieCredits(+id);
|
|
|
|
cache.set(
|
|
|
|
cacheId,
|
|
|
|
JSON.stringify({
|
|
|
|
lastUpdated: Date.now(),
|
|
|
|
data: res,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2023-08-01 17:50:00 +02:00
|
|
|
return json(res);
|
2023-07-31 04:19:04 +02:00
|
|
|
};
|