feat: Add new dependencies and SunoApi class for audio generation

functionality

Added new dependencies for axios, pino, and user-agents in the
package.json and pnpm-lock.yaml files to support audio generation
functionality. Additionally, implemented a new SunoApi class in the lib
directory to handle the audio generation process, including functions
for generating, customizing, and retrieving audio content from the
Suno.ai API. This addresses the need for enhanced audio processing
capabilities and lays the groundwork for efficient integration with the
Suno.ai API. No issues are associated with these additions.
This commit is contained in:
blueeon
2024-03-28 03:35:22 +08:00
parent 12f31cb3ea
commit 0fcf77df6d
11 changed files with 676 additions and 1 deletions
+50
View File
@@ -0,0 +1,50 @@
import { NextResponse, NextRequest } from "next/server";
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 (!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
});
}
}
+46
View File
@@ -0,0 +1,46 @@
import { NextResponse, NextRequest } from "next/server";
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 (!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
});
}
}
+38
View File
@@ -0,0 +1,38 @@
import { NextResponse, NextRequest } from "next/server";
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();
}
// 使用 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
});
}
}
+29
View File
@@ -0,0 +1,29 @@
import { NextResponse, NextRequest } from "next/server";
import SunoApi from '@/lib/sunoApi';
export async function GET(req: NextRequest) {
if (req.method === 'GET') {
try {
// 调用 SunoApi.get_limit 方法获取剩余的信用额度
const limit = await SunoApi.get_limit();
// 使用 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
});
}
}