@@ -126,22 +126,37 @@ const CONTRIBUTION_QUERY = gql`
126126
127127// ========== Helper Functions ==========
128128
129+ /**
130+ * Creates a GraphQL client with GitHub token
131+ */
132+ // Import dynamic private environment variables
133+ import { env } from '$env/dynamic/private' ;
134+
129135/**
130136 * Creates a GraphQL client with GitHub token
131137 */
132138function createGitHubClient ( ) : GraphQLClient {
133- const token = process . env . VITE_GITHUB_TOKEN ;
139+ // Try standard GITHUB_TOKEN first, then fall back to VITE_ prefixed version
140+ // Using $env/dynamic/private ensures we get the runtime value in all adapters
141+ let token = env . GITHUB_TOKEN || env . VITE_GITHUB_TOKEN || process . env . VITE_GITHUB_TOKEN || '' ;
142+ token = token . trim ( ) ;
134143
135144 console . log ( `[GitHub API] Checking for GitHub token...` ) ;
136145 if ( ! token ) {
137- console . error ( `[GitHub API] No VITE_GITHUB_TOKEN found in environment` ) ;
146+ console . error ( `[GitHub API] No GITHUB_TOKEN or VITE_GITHUB_TOKEN found in environment` ) ;
138147 throw error ( 500 , 'GitHub token not configured' ) ;
139148 }
140- console . log ( `[GitHub API] Token found (length: ${ token . length } )` ) ;
149+
150+ // Log token details for debugging (safe mask)
151+ const mask =
152+ token . length > 8 ? `${ token . substring ( 0 , 4 ) } ...${ token . substring ( token . length - 4 ) } ` : '***' ;
153+ console . log ( `[GitHub API] Token found (length: ${ token . length } , prefix: ${ mask } )` ) ;
141154
142155 return new GraphQLClient ( 'https://api.github.com/graphql' , {
143156 headers : {
144- Authorization : `bearer ${ token } `
157+ Authorization : `Bearer ${ token } ` ,
158+ 'User-Agent' : 'Svelte-MiniApps-GitHub-Tracker' ,
159+ 'X-GitHub-Api-Version' : '2022-11-28'
145160 }
146161 } ) ;
147162}
@@ -352,16 +367,20 @@ export const load: PageServerLoad = async ({ params }) => {
352367 }
353368
354369 // Handle specific error types
355- if ( err && typeof err === 'object' && 'status' in err ) {
356- const statusCode = ( err as any ) . status ;
357- console . error ( `[GitHub API] HTTP status code: ${ statusCode } ` ) ;
370+ if ( err && typeof err === 'object' ) {
371+ // graphql-request ClientError structure
372+ const response = ( err as any ) . response ;
373+ const statusCode = response ?. status || ( err as any ) . status ;
374+
375+ console . error ( `[GitHub API] Detected status code: ${ statusCode } ` ) ;
376+
358377 if ( statusCode === 404 ) {
359378 console . error ( `[GitHub API] Throwing 404 - User not found` ) ;
360379 throw error ( 404 , `GitHub user "${ username } " not found` ) ;
361380 }
362381 if ( statusCode === 401 || statusCode === 403 ) {
363- console . error ( `[GitHub API] Throwing 500 - Authentication failed` ) ;
364- throw error ( 500 , 'GitHub API authentication failed' ) ;
382+ console . error ( `[GitHub API] Throwing 500 - Authentication failed (Status: ${ statusCode } ) ` ) ;
383+ throw error ( 500 , 'GitHub API authentication failed. Please check your token configuration. ' ) ;
365384 }
366385 }
367386
0 commit comments