forked from JhaSourav07/commitpulse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
43 lines (37 loc) · 1.41 KB
/
Copy pathroute.ts
File metadata and controls
43 lines (37 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { NextResponse } from 'next/server';
import { fetchCIAnalytics } from '@/services/github/ci-analytics';
import { getUserGitHubToken } from '@/lib/githubtoken';
import { validateGitHubUsername } from '@/lib/validations';
import { RateLimiter } from '@/lib/rate-limit';
const ciAnalyticsLimiter = new RateLimiter(10, 60_000, 10_000);
export async function GET(request: Request) {
const ip =
request.headers.get('x-forwarded-for')?.split(',')[0]?.trim() ??
request.headers.get('x-real-ip') ??
'unknown';
if (!(await ciAnalyticsLimiter.check(ip))) {
return NextResponse.json(
{ error: 'Too many requests. Please try again later.' },
{ status: 429 }
);
}
const { searchParams } = new URL(request.url);
const username = searchParams.get('username')?.trim();
if (!username) {
return NextResponse.json({ error: 'Username is required' }, { status: 400 });
}
if (!validateGitHubUsername(username)) {
return NextResponse.json({ error: 'Invalid GitHub username' }, { status: 400 });
}
try {
const userToken = await getUserGitHubToken();
const data = await fetchCIAnalytics(username, userToken);
return NextResponse.json(data);
} catch (error: unknown) {
console.error('Error fetching CI analytics:', error);
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to fetch CI analytics' },
{ status: 500 }
);
}
}