49 lines
1005 B
TypeScript
49 lines
1005 B
TypeScript
import { getMovie } from "@lib/tmdb.ts";
|
|
import { json } from "@lib/helpers.ts";
|
|
import { createCache } from "@lib/cache.ts";
|
|
import { define } from "../../../utils.ts";
|
|
|
|
type CachedMovieCredits = {
|
|
lastUpdated: number;
|
|
data: unknown;
|
|
};
|
|
|
|
const CACHE_INTERVAL = 1000 * 60 * 24 * 30;
|
|
const cache = createCache<CachedMovieCredits>("movie-credits", {
|
|
expires: CACHE_INTERVAL,
|
|
});
|
|
|
|
export const handler = define.handlers({
|
|
GET: async (ctx) => {
|
|
const id = ctx.params.id;
|
|
|
|
if (!id) {
|
|
return new Response("Bad Request", {
|
|
status: 400,
|
|
});
|
|
}
|
|
|
|
const cacheId = `/movie/${id}`;
|
|
|
|
const cachedResponse = cache.get(cacheId);
|
|
if (
|
|
cachedResponse &&
|
|
Date.now() < (cachedResponse.lastUpdated + CACHE_INTERVAL)
|
|
) {
|
|
return json(cachedResponse.data);
|
|
}
|
|
|
|
const res = await getMovie(+id);
|
|
|
|
cache.set(
|
|
cacheId,
|
|
JSON.stringify({
|
|
lastUpdated: Date.now(),
|
|
data: res,
|
|
}),
|
|
);
|
|
|
|
return json(res);
|
|
},
|
|
});
|