53 lines
1.6 KiB
TypeScript
53 lines
1.6 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
const API_BASE = process.env.API_URL || 'https://ai.aaronnelson.studio';
|
|
|
|
async function handler(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ slug: string[] }> }
|
|
) {
|
|
const { slug } = await params;
|
|
const slugPath = slug.join('/');
|
|
|
|
// Auth routes go directly without /api/ prefix
|
|
const isAuth = slugPath.startsWith('auth/');
|
|
const path = isAuth ? `/${slugPath}` : `/api/${slugPath}`;
|
|
|
|
const url = `${API_BASE}${path}${request.nextUrl.search}`;
|
|
|
|
const headers = new Headers();
|
|
const contentType = request.headers.get('content-type');
|
|
if (contentType) headers.set('content-type', contentType);
|
|
const cookie = request.headers.get('cookie');
|
|
if (cookie) headers.set('cookie', cookie);
|
|
|
|
let body: BodyInit | undefined;
|
|
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
|
body = await request.arrayBuffer();
|
|
}
|
|
|
|
const response = await fetch(url, {
|
|
method: request.method,
|
|
headers,
|
|
body,
|
|
});
|
|
|
|
const responseHeaders = new Headers();
|
|
const setCookie = response.headers.get('set-cookie');
|
|
if (setCookie) responseHeaders.set('set-cookie', setCookie);
|
|
const respContentType = response.headers.get('content-type');
|
|
if (respContentType) responseHeaders.set('content-type', respContentType);
|
|
|
|
return new NextResponse(response.body, {
|
|
status: response.status,
|
|
headers: responseHeaders,
|
|
});
|
|
}
|
|
|
|
export const GET = handler;
|
|
export const POST = handler;
|
|
export const PUT = handler;
|
|
export const PATCH = handler;
|
|
export const DELETE = handler;
|
|
export const OPTIONS = handler;
|