this repo has no description
7
fork

Configure Feed

Select the types of activity you want to include in your feed.

kill cors

rimar1337 2efece03 7a794e8d

+43 -7
+43 -7
src/xrpc.ts
··· 5 5 serve(async (req) => { 6 6 const url = new URL(req.url); 7 7 8 + if (req.method === "OPTIONS") { 9 + return new Response(null, { 10 + status: 204, 11 + headers: corsHeaders(), 12 + }); 13 + } 14 + 8 15 if (url.pathname === "/xrpc/com.example.prototypeESQuery") { 9 16 let esQuery: any; 10 17 ··· 12 19 try { 13 20 esQuery = await req.json(); 14 21 } catch { 15 - return new Response("Invalid JSON body", { status: 400 }); 22 + return new Response("Invalid JSON body", { 23 + status: 400, 24 + headers: corsHeaders(), 25 + }); 16 26 } 17 27 } else if (req.method === "GET") { 18 28 const queryParam = url.searchParams.get("q"); 19 29 if (!queryParam) { 20 - return new Response("Missing 'q'", { status: 400 }); 30 + return new Response("Missing 'q'", { 31 + status: 400, 32 + headers: corsHeaders(), 33 + }); 21 34 } 22 35 23 36 try { 24 37 esQuery = JSON.parse(queryParam); 25 38 } catch { 26 - return new Response("Invalid JSON in 'q'", { status: 400 }); 39 + return new Response("Invalid JSON in 'q'", { 40 + status: 400, 41 + headers: corsHeaders(), 42 + }); 27 43 } 28 44 } else { 29 - return new Response("Method not allowed", { status: 405 }); 45 + return new Response("Method not allowed", { 46 + status: 405, 47 + headers: corsHeaders(), 48 + }); 30 49 } 31 50 32 51 const esRes = await fetch(`${config.es_url}/${config.index_name}/_search`, { ··· 37 56 38 57 if (!esRes.ok) { 39 58 const errText = await esRes.text(); 40 - return new Response(`Elasticsearch error: ${errText}`, { status: 500 }); 59 + return new Response(`Elasticsearch error: ${errText}`, { 60 + status: 500, 61 + headers: corsHeaders(), 62 + }); 41 63 } 42 64 43 65 const result = await esRes.json(); 44 - return Response.json(result.hits.hits.map((hit: any) => hit._source)); 66 + return new Response(JSON.stringify(result), { 67 + status: 200, 68 + headers: { 69 + ...corsHeaders(), 70 + "Content-Type": "application/json", 71 + }, 72 + }); 45 73 } 46 74 47 - return new Response("Not found", { status: 404 }); 75 + return new Response("Not found", { status: 404, headers: corsHeaders() }); 48 76 }, { port: Number(config.serve_port) }); 77 + } 78 + 79 + function corsHeaders(): HeadersInit { 80 + return { 81 + "Access-Control-Allow-Origin": "*", 82 + "Access-Control-Allow-Methods": "GET,POST,OPTIONS", 83 + "Access-Control-Allow-Headers": "Content-Type", 84 + }; 49 85 }