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,
|
||||
|
||||
+2
-2
@@ -35,7 +35,7 @@ export const api = {
|
||||
transcribe: async (audio: Blob): Promise<{ text: string }> => {
|
||||
const form = new FormData();
|
||||
form.append('audio', audio, 'recording.webm');
|
||||
const url = API_BASE ? `${API_BASE}/api/transcribe` : '/api/api/transcribe';
|
||||
const url = API_BASE ? `${API_BASE}/api/transcribe` : '/api/transcribe';
|
||||
const res = await fetch(url, { method: 'POST', credentials: 'include', body: form });
|
||||
if (!res.ok) throw new Error(`Transcribe error: ${res.status}`);
|
||||
return res.json();
|
||||
@@ -46,7 +46,7 @@ export const api = {
|
||||
if (data.image) form.append('image', data.image);
|
||||
if (data.text) form.append('text', data.text);
|
||||
if (data.project) form.append('project', data.project);
|
||||
const url = API_BASE ? `${API_BASE}/api/capture` : '/api/api/capture';
|
||||
const url = API_BASE ? `${API_BASE}/api/capture` : '/api/capture';
|
||||
const res = await fetch(url, { method: 'POST', credentials: 'include', body: form });
|
||||
if (!res.ok) throw new Error(`Capture error: ${res.status}`);
|
||||
return res.json();
|
||||
|
||||
Reference in New Issue
Block a user