Added basic validation for requests

This commit is contained in:
Tom
2024-08-25 21:35:46 +00:00
parent 2d40d6fe09
commit 624b3fa63b
42 changed files with 608 additions and 492 deletions

View File

@ -7,7 +7,7 @@ export async function GET(req: Request) {
try {
const user = await fetchUserWithImpersonation(req)
if (!user) {
return new NextResponse("Unauthorized", { status: 401 });
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
}
const selected = await db.ttsChatVoice.findMany({
@ -22,8 +22,7 @@ export async function GET(req: Request) {
const data = selected.map(s => ({ chatter_id: new Number(s.chatterId), voice: voiceNamesMapped[s.ttsVoiceId] }))
return NextResponse.json(data);
} catch (error) {
console.log("[TTS/SELECTED]", error);
return new NextResponse("Internal Error", { status: 500 });
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
}
}
@ -31,11 +30,12 @@ export async function POST(req: Request) {
try {
const user = await fetchUserWithImpersonation(req)
if (!user) {
return new NextResponse("Unauthorized", { status: 401 });
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
}
const { voice, chatterId }: { voice: string, chatterId: number } = await req.json();
if (!voice || !voices.map(v => v.toLowerCase()).includes(voice.toLowerCase())) return new NextResponse("Bad Request", { status: 400 });
if (!voice || !voices.map(v => v.toLowerCase()).includes(voice.toLowerCase()))
return NextResponse.json({ message: 'Voice does not exist.', error: null, value: null }, { status: 400 })
const v = voices.find(v => v.toLowerCase() == voice.toLowerCase())
const voiceData = await db.ttsVoice.findFirst({
@ -44,7 +44,7 @@ export async function POST(req: Request) {
}
})
if (!voiceData)
return new NextResponse("Bad Request", { status: 400 });
return NextResponse.json({ message: 'Voice does not exist.', error: null, value: null }, { status: 400 })
await db.ttsChatVoice.upsert({
where: {
@ -63,9 +63,8 @@ export async function POST(req: Request) {
}
});
return new NextResponse("", { status: 200 });
return NextResponse.json({ message: null, error: null, value: null }, { status: 200 })
} catch (error) {
console.log("[TTS/SELECTED]", error);
return new NextResponse("Internal Error", { status: 500 });
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
}
}