1+ import { NextResponse , NextRequest } from "next/server" ;
2+ import { DEFAULT_MODEL , sunoApi } from "@/lib/SunoApi" ;
3+ import { corsHeaders } from "@/lib/utils" ;
4+
5+ export const dynamic = "force-dynamic" ;
6+
7+ export async function POST ( req : NextRequest ) {
8+ if ( req . method === 'POST' ) {
9+ try {
10+ const body = await req . json ( ) ;
11+ const { audio_id } = body ;
12+
13+ if ( ! audio_id ) {
14+ return new NextResponse ( JSON . stringify ( { error : 'Audio ID is required' } ) , {
15+ status : 400 ,
16+ headers : {
17+ 'Content-Type' : 'application/json' ,
18+ ...corsHeaders
19+ }
20+ } ) ;
21+ }
22+
23+ const audioInfo = await ( await sunoApi )
24+ . generateStems ( audio_id ) ;
25+
26+ return new NextResponse ( JSON . stringify ( audioInfo ) , {
27+ status : 200 ,
28+ headers : {
29+ 'Content-Type' : 'application/json' ,
30+ ...corsHeaders
31+ }
32+ } ) ;
33+ } catch ( error : any ) {
34+ console . error ( 'Error generating stems:' , JSON . stringify ( error . response . data ) ) ;
35+ if ( error . response . status === 402 ) {
36+ return new NextResponse ( JSON . stringify ( { error : error . response . data . detail } ) , {
37+ status : 402 ,
38+ headers : {
39+ 'Content-Type' : 'application/json' ,
40+ ...corsHeaders
41+ }
42+ } ) ;
43+ }
44+ return new NextResponse ( JSON . stringify ( { error : 'Internal server error: ' + JSON . stringify ( error . response . data . detail ) } ) , {
45+ status : 500 ,
46+ headers : {
47+ 'Content-Type' : 'application/json' ,
48+ ...corsHeaders
49+ }
50+ } ) ;
51+ }
52+ } else {
53+ return new NextResponse ( 'Method Not Allowed' , {
54+ headers : {
55+ Allow : 'POST' ,
56+ ...corsHeaders
57+ } ,
58+ status : 405
59+ } ) ;
60+ }
61+ }
62+
63+
64+ export async function OPTIONS ( request : Request ) {
65+ return new Response ( null , {
66+ status : 200 ,
67+ headers : corsHeaders
68+ } ) ;
69+ }
0 commit comments