|
| 1 | +import { NextResponse } from "next/server"; |
| 2 | +import { fetchGitHubUserData } from "../../../lib/github"; |
| 3 | +import { calculateUserScore } from "../../../lib/score"; |
| 4 | + |
| 5 | +export const runtime = "nodejs"; |
| 6 | + |
| 7 | +export async function GET(request: Request) { |
| 8 | + const { searchParams } = new URL(request.url); |
| 9 | + const usernames = searchParams.getAll("username"); |
| 10 | + |
| 11 | + if (usernames.length === 0) { |
| 12 | + return NextResponse.json( |
| 13 | + { success: false, error: "provide at least one username param" }, |
| 14 | + { status: 400 } |
| 15 | + ); |
| 16 | + } |
| 17 | + |
| 18 | + try { |
| 19 | + const results = await Promise.all( |
| 20 | + usernames.map(async (username) => { |
| 21 | + const data = await fetchGitHubUserData(username); |
| 22 | + const score = calculateUserScore(data, username); |
| 23 | + |
| 24 | + return { |
| 25 | + username, |
| 26 | + repoScore: score.repoScore, |
| 27 | + prScore: score.prScore, |
| 28 | + contributionScore: score.contributionScore, |
| 29 | + finalScore: score.finalScore, |
| 30 | + topRepos: score.topRepos, |
| 31 | + topPullRequests: score.topPullRequests, |
| 32 | + }; |
| 33 | + }) |
| 34 | + ); |
| 35 | + |
| 36 | + return NextResponse.json({ success: true, users: results }); |
| 37 | + } catch (error: any) { |
| 38 | + console.error("GitHub score error:", error); |
| 39 | + const message = |
| 40 | + error?.message === "User not found" |
| 41 | + ? "GitHub user not found" |
| 42 | + : "Failed to calculate score"; |
| 43 | + return NextResponse.json( |
| 44 | + { success: false, error: message }, |
| 45 | + { status: 500 } |
| 46 | + ); |
| 47 | + } |
| 48 | +} |
0 commit comments