1+ import type { NextRequest } from 'next/server'
12import { NextResponse } from 'next/server'
3+ import { checkHybridAuth } from '@/lib/auth/hybrid'
24import { createLogger } from '@/lib/logs/console/logger'
35import { validateAlphanumericId } from '@/lib/security/input-validation'
46import { uploadFile } from '@/lib/uploads/storage-client'
57import { getBaseUrl } from '@/lib/urls/utils'
68
79const logger = createLogger ( 'ProxyTTSAPI' )
810
9- export async function POST ( request : Request ) {
11+ export async function POST ( request : NextRequest ) {
1012 try {
13+ const authResult = await checkHybridAuth ( request , { requireWorkflowId : false } )
14+ if ( ! authResult . success ) {
15+ logger . error ( 'Authentication failed for TTS proxy:' , authResult . error )
16+ return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
17+ }
18+
1119 const body = await request . json ( )
1220 const { text, voiceId, apiKey, modelId = 'eleven_monolingual_v1' } = body
1321
1422 if ( ! text || ! voiceId || ! apiKey ) {
15- return new NextResponse ( 'Missing required parameters' , { status : 400 } )
23+ return NextResponse . json ( { error : 'Missing required parameters' } , { status : 400 } )
1624 }
1725
1826 const voiceIdValidation = validateAlphanumericId ( voiceId , 'voiceId' , 255 )
1927 if ( ! voiceIdValidation . isValid ) {
2028 logger . error ( `Invalid voice ID: ${ voiceIdValidation . error } ` )
21- return new NextResponse ( voiceIdValidation . error , { status : 400 } )
29+ return NextResponse . json ( { error : voiceIdValidation . error } , { status : 400 } )
2230 }
2331
2432 logger . info ( 'Proxying TTS request for voice:' , voiceId )
@@ -41,16 +49,17 @@ export async function POST(request: Request) {
4149
4250 if ( ! response . ok ) {
4351 logger . error ( `Failed to generate TTS: ${ response . status } ${ response . statusText } ` )
44- return new NextResponse ( `Failed to generate TTS: ${ response . status } ${ response . statusText } ` , {
45- status : response . status ,
46- } )
52+ return NextResponse . json (
53+ { error : `Failed to generate TTS: ${ response . status } ${ response . statusText } ` } ,
54+ { status : response . status }
55+ )
4756 }
4857
4958 const audioBlob = await response . blob ( )
5059
5160 if ( audioBlob . size === 0 ) {
5261 logger . error ( 'Empty audio received from ElevenLabs' )
53- return new NextResponse ( 'Empty audio received' , { status : 422 } )
62+ return NextResponse . json ( { error : 'Empty audio received' } , { status : 422 } )
5463 }
5564
5665 const audioBuffer = Buffer . from ( await audioBlob . arrayBuffer ( ) )
@@ -67,11 +76,11 @@ export async function POST(request: Request) {
6776 } catch ( error ) {
6877 logger . error ( 'Error proxying TTS:' , error )
6978
70- return new NextResponse (
71- `Internal Server Error: ${ error instanceof Error ? error . message : 'Unknown error' } ` ,
79+ return NextResponse . json (
7280 {
73- status : 500 ,
74- }
81+ error : `Internal Server Error: ${ error instanceof Error ? error . message : 'Unknown error' } ` ,
82+ } ,
83+ { status : 500 }
7584 )
7685 }
7786}
0 commit comments