import { NextRequest, NextResponse } from 'next/server'; /** * 通用的 API 代理路由 */ export async function GET( request: NextRequest, { params }: { params: Promise<{ path: string[] }> } ) { const { path } = await params; const targetPath = path.join('/'); const searchParams = request.nextUrl.search; const targetUrl = `http://localhost:8000/api/${targetPath}${searchParams}`; const headers: Record = {}; request.headers.forEach((value, key) => { if (key.toLowerCase() !== 'host') { headers[key] = value; } }); try { const response = await fetch(targetUrl, { headers }); const data = await response.json(); return NextResponse.json(data, { status: response.status }); } catch (error) { return NextResponse.json({ detail: 'Proxy error' }, { status: 502 }); } } export async function POST( request: NextRequest, { params }: { params: Promise<{ path: string[] }> } ) { const { path } = await params; const targetPath = path.join('/'); const searchParams = request.nextUrl.search; const targetUrl = `http://localhost:8000/api/${targetPath}${searchParams}`; const body = await request.text(); const headers: Record = {}; request.headers.forEach((value, key) => { if (key.toLowerCase() !== 'host') { headers[key] = value; } }); try { const response = await fetch(targetUrl, { method: 'POST', headers, body, }); const data = await response.json(); return NextResponse.json(data, { status: response.status }); } catch (error) { return NextResponse.json({ detail: 'Proxy error' }, { status: 502 }); } } export async function PUT( request: NextRequest, { params }: { params: Promise<{ path: string[] }> } ) { const { path } = await params; const targetPath = path.join('/'); const searchParams = request.nextUrl.search; const targetUrl = `http://localhost:8000/api/${targetPath}${searchParams}`; const body = await request.text(); const headers: Record = {}; request.headers.forEach((value, key) => { if (key.toLowerCase() !== 'host') { headers[key] = value; } }); try { const response = await fetch(targetUrl, { method: 'PUT', headers, body, }); const data = await response.json(); return NextResponse.json(data, { status: response.status }); } catch (error) { return NextResponse.json({ detail: 'Proxy error' }, { status: 502 }); } } export async function PATCH( request: NextRequest, { params }: { params: Promise<{ path: string[] }> } ) { const { path } = await params; const targetPath = path.join('/'); const searchParams = request.nextUrl.search; const targetUrl = `http://localhost:8000/api/${targetPath}${searchParams}`; const body = await request.text(); const headers: Record = {}; request.headers.forEach((value, key) => { if (key.toLowerCase() !== 'host') { headers[key] = value; } }); try { const response = await fetch(targetUrl, { method: 'PATCH', headers, body, }); const data = await response.json(); return NextResponse.json(data, { status: response.status }); } catch (error) { return NextResponse.json({ detail: 'Proxy error' }, { status: 502 }); } } export async function DELETE( request: NextRequest, { params }: { params: Promise<{ path: string[] }> } ) { const { path } = await params; const targetPath = path.join('/'); const searchParams = request.nextUrl.search; const targetUrl = `http://localhost:8000/api/${targetPath}${searchParams}`; const headers: Record = {}; request.headers.forEach((value, key) => { if (key.toLowerCase() !== 'host') { headers[key] = value; } }); try { const response = await fetch(targetUrl, { method: 'DELETE', headers, }); const data = await response.json(); return NextResponse.json(data, { status: response.status }); } catch (error) { return NextResponse.json({ detail: 'Proxy error' }, { status: 502 }); } }