@@ -15,10 +15,14 @@ function getContributorData(): ContributorData {
1515 return contributorData as ContributorData
1616}
1717
18+ const ALLOWED_ORIGIN = process . env . VERCEL_URL
19+ ? `https://${ process . env . VERCEL_URL } `
20+ : "http://localhost:3000"
21+
1822export async function GET ( request : NextRequest ) {
1923 const headers = new Headers ( {
2024 "Content-Type" : "application/json" ,
21- "Access-Control-Allow-Origin" : "*" ,
25+ "Access-Control-Allow-Origin" : ALLOWED_ORIGIN ,
2226 "Access-Control-Allow-Methods" : "GET" ,
2327 "Access-Control-Allow-Headers" : "Content-Type" ,
2428 "Cache-Control" : "public, s-maxage=86400, stale-while-revalidate=172800" ,
@@ -33,42 +37,45 @@ export async function GET(request: NextRequest) {
3337 const after = searchParams . get ( "after" ) || ""
3438 const repository = searchParams . get ( "repository" ) || ""
3539
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+ }
3752
38- // Flatten all contributors
39- const allContributors : Contributor [ ] = [ ]
53+ const data = getContributorData ( )
54+ const repositoryContributors = data [ repository ]
4055
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 } )
4858 }
4959
50- // Sort by contributions (descending), then by id for stable sorting
51- allContributors . sort ( ( a , b ) => {
60+ const sortedContributors = [ ...repositoryContributors ] . sort ( ( a , b ) => {
5261 if ( b . contributions !== a . contributions ) {
5362 return b . contributions - a . contributions
5463 }
5564 return a . id . localeCompare ( b . id )
5665 } )
5766
58- // Find starting index based on cursor (contributor id)
5967 let startIndex = 0
6068 if ( after ) {
61- const afterIndex = allContributors . findIndex (
69+ const afterIndex = sortedContributors . findIndex (
6270 contributor => contributor . id === after ,
6371 )
6472 if ( afterIndex >= 0 ) {
6573 startIndex = afterIndex + 1
6674 }
6775 }
6876
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 )
7279
7380 return NextResponse . json ( paginatedContributors , { headers } )
7481 } catch ( error ) {
0 commit comments