Skip to content

Commit 8076d1f

Browse files
authored
Phase:2- Refactoring (#91)
2 parents 80d0ae6 + 1d1c1d3 commit 8076d1f

File tree

14 files changed

+3516
-1759
lines changed

14 files changed

+3516
-1759
lines changed

app/api/credentials/[provider]/route.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ export async function GET(
1313
);
1414
return NextResponse.json(data, { status });
1515
} catch (e: unknown) {
16-
return NextResponse.json({ error: e instanceof Error ? e.message : String(e) }, { status: 500 });
16+
return NextResponse.json(
17+
{ error: e instanceof Error ? e.message : String(e) },
18+
{ status: 500 },
19+
);
1720
}
1821
}
1922

@@ -34,6 +37,9 @@ export async function DELETE(
3437

3538
return NextResponse.json(data, { status });
3639
} catch (e: unknown) {
37-
return NextResponse.json({ error: e instanceof Error ? e.message : String(e) }, { status: 500 });
40+
return NextResponse.json(
41+
{ error: e instanceof Error ? e.message : String(e) },
42+
{ status: 500 },
43+
);
3844
}
3945
}

app/api/credentials/route.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ export async function GET(request: NextRequest) {
66
const { status, data } = await apiClient(request, "/api/v1/credentials/");
77
return NextResponse.json(data, { status });
88
} catch (e: unknown) {
9-
return NextResponse.json({ error: e instanceof Error ? e.message : String(e) }, { status: 500 });
9+
return NextResponse.json(
10+
{ error: e instanceof Error ? e.message : String(e) },
11+
{ status: 500 },
12+
);
1013
}
1114
}
1215

@@ -19,7 +22,10 @@ export async function POST(request: NextRequest) {
1922
});
2023
return NextResponse.json(data, { status });
2124
} catch (e: unknown) {
22-
return NextResponse.json({ error: e instanceof Error ? e.message : String(e) }, { status: 500 });
25+
return NextResponse.json(
26+
{ error: e instanceof Error ? e.message : String(e) },
27+
{ status: 500 },
28+
);
2329
}
2430
}
2531

@@ -32,6 +38,9 @@ export async function PATCH(request: NextRequest) {
3238
});
3339
return NextResponse.json(data, { status });
3440
} catch (e: unknown) {
35-
return NextResponse.json({ error: e instanceof Error ? e.message : String(e) }, { status: 500 });
41+
return NextResponse.json(
42+
{ error: e instanceof Error ? e.message : String(e) },
43+
{ status: 500 },
44+
);
3645
}
3746
}
Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,28 @@
1-
import { NextRequest, NextResponse } from 'next/server';
2-
1+
import { apiClient } from "@/app/lib/apiClient";
2+
import { NextRequest, NextResponse } from "next/server";
33

44
export async function POST(request: NextRequest) {
55
try {
6-
// Get the API key from request headers
7-
const apiKey = request.headers.get('X-API-KEY');
8-
9-
if (!apiKey) {
10-
return NextResponse.json(
11-
{ error: 'Missing X-API-KEY header' },
12-
{ status: 401 }
13-
);
14-
}
15-
16-
// Get the form data from the request
176
const formData = await request.formData();
187

19-
// Get backend URL from environment variable
20-
const backendUrl = process.env.BACKEND_URL || 'http://localhost:8000';
21-
22-
// Forward the request to the actual backend
23-
const response = await fetch(`${backendUrl}/api/v1/evaluations/stt/files`, {
24-
method: 'POST',
25-
body: formData,
26-
headers: {
27-
'X-API-KEY': apiKey,
28-
8+
const { status, data: responseData } = await apiClient(
9+
request,
10+
"/api/v1/evaluations/stt/files",
11+
{
12+
method: "POST",
13+
body: formData,
2914
},
30-
});
31-
32-
// Handle empty responses (204 No Content, etc.)
33-
const text = await response.text();
34-
const data = text ? JSON.parse(text) : { success: true };
35-
36-
// Return the response with the same status code
37-
if (!response.ok) {
38-
return NextResponse.json(data, { status: response.status });
39-
}
15+
);
4016

41-
return NextResponse.json(data, { status: response.status });
17+
return NextResponse.json(responseData, { status });
4218
} catch (error: unknown) {
43-
console.error('Proxy error:', error);
19+
console.error("Proxy error:", error);
4420
return NextResponse.json(
45-
{ error: 'Failed to forward request to backend', details: error instanceof Error ? error.message : String(error) },
46-
{ status: 500 }
21+
{
22+
error: "Failed to forward request to backend",
23+
details: error instanceof Error ? error.message : String(error),
24+
},
25+
{ status: 500 },
4726
);
4827
}
4928
}
Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
import { NextResponse } from 'next/server';
1+
import { apiClient } from "@/app/lib/apiClient";
2+
import { NextResponse } from "next/server";
23

34
export 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

6259
export 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
}
Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,41 @@
1-
import { NextResponse, NextRequest } from 'next/server';
1+
import { apiClient } from "@/app/lib/apiClient";
2+
import { NextResponse, NextRequest } from "next/server";
23

34
export async function GET(request: Request) {
4-
const backendUrl = process.env.BACKEND_URL || 'http://localhost:8000';
5-
const apiKey = request.headers.get('X-API-KEY');
6-
75
try {
8-
const response = await fetch(`${backendUrl}/api/v1/evaluations/tts/datasets`, {
9-
headers: {
10-
'X-API-KEY': apiKey || '',
11-
},
12-
});
13-
14-
const data = await response.json();
15-
return NextResponse.json(data, { status: response.status });
6+
const { status, data } = await apiClient(
7+
request,
8+
"/api/v1/evaluations/tts/datasets",
9+
);
10+
return NextResponse.json(data, { status });
1611
} catch (error) {
1712
return NextResponse.json(
1813
{ success: false, error: error, data: null },
19-
{ status: 500 }
14+
{ status: 500 },
2015
);
2116
}
2217
}
2318

2419
export async function POST(request: NextRequest) {
2520
try {
26-
const apiKey = request.headers.get('X-API-KEY');
27-
if (!apiKey) {
28-
return NextResponse.json(
29-
{ error: 'Missing X-API-KEY. Either generate an API Key. Contact Kaapi team for more details' },
30-
{ status: 401 }
31-
);
32-
}
3321
const body = await request.json();
34-
const backendUrl = process.env.BACKEND_URL || 'http://localhost:8000';
35-
36-
const response = await fetch(`${backendUrl}/api/v1/evaluations/tts/datasets`, {
37-
method: 'POST',
38-
body: JSON.stringify(body),
39-
headers: {
40-
'X-API-KEY': apiKey,
41-
'Content-Type': 'application/json',
22+
const { status, data } = await apiClient(
23+
request,
24+
"/api/v1/evaluations/tts/datasets",
25+
{
26+
method: "POST",
27+
body: JSON.stringify(body),
4228
},
43-
});
44-
const data = await response.json();
45-
return NextResponse.json(data, { status: response.status });
29+
);
30+
return NextResponse.json(data, { status });
4631
} catch (error) {
47-
console.error('Proxy error:', error);
32+
console.error("Proxy error:", error);
4833
return NextResponse.json(
49-
{ error: 'Failed to forward request to backend', details: error instanceof Error ? error.message : String(error) },
50-
{ status: 500 }
34+
{
35+
error: "Failed to forward request to backend",
36+
details: error instanceof Error ? error.message : String(error),
37+
},
38+
{ status: 500 },
5139
);
5240
}
5341
}

0 commit comments

Comments
 (0)