1- import { NextResponse } from 'next/server' ;
1+ import { apiClient } from "@/app/lib/apiClient" ;
2+ import { NextResponse } from "next/server" ;
23
34export async function GET (
45 request : Request ,
5- { params } : { params : Promise < { dataset_id : string } > }
6+ { params } : { params : Promise < { dataset_id : string } > } ,
67) {
78 const { dataset_id } = await params ;
8- const backendUrl = process . env . BACKEND_URL || 'http://localhost:8000' ;
9- const apiKey = request . headers . get ( 'X-API-KEY' ) ;
10-
11- if ( ! apiKey ) {
12- return NextResponse . json (
13- { success : false , error : 'Unauthorized: Missing API key' , data : null } ,
14- { status : 401 }
15- ) ;
16- }
179
1810 try {
1911 // Forward query parameters to the backend
@@ -22,64 +14,69 @@ export async function GET(
2214 for ( const [ key , value ] of searchParams . entries ( ) ) {
2315 backendParams . append ( key , value ) ;
2416 }
25- const queryString = backendParams . toString ( ) ? `?${ backendParams . toString ( ) } ` : '' ;
17+ const queryString = backendParams . toString ( )
18+ ? `?${ backendParams . toString ( ) } `
19+ : "" ;
2620
27- const response = await fetch ( `${ backendUrl } /api/v1/evaluations/tts/datasets/${ dataset_id } ${ queryString } ` , {
28- headers : {
29- 'X-API-KEY' : apiKey ,
30- } ,
31- } ) ;
32-
33- const data = await response . json ( ) ;
34- if ( ! response . ok ) {
35- return NextResponse . json ( data , { status : response . status } ) ;
36- }
21+ const { data, status } = await apiClient (
22+ request ,
23+ `/api/v1/evaluations/tts/datasets/${ dataset_id } ${ queryString } ` ,
24+ ) ;
3725
3826 // If fetch_content=true, download the CSV from the signed URL and return it
39- const fetchContent = new URL ( request . url ) . searchParams . get ( ' fetch_content' ) ;
40- if ( fetchContent === ' true' ) {
27+ const fetchContent = new URL ( request . url ) . searchParams . get ( " fetch_content" ) ;
28+ if ( fetchContent === " true" ) {
4129 const signedUrl = data ?. data ?. signed_url || data ?. signed_url ;
4230 if ( ! signedUrl ) {
43- return NextResponse . json ( { error : 'No signed URL available' } , { status : 404 } ) ;
31+ return NextResponse . json (
32+ { error : "No signed URL available" } ,
33+ { status : 404 } ,
34+ ) ;
4435 }
4536 const csvResponse = await fetch ( signedUrl ) ;
4637 if ( ! csvResponse . ok ) {
47- return NextResponse . json ( { error : 'Failed to fetch CSV file' } , { status : 502 } ) ;
38+ return NextResponse . json (
39+ { error : "Failed to fetch CSV file" } ,
40+ { status : 502 } ,
41+ ) ;
4842 }
4943 const csvText = await csvResponse . text ( ) ;
50- return NextResponse . json ( { ...data , csv_content : csvText } , { status : 200 } ) ;
44+ return NextResponse . json (
45+ { ...data , csv_content : csvText } ,
46+ { status : 200 } ,
47+ ) ;
5148 }
5249
53- return NextResponse . json ( data , { status : response . status } ) ;
50+ return NextResponse . json ( data , { status } ) ;
5451 } catch ( _error ) {
5552 return NextResponse . json (
56- { success : false , error : ' Failed to fetch dataset' , data : null } ,
57- { status : 500 }
53+ { success : false , error : " Failed to fetch dataset" , data : null } ,
54+ { status : 500 } ,
5855 ) ;
5956 }
6057}
6158
6259export async function DELETE (
6360 request : Request ,
64- { params } : { params : Promise < { dataset_id : string } > }
61+ { params } : { params : Promise < { dataset_id : string } > } ,
6562) {
6663 const { dataset_id } = await params ;
67- const backendUrl = process . env . BACKEND_URL || 'http://localhost:8000' ;
68- const apiKey = request . headers . get ( 'X-API-KEY' ) ;
69-
70- if ( ! apiKey ) {
71- return NextResponse . json ( { success : false , error : 'Unauthorized: Missing API key' } , { status : 401 } ) ;
72- }
7364
7465 try {
75- const response = await fetch ( `${ backendUrl } /api/v1/evaluations/tts/datasets/${ dataset_id } ` , {
76- method : 'DELETE' ,
77- headers : { 'X-API-KEY' : apiKey } ,
78- } ) ;
79- let data ;
80- try { data = await response . json ( ) ; } catch { data = { success : true } ; }
81- return NextResponse . json ( data , { status : response . ok ? 200 : response . status } ) ;
66+ const { data, status } = await apiClient (
67+ request ,
68+ `/api/v1/evaluations/tts/datasets/${ dataset_id } ` ,
69+ { method : "DELETE" } ,
70+ ) ;
71+ return NextResponse . json ( data , { status } ) ;
8272 } catch ( error : unknown ) {
83- return NextResponse . json ( { success : false , error : 'Failed to delete dataset' , details : error instanceof Error ? error . message : String ( error ) } , { status : 500 } ) ;
73+ return NextResponse . json (
74+ {
75+ success : false ,
76+ error : "Failed to delete dataset" ,
77+ details : error instanceof Error ? error . message : String ( error ) ,
78+ } ,
79+ { status : 500 } ,
80+ ) ;
8481 }
8582}
0 commit comments