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 }); // 先检查 Content-Type,确保是 JSON const contentType = response.headers.get('content-type'); if (contentType && contentType.includes('application/json')) { const data = await response.json(); return NextResponse.json(data, { status: response.status }); } else { const text = await response.text(); return new NextResponse(text, { status: response.status, headers: { 'content-type': contentType || 'text/plain' } }); } } catch (error) { console.error('GET proxy error:', error); return NextResponse.json({ detail: 'Proxy error', error: String(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, }); // 先检查 Content-Type,确保是 JSON const contentType = response.headers.get('content-type'); if (contentType && contentType.includes('application/json')) { const data = await response.json(); return NextResponse.json(data, { status: response.status }); } else { const text = await response.text(); return new NextResponse(text, { status: response.status, headers: { 'content-type': contentType || 'text/plain' } }); } } catch (error) { console.error('POST proxy error:', error); return NextResponse.json({ detail: 'Proxy error', error: String(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 contentType = response.headers.get('content-type'); if (contentType && contentType.includes('application/json')) { const data = await response.json(); return NextResponse.json(data, { status: response.status }); } else { const text = await response.text(); return new NextResponse(text, { status: response.status, headers: { 'content-type': contentType || 'text/plain' } }); } } catch (error) { console.error('PUT proxy error:', error); return NextResponse.json({ detail: 'Proxy error', error: String(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 contentType = response.headers.get('content-type'); if (contentType && contentType.includes('application/json')) { const data = await response.json(); return NextResponse.json(data, { status: response.status }); } else { const text = await response.text(); return new NextResponse(text, { status: response.status, headers: { 'content-type': contentType || 'text/plain' } }); } } catch (error) { console.error('PATCH proxy error:', error); return NextResponse.json({ detail: 'Proxy error', error: String(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 contentType = response.headers.get('content-type'); if (contentType && contentType.includes('application/json')) { const data = await response.json(); return NextResponse.json(data, { status: response.status }); } else { const text = await response.text(); return new NextResponse(text, { status: response.status, headers: { 'content-type': contentType || 'text/plain' } }); } } catch (error) { console.error('DELETE proxy error:', error); return NextResponse.json({ detail: 'Proxy error', error: String(error) }, { status: 502 }); } }