diff --git a/src/app/api/custom_generate/route.ts b/src/app/api/custom_generate/route.ts index dcd2104..12e9964 100644 --- a/src/app/api/custom_generate/route.ts +++ b/src/app/api/custom_generate/route.ts @@ -1,6 +1,9 @@ import { NextResponse, NextRequest } from "next/server"; import { sunoApi } from "@/lib/SunoApi"; +import { corsHeaders } from "@/lib/utils"; + export const dynamic = "force-dynamic"; + export async function POST(req: NextRequest) { if (req.method === 'POST') { try { @@ -9,7 +12,10 @@ export async function POST(req: NextRequest) { if (!prompt || !tags || !title) { return new NextResponse(JSON.stringify({ error: 'Prompt, tags, and title are required' }), { status: 400, - headers: { 'Content-Type': 'application/json' } + headers: { + 'Content-Type': 'application/json', + ...corsHeaders + } }); } const audioInfo = await (await sunoApi).custom_generate( @@ -19,25 +25,44 @@ export async function POST(req: NextRequest) { ); return new NextResponse(JSON.stringify(audioInfo), { status: 200, - headers: { 'Content-Type': 'application/json' } + 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' } + headers: { + 'Content-Type': 'application/json', + ...corsHeaders + } }); } return new NextResponse(JSON.stringify({ error: 'Internal server error' }), { status: 500, - headers: { 'Content-Type': 'application/json' } + headers: { + 'Content-Type': 'application/json', + ...corsHeaders + } }); } } else { return new NextResponse('Method Not Allowed', { - headers: { Allow: 'POST' }, + headers: { + Allow: 'POST', + ...corsHeaders + }, status: 405 }); } +} + +export async function OPTIONS(request: Request) { + return new Response(null, { + status: 200, + headers: corsHeaders + }); } \ No newline at end of file diff --git a/src/app/api/generate/route.ts b/src/app/api/generate/route.ts index ea8d06a..3469f41 100644 --- a/src/app/api/generate/route.ts +++ b/src/app/api/generate/route.ts @@ -1,6 +1,9 @@ import { NextResponse, NextRequest } from "next/server"; import { sunoApi } from "@/lib/SunoApi"; +import { corsHeaders } from "@/lib/utils"; + export const dynamic = "force-dynamic"; + export async function POST(req: NextRequest) { if (req.method === 'POST') { try { @@ -10,7 +13,10 @@ export async function POST(req: NextRequest) { if (!prompt) { return new NextResponse(JSON.stringify({ error: 'Prompt is required' }), { status: 400, - headers: { 'Content-Type': 'application/json' } + headers: { + 'Content-Type': 'application/json', + ...corsHeaders + } }); } @@ -18,25 +24,45 @@ export async function POST(req: NextRequest) { return new NextResponse(JSON.stringify(audioInfo), { status: 200, - headers: { 'Content-Type': 'application/json' } + headers: { + 'Content-Type': 'application/json', + ...corsHeaders + } }); } catch (error: any) { console.error('Error generating custom audio:', JSON.stringify(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' } + headers: { + 'Content-Type': 'application/json', + ...corsHeaders + } }); } return new NextResponse(JSON.stringify({ error: 'Internal server error: ' + JSON.stringify(error.response.data.detail) }), { status: 500, - headers: { 'Content-Type': 'application/json' } + headers: { + 'Content-Type': 'application/json', + ...corsHeaders + } }); } } else { return new NextResponse('Method Not Allowed', { - headers: { Allow: 'POST' }, + headers: { + Allow: 'POST', + ...corsHeaders + }, status: 405 }); } +} + + +export async function OPTIONS(request: Request) { + return new Response(null, { + status: 200, + headers: corsHeaders + }); } \ No newline at end of file diff --git a/src/app/api/generate_lyrics/route.ts b/src/app/api/generate_lyrics/route.ts index 732ae6b..91360ad 100644 --- a/src/app/api/generate_lyrics/route.ts +++ b/src/app/api/generate_lyrics/route.ts @@ -1,6 +1,9 @@ import { NextResponse, NextRequest } from "next/server"; import { sunoApi } from "@/lib/SunoApi"; +import { corsHeaders } from "@/lib/utils"; + export const dynamic = "force-dynamic"; + export async function POST(req: NextRequest) { if (req.method === 'POST') { try { @@ -10,7 +13,10 @@ export async function POST(req: NextRequest) { if (!prompt) { return new NextResponse(JSON.stringify({ error: 'Prompt is required' }), { status: 400, - headers: { 'Content-Type': 'application/json' } + headers: { + 'Content-Type': 'application/json', + ...corsHeaders + } }); } @@ -18,25 +24,44 @@ export async function POST(req: NextRequest) { return new NextResponse(JSON.stringify(lyrics), { status: 200, - headers: { 'Content-Type': 'application/json' } + headers: { + 'Content-Type': 'application/json', + ...corsHeaders + } }); } catch (error: any) { console.error('Error generating lyrics:', JSON.stringify(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' } + headers: { + 'Content-Type': 'application/json', + ...corsHeaders + } }); } return new NextResponse(JSON.stringify({ error: 'Internal server error: ' + JSON.stringify(error.response.data.detail) }), { status: 500, - headers: { 'Content-Type': 'application/json' } + headers: { + 'Content-Type': 'application/json', + ...corsHeaders + } }); } } else { return new NextResponse('Method Not Allowed', { - headers: { Allow: 'POST' }, + headers: { + Allow: 'POST', + ...corsHeaders + }, status: 405 }); } +} + +export async function OPTIONS(request: Request) { + return new Response(null, { + status: 200, + headers: corsHeaders + }); } \ No newline at end of file diff --git a/src/app/api/get/route.ts b/src/app/api/get/route.ts index 991e0be..f3f34f2 100644 --- a/src/app/api/get/route.ts +++ b/src/app/api/get/route.ts @@ -1,5 +1,7 @@ import { NextResponse, NextRequest } from "next/server"; import { sunoApi } from "@/lib/SunoApi"; +import { corsHeaders } from "@/lib/utils"; + export const dynamic = "force-dynamic"; export async function GET(req: NextRequest) { @@ -17,20 +19,36 @@ export async function GET(req: NextRequest) { return new NextResponse(JSON.stringify(audioInfo), { status: 200, - headers: { 'Content-Type': 'application/json' } + headers: { + 'Content-Type': 'application/json', + ...corsHeaders + } }); } catch (error) { console.error('Error fetching audio:', error); return new NextResponse(JSON.stringify({ error: 'Internal server error' }), { status: 500, - headers: { 'Content-Type': 'application/json' } + headers: { + 'Content-Type': 'application/json', + ...corsHeaders + } }); } } else { return new NextResponse('Method Not Allowed', { - headers: { Allow: 'GET' }, + headers: { + Allow: 'GET', + ...corsHeaders + }, status: 405 }); } +} + +export async function OPTIONS(request: Request) { + return new Response(null, { + status: 200, + headers: corsHeaders + }); } \ No newline at end of file diff --git a/src/app/api/get_limit/route.ts b/src/app/api/get_limit/route.ts index 9568a44..514c920 100644 --- a/src/app/api/get_limit/route.ts +++ b/src/app/api/get_limit/route.ts @@ -1,6 +1,9 @@ import { NextResponse, NextRequest } from "next/server"; import { sunoApi } from "@/lib/SunoApi"; +import { corsHeaders } from "@/lib/utils"; + export const dynamic = "force-dynamic"; + export async function GET(req: NextRequest) { if (req.method === 'GET') { try { @@ -10,20 +13,36 @@ export async function GET(req: NextRequest) { return new NextResponse(JSON.stringify(limit), { status: 200, - headers: { 'Content-Type': 'application/json' } + headers: { + 'Content-Type': 'application/json', + ...corsHeaders + } }); } catch (error) { console.error('Error fetching limit:', error); return new NextResponse(JSON.stringify({ error: 'Internal server error. ' + error }), { status: 500, - headers: { 'Content-Type': 'application/json' } + headers: { + 'Content-Type': 'application/json', + ...corsHeaders + } }); } } else { return new NextResponse('Method Not Allowed', { - headers: { Allow: 'GET' }, + headers: { + Allow: 'GET', + ...corsHeaders + }, status: 405 }); } +} + +export async function OPTIONS(request: Request) { + return new Response(null, { + status: 200, + headers: corsHeaders + }); } \ No newline at end of file diff --git a/src/app/v1/chat/completions/route.ts b/src/app/v1/chat/completions/route.ts index cd9e37b..e2f44fe 100644 --- a/src/app/v1/chat/completions/route.ts +++ b/src/app/v1/chat/completions/route.ts @@ -1,5 +1,6 @@ import { NextResponse, NextRequest } from "next/server"; import { sunoApi } from "@/lib/SunoApi"; +import { corsHeaders } from "@/lib/utils"; export const dynamic = "force-dynamic"; @@ -25,9 +26,7 @@ export async function POST(req: NextRequest) { status: 400, headers: { 'Content-Type': 'application/json', - 'Access-Control-Allow-Origin': '*', - 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', - 'Access-Control-Allow-Headers': 'Content-Type, Authorization', + ...corsHeaders } }); } @@ -40,11 +39,7 @@ export async function POST(req: NextRequest) { return new NextResponse(data, { status: 200, - headers: { - 'Access-Control-Allow-Origin': '*', - 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', - 'Access-Control-Allow-Headers': 'Content-Type, Authorization', - } + headers: corsHeaders }); } catch (error: any) { console.error('Error generating audio:', JSON.stringify(error.response.data)); @@ -52,9 +47,7 @@ export async function POST(req: NextRequest) { status: 500, headers: { 'Content-Type': 'application/json', - 'Access-Control-Allow-Origin': '*', - 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', - 'Access-Control-Allow-Headers': 'Content-Type, Authorization', + ...corsHeaders } }); } @@ -63,10 +56,6 @@ export async function POST(req: NextRequest) { export async function OPTIONS(request: Request) { return new Response(null, { status: 200, - headers: { - 'Access-Control-Allow-Origin': '*', - 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', - 'Access-Control-Allow-Headers': 'Content-Type, Authorization', - } + headers: corsHeaders }); } \ No newline at end of file diff --git a/src/lib/utils.ts b/src/lib/utils.ts index ddabf9d..4822741 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -18,4 +18,11 @@ export const sleep = (x: number, y?: number): Promise => { logger.info(`Sleeping for ${timeout / 1000} seconds`); return new Promise(resolve => setTimeout(resolve, timeout)); +} + + +export const corsHeaders = { + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type, Authorization', } \ No newline at end of file