Fix proxy — use arrayBuffer to preserve multipart boundary, fix double /api/ in transcribe URL
This commit is contained in:
@@ -2,20 +2,29 @@ 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[] }> }) {
|
||||
async function handler(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ slug: string[] }> }
|
||||
) {
|
||||
const { slug } = await params;
|
||||
const path = '/' + slug.join('/');
|
||||
const url = `${API_BASE}${path}${request.nextUrl.search}`;
|
||||
|
||||
const headers = new Headers();
|
||||
|
||||
// Forward content-type exactly (includes multipart boundary)
|
||||
const contentType = request.headers.get('content-type');
|
||||
if (contentType) headers.set('content-type', contentType);
|
||||
|
||||
// Forward session cookie
|
||||
const cookie = request.headers.get('cookie');
|
||||
if (cookie) headers.set('cookie', cookie);
|
||||
|
||||
const body = request.method !== 'GET' && request.method !== 'HEAD'
|
||||
? await request.blob()
|
||||
: undefined;
|
||||
// Forward body
|
||||
let body: BodyInit | undefined;
|
||||
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
||||
body = await request.arrayBuffer();
|
||||
}
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: request.method,
|
||||
@@ -26,7 +35,8 @@ async function handler(request: NextRequest, { params }: { params: Promise<{ slu
|
||||
const responseHeaders = new Headers();
|
||||
const setCookie = response.headers.get('set-cookie');
|
||||
if (setCookie) responseHeaders.set('set-cookie', setCookie);
|
||||
responseHeaders.set('content-type', response.headers.get('content-type') || 'application/json');
|
||||
const respContentType = response.headers.get('content-type');
|
||||
if (respContentType) responseHeaders.set('content-type', respContentType);
|
||||
|
||||
return new NextResponse(response.body, {
|
||||
status: response.status,
|
||||
|
||||
Reference in New Issue
Block a user