@@ -15,10 +15,14 @@ function getContributorData(): ContributorData {
15
15
return contributorData as ContributorData
16
16
}
17
17
18
+ const ALLOWED_ORIGIN = process . env . VERCEL_URL
19
+ ? `https://${ process . env . VERCEL_URL } `
20
+ : "http://localhost:3000"
21
+
18
22
export async function GET ( request : NextRequest ) {
19
23
const headers = new Headers ( {
20
24
"Content-Type" : "application/json" ,
21
- "Access-Control-Allow-Origin" : "*" ,
25
+ "Access-Control-Allow-Origin" : ALLOWED_ORIGIN ,
22
26
"Access-Control-Allow-Methods" : "GET" ,
23
27
"Access-Control-Allow-Headers" : "Content-Type" ,
24
28
"Cache-Control" : "public, s-maxage=86400, stale-while-revalidate=172800" ,
@@ -33,42 +37,45 @@ export async function GET(request: NextRequest) {
33
37
const after = searchParams . get ( "after" ) || ""
34
38
const repository = searchParams . get ( "repository" ) || ""
35
39
36
- const data = getContributorData ( )
40
+ if ( ! repository ) {
41
+ return NextResponse . json (
42
+ {
43
+ error : "Bad request" ,
44
+ message : "Repository parameter is required" ,
45
+ } ,
46
+ {
47
+ status : 400 ,
48
+ headers,
49
+ } ,
50
+ )
51
+ }
37
52
38
- // Flatten all contributors
39
- const allContributors : Contributor [ ] = [ ]
53
+ const data = getContributorData ( )
54
+ const repositoryContributors = data [ repository ]
40
55
41
- for ( const [ repoName , contributors ] of Object . entries ( data ) ) {
42
- if (
43
- ! repository ||
44
- repoName . toLowerCase ( ) . includes ( repository . toLowerCase ( ) )
45
- ) {
46
- allContributors . push ( ...contributors )
47
- }
56
+ if ( ! repositoryContributors ) {
57
+ return NextResponse . json ( [ ] , { headers } )
48
58
}
49
59
50
- // Sort by contributions (descending), then by id for stable sorting
51
- allContributors . sort ( ( a , b ) => {
60
+ const sortedContributors = [ ...repositoryContributors ] . sort ( ( a , b ) => {
52
61
if ( b . contributions !== a . contributions ) {
53
62
return b . contributions - a . contributions
54
63
}
55
64
return a . id . localeCompare ( b . id )
56
65
} )
57
66
58
- // Find starting index based on cursor (contributor id)
59
67
let startIndex = 0
60
68
if ( after ) {
61
- const afterIndex = allContributors . findIndex (
69
+ const afterIndex = sortedContributors . findIndex (
62
70
contributor => contributor . id === after ,
63
71
)
64
72
if ( afterIndex >= 0 ) {
65
73
startIndex = afterIndex + 1
66
74
}
67
75
}
68
76
69
- // Get the requested slice
70
- const endIndex = Math . min ( startIndex + first , allContributors . length )
71
- const paginatedContributors = allContributors . slice ( startIndex , endIndex )
77
+ const endIndex = Math . min ( startIndex + first , sortedContributors . length )
78
+ const paginatedContributors = sortedContributors . slice ( startIndex , endIndex )
72
79
73
80
return NextResponse . json ( paginatedContributors , { headers } )
74
81
} catch ( error ) {
0 commit comments