Files
memorium/routes/api/tmdb/query.ts
2025-11-07 18:58:23 +01:00

34 lines
727 B
TypeScript

import { FreshContext, Handlers } from "$fresh/server.ts";
import { searchMovie, searchTVShow } from "@lib/tmdb.ts";
import { AccessDeniedError, BadRequestError } from "@lib/errors.ts";
const GET = async (
req: Request,
ctx: FreshContext,
) => {
const session = ctx.state.session;
if (!session) {
throw new AccessDeniedError();
}
const u = new URL(req.url);
const query = u.searchParams.get("q");
if (!query) {
throw new BadRequestError();
}
const type = u.searchParams.get("type") || "movies";
const res = type === "movies"
? await searchMovie(query)
: await searchTVShow(query);
return new Response(JSON.stringify(res.results));
};
export const handler: Handlers = {
GET,
};