feat: add initial command pallete
This commit is contained in:
47
routes/api/tmdb/[id].ts
Normal file
47
routes/api/tmdb/[id].ts
Normal file
@ -0,0 +1,47 @@
|
||||
import { HandlerContext } from "$fresh/server.ts";
|
||||
import { getMovie } from "@lib/tmdb.ts";
|
||||
import * as cache from "@lib/cache/cache.ts";
|
||||
|
||||
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,
|
||||
});
|
||||
}
|
||||
|
||||
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 });
|
||||
}
|
||||
|
||||
const res = await getMovie(+id);
|
||||
|
||||
cache.set(
|
||||
cacheId,
|
||||
JSON.stringify({
|
||||
lastUpdated: Date.now(),
|
||||
data: res,
|
||||
}),
|
||||
);
|
||||
|
||||
return new Response(JSON.stringify(res));
|
||||
};
|
46
routes/api/tmdb/credits/[id].ts
Normal file
46
routes/api/tmdb/credits/[id].ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { HandlerContext } from "$fresh/server.ts";
|
||||
import { getMovieCredits } from "@lib/tmdb.ts";
|
||||
import * as cache from "@lib/cache/cache.ts";
|
||||
|
||||
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,
|
||||
});
|
||||
}
|
||||
|
||||
const headers = new Headers();
|
||||
headers.append("Content-Type", "application/json");
|
||||
|
||||
const cacheId = `/movie/credits/${id}`;
|
||||
|
||||
const cachedResponse = await cache.get<CachedMovieCredits>(cacheId);
|
||||
if (
|
||||
cachedResponse && Date.now() < (cachedResponse.lastUpdated + CACHE_INTERVAL)
|
||||
) {
|
||||
return new Response(JSON.stringify(cachedResponse.data), { headers });
|
||||
}
|
||||
|
||||
const res = await getMovieCredits(+id);
|
||||
cache.set(
|
||||
cacheId,
|
||||
JSON.stringify({
|
||||
lastUpdated: Date.now(),
|
||||
data: res,
|
||||
}),
|
||||
);
|
||||
|
||||
return new Response(JSON.stringify(res));
|
||||
};
|
49
routes/api/tmdb/query.ts
Normal file
49
routes/api/tmdb/query.ts
Normal file
@ -0,0 +1,49 @@
|
||||
import { HandlerContext } from "$fresh/server.ts";
|
||||
import { searchMovie } from "@lib/tmdb.ts";
|
||||
import * as cache from "@lib/cache/cache.ts";
|
||||
|
||||
type CachedMovieQuery = {
|
||||
lastUpdated: number;
|
||||
data: unknown;
|
||||
};
|
||||
|
||||
const CACHE_INTERVAL = 1000 * 60 * 24 * 30;
|
||||
|
||||
export const handler = async (
|
||||
_req: Request,
|
||||
_ctx: HandlerContext,
|
||||
) => {
|
||||
const u = new URL(_req.url);
|
||||
|
||||
const query = u.searchParams.get("q");
|
||||
|
||||
if (!query) {
|
||||
return new Response("Bad Request", {
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
const headers = new Headers();
|
||||
headers.append("Content-Type", "application/json");
|
||||
|
||||
const cacheId = `/movie/query/${query}`;
|
||||
|
||||
const cachedResponse = await cache.get<CachedMovieQuery>(cacheId);
|
||||
if (
|
||||
cachedResponse && Date.now() < (cachedResponse.lastUpdated + CACHE_INTERVAL)
|
||||
) {
|
||||
return new Response(JSON.stringify(cachedResponse.data), { headers });
|
||||
}
|
||||
|
||||
const res = await searchMovie(query);
|
||||
|
||||
cache.set(
|
||||
cacheId,
|
||||
JSON.stringify({
|
||||
lastUpdated: Date.now(),
|
||||
data: res,
|
||||
}),
|
||||
);
|
||||
|
||||
return new Response(JSON.stringify(res.results));
|
||||
};
|
Reference in New Issue
Block a user