@@ -2,45 +2,40 @@ import features from '$lib/data/features';
22import { fetchMarkdownPosts } from '$lib/utils/index_posts' ;
33import type { Contributor } from '$lib/utils/types' ;
44
5- export async function load ( ) {
6- const allPosts = await fetchMarkdownPosts ( ) ;
7-
8- const posts = allPosts
9- . sort ( ( a , b ) => new Date ( b . meta . date ) . getTime ( ) - new Date ( a . meta . date ) . getTime ( ) )
10- . slice ( 0 , 6 ) ;
11-
12- const orgReposURL = 'https://api.github.com/orgs/torrust/repos' ;
13- const baseURL = 'https://api.github.com/repos/torrust/' ;
14- const token = import . meta. env . VITE_GITHUB_TOKEN ;
5+ const GITHUB_TOKEN = import . meta. env . VITE_GITHUB_TOKEN ;
6+ const BASE_URL = 'https://api.github.com' ;
157
16- const repoResponse = await fetch ( orgReposURL , {
8+ async function fetchWithAuth ( url : string ) {
9+ const response = await fetch ( url , {
1710 headers : {
18- Authorization : `Bearer ${ token } `
11+ Authorization : `Bearer ${ GITHUB_TOKEN } `
1912 }
2013 } ) ;
2114
22- if ( ! repoResponse . ok ) {
23- console . error ( ' Failed to fetch repositories:' , repoResponse . statusText ) ;
24- throw new Error ( ' Failed to fetch repositories' ) ;
15+ if ( ! response . ok ) {
16+ console . error ( ` Failed to fetch from ${ url } :` , response . statusText ) ;
17+ throw new Error ( ` Failed to fetch from ${ url } ` ) ;
2518 }
2619
27- const repos : { name : string } [ ] = await repoResponse . json ( ) ;
20+ return response . json ( ) ;
21+ }
2822
29- const gitHubRepos : string [ ] = repos . map ( ( repo : { name : string } ) => repo . name ) ;
23+ export async function load ( ) {
24+ const allPosts = await fetchMarkdownPosts ( ) ;
25+ const posts = allPosts
26+ . sort ( ( a , b ) => new Date ( b . meta . date ) . getTime ( ) - new Date ( a . meta . date ) . getTime ( ) )
27+ . slice ( 0 , 6 ) ;
3028
31- const urls : string [ ] = gitHubRepos . map ( ( repo ) => `${ baseURL } ${ repo } /contributors` ) ;
29+ const repos = await fetchWithAuth ( `${ BASE_URL } /orgs/torrust/repos` ) ;
30+ const gitHubRepos = repos . map ( ( repo : { name : string } ) => repo . name ) ;
3231
3332 const contributorResponses = await Promise . all (
34- urls . map ( ( url ) =>
35- fetch ( url , {
36- headers : {
37- Authorization : `Bearer ${ token } `
38- }
39- } ) . then ( ( res ) => res . json ( ) )
33+ gitHubRepos . map ( ( repo : string ) =>
34+ fetchWithAuth ( `${ BASE_URL } /repos/torrust/${ repo } /contributors` )
4035 )
4136 ) ;
4237
43- const allContributors : Contributor [ ] = [ ] . concat ( ... contributorResponses ) ;
38+ const allContributors : Contributor [ ] = contributorResponses . flat ( ) ;
4439 const uniqueContributors = Array . from (
4540 new Map ( allContributors . map ( ( contributor ) => [ contributor . login , contributor ] ) ) . values ( )
4641 ) ;
0 commit comments