Files
s2f/frontend/app/api/[...path]/route.ts
T
freedakgmail b93929eb1a feat: UI优化和功能完善
- 添加公共分页组件,支持上边显示、分页大小10
- 侧边栏/Header按Liner风格优化,添加动画效果
- 异常处理列表支持分页和明细弹窗
- 任务结果页面支持已匹配列表分页
- 修复Dialog弹窗背景透明问题
- 新增dropdown-menu组件
- 添加parsed_file_record模型和回填脚本
- 前端枚举中文化
2026-07-07 12:57:15 +08:00

204 lines
6.1 KiB
TypeScript

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<string, string> = {};
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<string, string> = {};
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<string, string> = {};
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<string, string> = {};
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<string, string> = {};
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 });
}
}