aboutsummaryrefslogtreecommitdiff
path: root/apps/web/app/api/canvasai/route.ts
blob: 6c557ba6809763d2b39c05db3a889684a3471645 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import type { NextRequest } from "next/server";
import { ensureAuth } from "../ensureAuth";
import { SourcesFromApi } from "@repo/shared-types";

export const runtime = "edge";

export async function POST(req: NextRequest) {
	const session = await ensureAuth(req);

	if (!session) {
		return new Response("Unauthorized", { status: 401 });
	}

	const res: { query: string } = await req.json();

	const response = await fetch(
		`${process.env.BACKEND_BASE_URL}/api/chat?query=${res.query}&user=${session.user.id}&sourcesOnly=true`,
		{
			headers: {
				Authorization: `Bearer ${process.env.BACKEND_SECURITY_KEY}`,
				"Content-Type": "application/json",
			},
			method: "POST",
			body: JSON.stringify({}),
		},
	);

	const data = (await response.json()) as SourcesFromApi;
	console.log(data);
	return new Response(JSON.stringify(data), { status: 200 });
}