|
| 1 | +import { NextRequest, NextResponse } from "next/server"; |
| 2 | + |
| 3 | +interface SentryResponse { |
| 4 | + error?: string; |
| 5 | + message?: string; |
| 6 | +} |
| 7 | + |
| 8 | +const corsHeaders = { |
| 9 | + "Access-Control-Allow-Origin": "*", |
| 10 | + "Access-Control-Allow-Methods": "GET, POST, OPTIONS", |
| 11 | + "Access-Control-Allow-Headers": "Content-Type", |
| 12 | + "Cache-Control": "public, max-age=300", |
| 13 | +}; |
| 14 | + |
| 15 | +export async function POST(req: NextRequest) { |
| 16 | + try { |
| 17 | + const sentryDsn = |
| 18 | + "https://8296abef723d97e7b8d5a15d1e93be1f@o4509816683823104.ingest.us.sentry.io/4509831375683584"; |
| 19 | + |
| 20 | + const dsn = new URL(sentryDsn); |
| 21 | + const projectId = dsn.pathname.split("/").pop(); |
| 22 | + if (!projectId) { |
| 23 | + throw new Error("Invalid SENTRY_DSN: missing project ID"); |
| 24 | + } |
| 25 | + |
| 26 | + const sentryIngestUrl = `https://${dsn.host}/api/${projectId}/envelope/`; |
| 27 | + |
| 28 | + const clientIp = |
| 29 | + req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || ""; |
| 30 | + |
| 31 | + const newRequest = new Request(sentryIngestUrl, { |
| 32 | + method: "POST", |
| 33 | + headers: { |
| 34 | + "Content-Type": "application/x-sentry-envelope", |
| 35 | + "X-Forwarded-For": clientIp, |
| 36 | + }, |
| 37 | + body: await req.arrayBuffer(), |
| 38 | + }); |
| 39 | + |
| 40 | + const response = await fetch(newRequest); |
| 41 | + console.error("Sentry proxy response status:", response.status); |
| 42 | + |
| 43 | + return new Response(response.body, { |
| 44 | + status: response.status, |
| 45 | + headers: { |
| 46 | + ...corsHeaders, |
| 47 | + "Content-Type": "application/x-sentry-envelope", |
| 48 | + }, |
| 49 | + }); |
| 50 | + } catch (error: unknown) { |
| 51 | + console.error("Sentry proxy failed:", error); |
| 52 | + const errorMessage = |
| 53 | + error instanceof Error ? error.message : "Unknown error"; |
| 54 | + return NextResponse.json( |
| 55 | + { |
| 56 | + error: "Internal server error", |
| 57 | + message: `Failed to proxy Sentry request: ${errorMessage}`, |
| 58 | + } as SentryResponse, |
| 59 | + { |
| 60 | + status: 500, |
| 61 | + headers: corsHeaders, |
| 62 | + } |
| 63 | + ); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +export async function OPTIONS() { |
| 68 | + return new Response(null, { |
| 69 | + status: 204, |
| 70 | + headers: corsHeaders, |
| 71 | + }); |
| 72 | +} |
0 commit comments