(req: NextRequest)
| 76 | const headers = { "Content-Type": "application/json" }; |
| 77 | |
| 78 | export default async function handler(req: NextRequest) { |
| 79 | try { |
| 80 | console.log("/api/generate-alternative-questions called!"); |
| 81 | if (req.method === "OPTIONS") { |
| 82 | // Handle CORS preflight request |
| 83 | return new Response(undefined, { status: 200 }); |
| 84 | } |
| 85 | if (req.method !== "POST") { |
| 86 | // Handle non-POST requests |
| 87 | return new Response( |
| 88 | JSON.stringify({ |
| 89 | error: "Only POST requests allowed", |
| 90 | }), |
| 91 | { status: 405, headers }, |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | // Validate that the request body is of the correct format |
| 96 | const requestData = await req.json(); |
| 97 | if ( |
| 98 | !isValidBody<EmbedAndSaveAlternativesType>( |
| 99 | requestData, |
| 100 | EmbedAndSaveAlternativesZod, |
| 101 | ) |
| 102 | ) { |
| 103 | return new Response(JSON.stringify({ message: "Invalid request body" }), { |
| 104 | status: 400, |
| 105 | headers, |
| 106 | }); |
| 107 | } |
| 108 | console.log("Req body", requestData); |
| 109 | |
| 110 | let supabase = serviceLevelSupabase; |
| 111 | let { session, supabase: localSupabase } = await getSessionFromCookie(req); |
| 112 | if (localSupabase) supabase = localSupabase; |
| 113 | if (!session) { |
| 114 | return new Response(JSON.stringify({ error: "Unauthorized" }), { |
| 115 | status: 401, |
| 116 | headers, |
| 117 | }); |
| 118 | } |
| 119 | |
| 120 | // Check that the user hasn't surpassed the production rate limit (protects DB query below) |
| 121 | if (ratelimitProduction) { |
| 122 | // If over limit, success is false |
| 123 | const { success } = await ratelimitProduction.limit(session.user.id); |
| 124 | if (!success) { |
| 125 | return new Response( |
| 126 | JSON.stringify({ error: "Rate limit hit (30 requests/10s)" }), |
| 127 | { |
| 128 | status: 429, |
| 129 | headers, |
| 130 | }, |
| 131 | ); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | const authRes = await supabase |
nothing calls this directly
no test coverage detected