-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
72 lines (60 loc) · 1.94 KB
/
script.js
File metadata and controls
72 lines (60 loc) · 1.94 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { NextResponse } from "next/server"
export async function GET(request) {
const searchParams = request.nextUrl.searchParams
const username = searchParams.get("username")
if (!username) {
return NextResponse.json({ error: "Username is required" }, { status: 400 })
}
try {
// Fetch user data
const userResponse = await fetch(`https://api.github.com/users/${username}`, {
headers: {
Accept: "application/vnd.github.v3+json",
"User-Agent": "GitHub-Profile-Analyzer",
},
})
if (!userResponse.ok) {
if (userResponse.status === 404) {
return NextResponse.json({ error: "User not found" }, { status: 404 })
}
throw new Error(`GitHub API error: ${userResponse.status}`)
}
const user = await userResponse.json()
// Fetch repositories
const reposResponse = await fetch(
`https://api.github.com/users/${username}/repos?sort=stars&direction=desc&per_page=100`,
{
headers: {
Accept: "application/vnd.github.v3+json",
"User-Agent": "GitHub-Profile-Analyzer",
},
},
)
if (!reposResponse.ok) {
throw new Error(`GitHub API error: ${reposResponse.status}`)
}
const repositories = await reposResponse.json()
// Calculate statistics
const languages = {}
let totalStars = 0
let totalForks = 0
repositories.forEach((repo) => {
if (repo.language) {
languages[repo.language] = (languages[repo.language] || 0) + 1
}
totalStars += repo.stargazers_count || 0
totalForks += repo.forks_count || 0
})
const data = {
user,
repositories: repositories.slice(0, 20), // Limit to top 20 repos
languages,
totalStars,
totalForks,
}
return NextResponse.json(data)
} catch (error) {
console.error("Error fetching GitHub data:", error)
return NextResponse.json({ error: "Failed to fetch GitHub data" }, { status: 500 })
}
}