import { NextRequest, NextResponse } from "next/server"; import { applyAuthResponseCookies, buildBackendRequestHeaders, } from "@/lib/backend-auth"; import { buildProxyExceptionResponse, buildUpstreamErrorResponse, } from "@/lib/api-proxy"; const API_BASE = process.env.POLYWEATHER_API_BASE_URL; export async function POST(req: NextRequest) { if (!API_BASE) { return NextResponse.json( { error: "POLYWEATHER_API_BASE_URL is not configured" }, { status: 500 }, ); } try { const body = await req.text(); const auth = await buildBackendRequestHeaders(req); const headers = new Headers(auth.headers); headers.set("Content-Type", "application/json"); const res = await fetch(`${API_BASE}/api/auth/telegram/bind-by-token`, { method: "POST", headers, body, cache: "no-store", }); if (!res.ok) { const raw = await res.text(); const response = buildUpstreamErrorResponse(res.status, raw); return applyAuthResponseCookies(response, auth.response); } const data = await res.json(); const response = NextResponse.json(data); return applyAuthResponseCookies(response, auth.response); } catch (error) { return buildProxyExceptionResponse(error, { publicMessage: "Failed to bind Telegram account", }); } }