72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
const STRAPI_URL = process.env.STRAPI_INTERNAL || 'http://127.0.0.1:1337/api';
|
|
|
|
async function proxyRequest(req: NextRequest, params: { path: string[] }) {
|
|
const path = params.path.join('/');
|
|
const searchParams = req.nextUrl.searchParams.toString();
|
|
const url = `${STRAPI_URL}/${path}${searchParams ? `?${searchParams}` : ''}`;
|
|
|
|
try {
|
|
const headers: Record<string, string> = {
|
|
'Content-Type': 'application/json',
|
|
};
|
|
|
|
const authHeader = req.headers.get('Authorization');
|
|
if (authHeader) {
|
|
headers['Authorization'] = authHeader;
|
|
}
|
|
|
|
const fetchOptions: RequestInit = {
|
|
method: req.method,
|
|
headers,
|
|
};
|
|
|
|
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
const body = await req.text();
|
|
if (body) {
|
|
fetchOptions.body = body;
|
|
}
|
|
}
|
|
|
|
const response = await fetch(url, fetchOptions);
|
|
const data = await response.json();
|
|
|
|
return NextResponse.json(data, { status: response.status });
|
|
} catch (error) {
|
|
console.error('Strapi proxy error:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Strapi API unavailable' },
|
|
{ status: 502 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function GET(
|
|
req: NextRequest,
|
|
{ params }: { params: { path: string[] } }
|
|
) {
|
|
return proxyRequest(req, params);
|
|
}
|
|
|
|
export async function POST(
|
|
req: NextRequest,
|
|
{ params }: { params: { path: string[] } }
|
|
) {
|
|
return proxyRequest(req, params);
|
|
}
|
|
|
|
export async function PUT(
|
|
req: NextRequest,
|
|
{ params }: { params: { path: string[] } }
|
|
) {
|
|
return proxyRequest(req, params);
|
|
}
|
|
|
|
export async function DELETE(
|
|
req: NextRequest,
|
|
{ params }: { params: { path: string[] } }
|
|
) {
|
|
return proxyRequest(req, params);
|
|
}
|