@@ -6,13 +6,13 @@ export const prerender = false;
6
6
export const load :LayoutServerLoad = async ( event ) => {
7
7
const session = await event . locals . auth ( ) ;
8
8
9
- const RandomUsers = Math . floor ( Math . random ( ) * 1000000 ) ;
9
+ const RandomUsers = Math . floor ( Math . random ( ) * 100000 ) ;
10
10
11
11
if ( ! session ?. user ) {
12
12
throw redirect ( 303 , '/login' ) ;
13
13
}
14
14
const getRandomUsers = async ( ) => {
15
- const res = await fetch ( `https://api.github.com/users?since=${ RandomUsers } &per_page=50 ` , {
15
+ const res = await fetch ( `https://api.github.com/users?since=${ RandomUsers } &per_page=100 ` , {
16
16
headers : {
17
17
Accept : 'application/vnd.github+json' ,
18
18
//@ts -ignore
@@ -21,9 +21,60 @@ export const load:LayoutServerLoad = async (event) => {
21
21
}
22
22
} ) ;
23
23
24
- return await res . json ( ) ;
24
+ const users = await res . json ( ) ;
25
+
26
+ const userDetailsPromise = users . map ( async ( user :any ) => {
27
+ const userDetailsRes = await fetch ( user . url , {
28
+ headers : {
29
+ Accept : 'application/vnd.github+json' ,
30
+ //@ts -ignore
31
+ Authorization : `Bearer ${ session ?. access_token } ` ,
32
+ 'X-Github-Api-Version' : '2022-11-28'
33
+ }
34
+ } ) ;
35
+ const userDetails = await userDetailsRes . json ( ) ;
36
+
37
+ const reposRes = await fetch ( userDetails . repos_url , {
38
+ headers :{
39
+ Accept : 'application/vnd.github+json' ,
40
+ //@ts -ignore
41
+ Authorization : `Bearer ${ session ?. access_token } ` ,
42
+ 'X-Github-Api-Version' : '2022-11-28'
43
+ }
44
+ } ) ;
45
+
46
+ const repos = await reposRes . json ( ) ;
47
+
48
+ const totalStars = repos . reduce ( ( sum : number , repo : any ) => sum + repo . stargazers_count , 0 ) ;
49
+
50
+ return {
51
+ ...userDetails ,
52
+ totalStars // Adding total stars count to user details
53
+ } ;
54
+ } ) ;
55
+
56
+ const userDetails = await Promise . all ( userDetailsPromise ) ;
57
+
58
+
59
+
60
+ //@ts -ignore
61
+ return userDetails . sort ( ( a , b ) => {
62
+ if ( b . followers === a . followers ) {
63
+ return b . totalStars - a . totalStars ;
64
+
65
+ }
66
+ return b . followers - a . followers ;
67
+ } )
68
+
69
+
25
70
} ;
71
+
72
+
26
73
return {
27
- RandomUsers : await getRandomUsers ( )
28
- } ;
29
- } ;
74
+ RandomUsers : await getRandomUsers ( )
75
+ }
76
+
77
+
78
+
79
+ } ;
80
+
0 commit comments