1
+ import { redirect } from '@sveltejs/kit' ;
2
+ import type { PageServerLoad } from './$types' ;
3
+
4
+ export const prerender = 'auto' ;
5
+
6
+ export const load : PageServerLoad = async ( event ) => {
7
+ const session = await event . locals . auth ( ) ;
8
+
9
+ if ( ! session ?. user ) {
10
+ throw redirect ( 303 , '/login' ) ;
11
+ }
12
+
13
+ const fetchFollowers = async ( ) => {
14
+ try {
15
+ const res = await fetch ( `https://api.github.com/user/followers` , {
16
+ headers : {
17
+ Accept : 'application/vnd.github+json' ,
18
+ //@ts -ignore
19
+ Authorization : `Bearer ${ session ?. access_token } ` ,
20
+ 'X-Github-Api-Version' : '2022-11-28'
21
+ }
22
+ } ) ;
23
+
24
+ if ( ! res . ok ) throw new Error ( 'Failed to fetch followers' ) ;
25
+ return await res . json ( ) ;
26
+ } catch ( error ) {
27
+ console . error ( 'Error fetching followers:' , error ) ;
28
+ return null ;
29
+ }
30
+ } ;
31
+
32
+ const fetchUserInfo = async ( ) => {
33
+ try {
34
+ const res = await fetch ( `https://api.github.com/user` , {
35
+ headers : {
36
+ Accept : 'application/vnd.github+json' ,
37
+ //@ts -ignore
38
+ Authorization : `Bearer ${ session ?. access_token } ` ,
39
+ 'X-Github-Api-Version' : '2022-11-28'
40
+ }
41
+ } ) ;
42
+
43
+ if ( ! res . ok ) throw new Error ( 'Failed to fetch user info' ) ;
44
+ return await res . json ( ) ;
45
+ } catch ( error ) {
46
+ console . error ( 'Error fetching user info:' , error ) ;
47
+ return null ;
48
+ }
49
+ } ;
50
+
51
+ return {
52
+ user : await fetchUserInfo ( ) ,
53
+ session,
54
+ followers : await fetchFollowers ( )
55
+ } ;
56
+ } ;
0 commit comments