(req: NextRequest)
| 35 | }; |
| 36 | |
| 37 | export default async function handler(req: NextRequest): Promise<Response> { |
| 38 | try { |
| 39 | // Handle non-POST requests |
| 40 | if (req.method !== "GET") { |
| 41 | return new Response( |
| 42 | JSON.stringify({ |
| 43 | error: "Only GET requests allowed", |
| 44 | }), |
| 45 | { status: 405, headers }, |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | // Authenticate that the user is allowed to use this API |
| 50 | const orgApiKey = req.headers |
| 51 | .get("Authorization") |
| 52 | ?.replace("Bearer ", "") |
| 53 | .replace("bearer ", ""); |
| 54 | |
| 55 | if (!orgApiKey) { |
| 56 | return new Response(JSON.stringify({ error: "Authentication failed" }), { |
| 57 | status: 401, |
| 58 | headers, |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | let org: OrgJoinIsPaidFinetunedModels | null = null; |
| 63 | if (orgApiKey) { |
| 64 | const authRes = await supabase |
| 65 | .from("organizations") |
| 66 | .select("*, is_paid(*), finetuned_models(*)") |
| 67 | .eq("api_key", orgApiKey); |
| 68 | if (authRes.error) throw new Error(authRes.error.message); |
| 69 | org = authRes.data?.[0] ?? null; |
| 70 | } |
| 71 | if (!org) { |
| 72 | return new Response(JSON.stringify({ error: "Authentication failed" }), { |
| 73 | status: 401, |
| 74 | headers, |
| 75 | }); |
| 76 | } |
| 77 | if (!org.chat_to_docs_enabled) { |
| 78 | return new Response( |
| 79 | JSON.stringify({ |
| 80 | error: "Chat to docs is not enabled for this organization", |
| 81 | }), |
| 82 | { |
| 83 | status: 401, |
| 84 | headers, |
| 85 | }, |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | // Validate that the request body is of the correct format |
| 90 | const requestData = new URLSearchParams(req.url.split("?")[1]); |
| 91 | if (!requestData.has("query")) { |
| 92 | return new Response(JSON.stringify({ message: "Invalid request body" }), { |
| 93 | status: 400, |
| 94 | headers, |
nothing calls this directly
no test coverage detected