feat: optimize code

This commit is contained in:
GitPusher99
2024-03-28 13:21:33 +08:00
parent 2a309f87a7
commit d3160ce5f9
8 changed files with 5902 additions and 218 deletions
+43 -43
View File
@@ -1,50 +1,50 @@
import { NextResponse, NextRequest } from "next/server";
import SunoApi from '@/lib/sunoApi';
import { sunoApi } from "@/lib/SunoApi";
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 (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' }
});
}
// 调用 SunoApi.custom_generate 方法生成定制音频
const audioInfo = 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' }
});
} 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' }
});
}
// 使用 NextResponse 构建错误响应
return new NextResponse(JSON.stringify({ error: 'Internal server error' }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
} else {
return new NextResponse('Method Not Allowed', {
headers: { Allow: 'POST' },
status: 405
// 校验输入参数
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' }
});
} 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' }
});
}
// 使用 NextResponse 构建错误响应
return new NextResponse(JSON.stringify({ error: 'Internal server error' }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
} else {
return new NextResponse('Method Not Allowed', {
headers: { Allow: 'POST' },
status: 405
});
}
}
+39 -39
View File
@@ -1,46 +1,46 @@
import { NextResponse, NextRequest } from "next/server";
import SunoApi from '@/lib/sunoApi';
import { sunoApi } from "@/lib/SunoApi";
export async function POST(req: NextRequest) {
if (req.method === 'POST') {
try {
const body = await req.json();
const { prompt, make_instrumental, wait_audio } = body;
if (req.method === 'POST') {
try {
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,
headers: { 'Content-Type': 'application/json' }
});
}
// 调用 SunoApi.generate 方法生成音频
const audioInfo = await SunoApi.generate(prompt, make_instrumental == true, wait_audio == true);
// 使用 NextResponse 构建成功响应
return new NextResponse(JSON.stringify(audioInfo), {
status: 200,
headers: { 'Content-Type': 'application/json' }
});
} 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' }
});
}
// 使用 NextResponse 构建错误响应
return new NextResponse(JSON.stringify({ error: 'Internal server error: ' + JSON.stringify(error.response.data.detail) }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
} else {
return new NextResponse('Method Not Allowed', {
headers: { Allow: 'POST' },
status: 405
// 校验输入参数
if (!prompt) {
return new NextResponse(JSON.stringify({ error: 'Prompt is required' }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
});
}
// 调用 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' }
});
} 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' }
});
}
// 使用 NextResponse 构建错误响应
return new NextResponse(JSON.stringify({ error: 'Internal server error: ' + JSON.stringify(error.response.data.detail) }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
} else {
return new NextResponse('Method Not Allowed', {
headers: { Allow: 'POST' },
status: 405
});
}
}
+32 -32
View File
@@ -1,38 +1,38 @@
import { NextResponse, NextRequest } from "next/server";
import SunoApi from '@/lib/sunoApi';
import { sunoApi } from "@/lib/SunoApi";
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 SunoApi.get(idsArray);
} else {
audioInfo = await SunoApi.get();
}
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' }
});
}
} else {
return new NextResponse('Method Not Allowed', {
headers: { Allow: 'GET' },
status: 405
});
// 使用 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' }
});
}
} else {
return new NextResponse('Method Not Allowed', {
headers: { Allow: 'GET' },
status: 405
});
}
}
+23 -23
View File
@@ -1,29 +1,29 @@
import { NextResponse, NextRequest } from "next/server";
import SunoApi from '@/lib/sunoApi';
import { sunoApi } from "@/lib/SunoApi";
export async function GET(req: NextRequest) {
if (req.method === 'GET') {
try {
// 调用 SunoApi.get_limit 方法获取剩余的信用额度
const limit = await SunoApi.get_credits();
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' }
});
}
} else {
return new NextResponse('Method Not Allowed', {
headers: { Allow: 'GET' },
status: 405
});
// 使用 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' }
});
}
} else {
return new NextResponse('Method Not Allowed', {
headers: { Allow: 'GET' },
status: 405
});
}
}