
* increased timeout for custom_generate to 60 s * prevents many timeouts if wait_audio is set to true on Vercel * highest possible value to work in all Vercel tiers
70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
import { NextResponse, NextRequest } from "next/server";
|
|
import { sunoApi } from "@/lib/SunoApi";
|
|
import { corsHeaders } from "@/lib/utils";
|
|
|
|
export const maxDuration = 60; // allow longer timeout for wait_audio == true
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export async function POST(req: NextRequest) {
|
|
if (req.method === 'POST') {
|
|
try {
|
|
const body = await req.json();
|
|
const { prompt, tags, title, make_instrumental, wait_audio } = body;
|
|
if (!prompt || !tags || !title) {
|
|
return new NextResponse(JSON.stringify({ error: 'Prompt, tags, and title are required' }), {
|
|
status: 400,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...corsHeaders
|
|
}
|
|
});
|
|
}
|
|
const audioInfo = await (await sunoApi).custom_generate(
|
|
prompt, tags, title,
|
|
make_instrumental == true,
|
|
wait_audio == true
|
|
);
|
|
return new NextResponse(JSON.stringify(audioInfo), {
|
|
status: 200,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...corsHeaders
|
|
}
|
|
});
|
|
} catch (error: any) {
|
|
console.error('Error generating custom audio:', error.response.data);
|
|
if (error.response.status === 402) {
|
|
return new NextResponse(JSON.stringify({ error: error.response.data.detail }), {
|
|
status: 402,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...corsHeaders
|
|
}
|
|
});
|
|
}
|
|
return new NextResponse(JSON.stringify({ error: 'Internal server error' }), {
|
|
status: 500,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...corsHeaders
|
|
}
|
|
});
|
|
}
|
|
} else {
|
|
return new NextResponse('Method Not Allowed', {
|
|
headers: {
|
|
Allow: 'POST',
|
|
...corsHeaders
|
|
},
|
|
status: 405
|
|
});
|
|
}
|
|
}
|
|
|
|
export async function OPTIONS(request: Request) {
|
|
return new Response(null, {
|
|
status: 200,
|
|
headers: corsHeaders
|
|
});
|
|
}
|