53 lines
1.1 KiB
TypeScript
Raw Normal View History

2025-01-06 16:14:29 +01:00
import { FreshContext } from "$fresh/server.ts";
2023-07-31 04:19:04 +02:00
import { getMovieCredits } from "@lib/tmdb.ts";
2023-08-01 17:50:00 +02:00
import { json } from "@lib/helpers.ts";
import { createLogger } from "@lib/log/index.ts";
2025-01-06 16:14:29 +01:00
import { createCache } from "@lib/cache.ts";
2023-07-31 04:19:04 +02:00
type CachedMovieCredits = {
lastUpdated: number;
data: unknown;
};
const CACHE_INTERVAL = 1000 * 60 * 24 * 30;
const cache = createCache<CachedMovieCredits>("movie-credits", {
expires: CACHE_INTERVAL,
});
2023-07-31 04:19:04 +02:00
2023-08-05 22:16:14 +02:00
const log = createLogger("api/tmdb");
2023-07-31 04:19:04 +02:00
export const handler = async (
_req: Request,
2025-01-06 16:14:29 +01:00
_ctx: FreshContext,
2023-07-31 04:19:04 +02:00
) => {
const id = _ctx.params.id;
if (!id) {
return new Response("Bad Request", {
status: 400,
});
}
2023-08-05 22:16:14 +02:00
log.debug("getting movie credits");
2023-07-31 04:19:04 +02:00
const cacheId = `/movie/credits/${id}`;
2025-01-06 16:14:29 +01:00
const cachedResponse = cache.get(cacheId);
2023-07-31 04:19:04 +02:00
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
};