diff options
| author | Yash <[email protected]> | 2024-04-11 04:52:44 +0000 |
|---|---|---|
| committer | Yash <[email protected]> | 2024-04-11 04:52:44 +0000 |
| commit | 6dcc7d18c9be5e3a5e0a3ff60668424ee0158b4e (patch) | |
| tree | 179aa936536510cc707368fc7c330c4c7fbdc3f8 /apps/web/src/app/api/ask | |
| parent | novel editor (diff) | |
| parent | save user ID with url to ensure that same website can be saved by users (diff) | |
| download | supermemory-new-ui.tar.xz supermemory-new-ui.zip | |
Merge branch 'main' of https://github.com/Dhravya/supermemory into new-uinew-ui
Diffstat (limited to 'apps/web/src/app/api/ask')
| -rw-r--r-- | apps/web/src/app/api/ask/route.ts | 80 |
1 files changed, 50 insertions, 30 deletions
diff --git a/apps/web/src/app/api/ask/route.ts b/apps/web/src/app/api/ask/route.ts index cad7a671..89123ac9 100644 --- a/apps/web/src/app/api/ask/route.ts +++ b/apps/web/src/app/api/ask/route.ts @@ -7,42 +7,62 @@ import { env } from "@/env"; export const runtime = "edge"; export async function POST(req: NextRequest) { - const token = req.cookies.get("next-auth.session-token")?.value ?? req.cookies.get("__Secure-authjs.session-token")?.value ?? req.cookies.get("authjs.session-token")?.value ?? req.headers.get("Authorization")?.replace("Bearer ", ""); + const token = + req.cookies.get("next-auth.session-token")?.value ?? + req.cookies.get("__Secure-authjs.session-token")?.value ?? + req.cookies.get("authjs.session-token")?.value ?? + req.headers.get("Authorization")?.replace("Bearer ", ""); - const sessionData = await db.select().from(sessions).where(eq(sessions.sessionToken, token!)) + const sessionData = await db + .select() + .from(sessions) + .where(eq(sessions.sessionToken, token!)); - if (!sessionData || sessionData.length === 0) { - return new Response(JSON.stringify({ message: "Invalid Key, session not found." }), { status: 404 }); - } + if (!sessionData || sessionData.length === 0) { + return new Response( + JSON.stringify({ message: "Invalid Key, session not found." }), + { status: 404 }, + ); + } - const user = await db.select().from(users).where(eq(users.id, sessionData[0].userId)).limit(1) + const user = await db + .select() + .from(users) + .where(eq(users.id, sessionData[0].userId)) + .limit(1); - if (!user || user.length === 0) { - return NextResponse.json({ message: "Invalid Key, session not found." }, { status: 404 }); - } + if (!user || user.length === 0) { + return NextResponse.json( + { message: "Invalid Key, session not found." }, + { status: 404 }, + ); + } - const body = await req.json() as { - query: string; - } + const body = (await req.json()) as { + query: string; + }; - const resp = await fetch(`https://cf-ai-backend.dhravya.workers.dev/ask`, { - headers: { - "X-Custom-Auth-Key": env.BACKEND_SECURITY_KEY, - }, - method: "POST", - body: JSON.stringify({ - query: body.query, - }), - }) + const resp = await fetch(`https://cf-ai-backend.dhravya.workers.dev/ask`, { + headers: { + "X-Custom-Auth-Key": env.BACKEND_SECURITY_KEY, + }, + method: "POST", + body: JSON.stringify({ + query: body.query, + }), + }); - if (resp.status !== 200 || !resp.ok) { - const errorData = await resp.json(); - return new Response(JSON.stringify({ message: "Error in CF function", error: errorData }), { status: resp.status }); - } + if (resp.status !== 200 || !resp.ok) { + const errorData = await resp.json(); + return new Response( + JSON.stringify({ message: "Error in CF function", error: errorData }), + { status: resp.status }, + ); + } - // Stream the response back to the client - const { readable, writable } = new TransformStream(); - resp && resp.body!.pipeTo(writable); + // Stream the response back to the client + const { readable, writable } = new TransformStream(); + resp && resp.body!.pipeTo(writable); - return new Response(readable, { status: 200 }); -}
\ No newline at end of file + return new Response(readable, { status: 200 }); +} |