refactor: Optimize code comments.
This commit is contained in:
@@ -6,23 +6,17 @@ export async function POST(req: NextRequest) {
|
||||
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' }
|
||||
});
|
||||
}
|
||||
|
||||
// 调用 SunoApi.custom_generate 方法生成定制音频
|
||||
const audioInfo = await (await sunoApi).custom_generate(
|
||||
prompt, tags, title,
|
||||
make_instrumental == true,
|
||||
wait_audio == true
|
||||
);
|
||||
|
||||
// 使用 NextResponse 构建成功响应
|
||||
return new NextResponse(JSON.stringify(audioInfo), {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
@@ -35,7 +29,6 @@ export async function POST(req: NextRequest) {
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
}
|
||||
// 使用 NextResponse 构建错误响应
|
||||
return new NextResponse(JSON.stringify({ error: 'Internal server error' }), {
|
||||
status: 500,
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
|
||||
@@ -7,7 +7,6 @@ export async function POST(req: NextRequest) {
|
||||
const body = await req.json();
|
||||
const { prompt, make_instrumental, wait_audio } = body;
|
||||
|
||||
// 校验输入参数
|
||||
if (!prompt) {
|
||||
return new NextResponse(JSON.stringify({ error: 'Prompt is required' }), {
|
||||
status: 400,
|
||||
@@ -15,10 +14,8 @@ export async function POST(req: NextRequest) {
|
||||
});
|
||||
}
|
||||
|
||||
// 调用 SunoApi.generate 方法生成音频
|
||||
const audioInfo = await (await sunoApi).generate(prompt, make_instrumental == true, wait_audio == true);
|
||||
|
||||
// 使用 NextResponse 构建成功响应
|
||||
return new NextResponse(JSON.stringify(audioInfo), {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
@@ -31,7 +28,6 @@ export async function POST(req: NextRequest) {
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
}
|
||||
// 使用 NextResponse 构建错误响应
|
||||
return new NextResponse(JSON.stringify({ error: 'Internal server error: ' + JSON.stringify(error.response.data.detail) }), {
|
||||
status: 500,
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
|
||||
@@ -5,26 +5,23 @@ export const dynamic = "force-dynamic";
|
||||
export async function GET(req: NextRequest) {
|
||||
if (req.method === 'GET') {
|
||||
try {
|
||||
// 修复了获取查询参数的方式
|
||||
const url = new URL(req.url);
|
||||
const songIds = url.searchParams.get('ids');
|
||||
let audioInfo = [];
|
||||
if (songIds && songIds.length > 0) {
|
||||
const idsArray = songIds.split(',');
|
||||
// 调用 SunoApi.get 方法获取音频信息
|
||||
audioInfo = await (await sunoApi).get(idsArray);
|
||||
} else {
|
||||
audioInfo = await (await sunoApi).get();
|
||||
}
|
||||
|
||||
// 使用 NextResponse 构建成功响应
|
||||
return new NextResponse(JSON.stringify(audioInfo), {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching audio:', error);
|
||||
// 使用 NextResponse 构建错误响应
|
||||
|
||||
return new NextResponse(JSON.stringify({ error: 'Internal server error' }), {
|
||||
status: 500,
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
|
||||
@@ -4,17 +4,17 @@ export const dynamic = "force-dynamic";
|
||||
export async function GET(req: NextRequest) {
|
||||
if (req.method === 'GET') {
|
||||
try {
|
||||
// 调用 SunoApi.get_limit 方法获取剩余的信用额度
|
||||
|
||||
const limit = await (await sunoApi).get_credits();
|
||||
|
||||
// 使用 NextResponse 构建成功响应
|
||||
|
||||
return new NextResponse(JSON.stringify(limit), {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching limit:', error);
|
||||
// 使用 NextResponse 构建错误响应
|
||||
|
||||
return new NextResponse(JSON.stringify({ error: 'Internal server error' }), {
|
||||
status: 500,
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
|
||||
Reference in New Issue
Block a user