@@ -15,6 +15,69 @@ export interface SteamRecentGamesResponse {
1515 } ;
1616}
1717
18+ // more cors proxy cuz reliability issues
19+ const CORS_PROXIES = [
20+ 'https://api.codetabs.com/v1/proxy?quest=' ,
21+ 'https://api.allorigins.win/get?url=' ,
22+ 'https://corsproxy.io/?' ,
23+ 'https://cors-anywhere.herokuapp.com/' ,
24+ ] ;
25+
26+ const fetchWithProxy = async ( apiUrl : string , proxyIndex = 0 ) : Promise < any > => {
27+ if ( proxyIndex >= CORS_PROXIES . length ) {
28+ throw new Error ( 'All proxy services failed' ) ;
29+ }
30+
31+ const proxy = CORS_PROXIES [ proxyIndex ] ;
32+ let proxyUrl : string ;
33+
34+ // Different proxies have different URL formats
35+ if ( proxy . includes ( 'allorigins' ) ) {
36+ proxyUrl = `${ proxy } ${ encodeURIComponent ( apiUrl ) } ` ;
37+ } else if ( proxy . includes ( 'codetabs' ) ) {
38+ proxyUrl = `${ proxy } ${ encodeURIComponent ( apiUrl ) } ` ;
39+ } else {
40+ proxyUrl = `${ proxy } ${ apiUrl } ` ;
41+ }
42+
43+ try {
44+ const controller = new AbortController ( ) ;
45+ const timeoutId = setTimeout ( ( ) => controller . abort ( ) , 10000 ) ; // 10 second timeout
46+
47+ const response = await fetch ( proxyUrl , {
48+ method : 'GET' ,
49+ headers : {
50+ 'Accept' : 'application/json' ,
51+ } ,
52+ signal : controller . signal
53+ } ) ;
54+
55+ clearTimeout ( timeoutId ) ;
56+
57+ if ( ! response . ok ) {
58+ throw new Error ( `HTTP ${ response . status } : ${ response . statusText } ` ) ;
59+ }
60+
61+ const data = await response . json ( ) ;
62+
63+ // Handle different proxy response formats
64+ if ( proxy . includes ( 'allorigins' ) && data . contents ) {
65+ return JSON . parse ( data . contents ) ;
66+ } else if ( proxy . includes ( 'codetabs' ) && data ) {
67+ return data ;
68+ } else if ( data ) {
69+ return data ;
70+ } else {
71+ throw new Error ( 'No valid data in response' ) ;
72+ }
73+
74+ } catch ( error ) {
75+ console . warn ( `Proxy ${ proxy } failed:` , error ) ;
76+ // Try next proxy
77+ return fetchWithProxy ( apiUrl , proxyIndex + 1 ) ;
78+ }
79+ } ;
80+
1881/**
1982 * Fetch recently played Steam games
2083 */
@@ -28,31 +91,15 @@ export const fetchRecentGames = async (): Promise<SteamGame[]> => {
2891 return [ ] ;
2992 }
3093
31- // Use CORS proxy directly to avoid CORS errors
94+ // Build Steam API URL
3295 const apiUrl = `https://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v1/?key=${ STEAM_API_KEY } &steamid=${ STEAM_ID } &format=json` ;
33- const proxyUrl = `https://api.allorigins.win/get?url=${ encodeURIComponent ( apiUrl ) } ` ;
3496
35- const response = await fetch ( proxyUrl , {
36- method : 'GET' ,
37- headers : {
38- 'Accept' : 'application/json' ,
39- }
40- } ) ;
41-
42- if ( ! response . ok ) {
43- throw new Error ( `Proxy request failed: ${ response . status } ` ) ;
44- }
45-
46- const proxyData = await response . json ( ) ;
47-
48- if ( ! proxyData . contents ) {
49- throw new Error ( 'No data received from proxy' ) ;
50- }
51-
52- const steamData : SteamRecentGamesResponse = JSON . parse ( proxyData . contents ) ;
97+ // Try fetching with different proxy services
98+ const steamData : SteamRecentGamesResponse = await fetchWithProxy ( apiUrl ) ;
5399
54100 if ( ! steamData . response || ! steamData . response . games ) {
55- throw new Error ( 'Invalid Steam API response' ) ;
101+ console . warn ( 'No recent games found in Steam API response' ) ;
102+ return [ ] ;
56103 }
57104
58105 return steamData . response . games . slice ( 0 , 4 ) ; // Show top 4 games
0 commit comments