@@ -9,174 +9,204 @@ export class APIService {
99 constructor ( @Inject ( CACHE_MANAGER ) private cacheManager : Cache ) { }
1010
1111 async getGithubRepos ( username : string ) : Promise < GithubRepos | null > {
12- const cache = await this . cacheManager . get < string > ( `repos:${ username } ` ) ;
13-
14- if ( cache ) return JSON . parse ( cache ) ;
15-
16- const response = await axios . post (
17- 'https://api.github.com/graphql' ,
18- {
19- query : "query GetUserDetails($username: String!) { user(login: $username)" +
20- "{ name login followers { totalCount } repositories(first: 100, orderBy: {field: STARGAZERS, direction: DESC})" +
21- "{ nodes { name owner { login } stargazerCount } } } } }" ,
22- variables : {
23- username : username
12+ try {
13+ const cache = await this . cacheManager . get < string > ( `repos:${ username } ` ) ;
14+
15+ if ( cache ) return JSON . parse ( cache ) ;
16+
17+ const response = await axios . post (
18+ 'https://api.github.com/graphql' ,
19+ {
20+ query : "query GetUserDetails($username: String!) { user(login: $username)" +
21+ "{ name login followers { totalCount } repositories(first: 100, orderBy: {field: STARGAZERS, direction: DESC})" +
22+ "{ nodes { name owner { login } stargazerCount } } } } }" ,
23+ variables : {
24+ username : username
25+ }
26+ } ,
27+ {
28+ headers : { Authorization : `Bearer ${ process . env . GITHUB } ` } ,
29+ validateStatus : ( ) => true
2430 }
25- } ,
26- {
27- headers : { Authorization : `Bearer ${ process . env . GITHUB } ` } ,
28- validateStatus : ( ) => true
29- }
30- ) ;
31+ ) ;
3132
32- if ( response . status !== 200 ) return null ;
33+ if ( response . status !== 200 ) return null ;
3334
34- const data : GithubRepos = response . data ;
35- data . total_stars = data ?. data ?. user ?. repositories ?. nodes ?. reduce ( ( acc , node ) => acc + node . stargazerCount , 0 ) ;
35+ const data : GithubRepos = response . data ;
36+ data . total_stars = data ?. data ?. user ?. repositories ?. nodes ?. reduce ( ( acc , node ) => acc + node . stargazerCount , 0 ) ;
3637
37- await this . cacheManager . set ( `repos:${ username } ` , JSON . stringify ( data ) , 1000 * 60 * 60 ) ;
38- return data ;
38+ await this . cacheManager . set ( `repos:${ username } ` , JSON . stringify ( data ) , 1000 * 60 * 60 ) ;
39+ return data ;
40+ } catch ( e ) {
41+ console . error ( e )
42+ return null
43+ }
3944 }
4045
4146
4247 async getGithubStreak ( username : string ) : Promise < { streak : number ; longest : number ; total_contributions : number ; } | null > {
43- const cache = await this . cacheManager . get < string > ( `streak:${ username } ` ) ;
44-
45- if ( cache ) return JSON . parse ( cache ) ;
46-
47- const date = new Date ( ) ;
48- const timezoneOffset = date . getTimezoneOffset ( ) ;
49- const now = date . getTime ( ) - timezoneOffset * 60000 ;
50-
51- const nowISO = new Date ( now ) . toISOString ( ) ;
52-
53- const from = new Date ( now ) ;
54- from . setFullYear ( from . getFullYear ( ) - 1 ) ;
55- const fromISO = from . toISOString ( ) ;
56-
57- const response = await axios . post (
58- 'https://api.github.com/graphql' ,
59- {
60- query : "query GetUserContributions($username: String!, $from: DateTime!, $to: DateTime!)" +
61- "{ user(login: $username) { contributionsCollection(from: $from, to: $to)" +
62- "{ contributionCalendar { weeks { contributionDays { date contributionCount } } } } } }" ,
63- variables : {
64- username : username ,
65- from : fromISO ,
66- to : nowISO
48+ try {
49+ const cache = await this . cacheManager . get < string > ( `streak:${ username } ` ) ;
50+
51+ if ( cache ) return JSON . parse ( cache ) ;
52+
53+ const date = new Date ( ) ;
54+ const timezoneOffset = date . getTimezoneOffset ( ) ;
55+ const now = date . getTime ( ) - timezoneOffset * 60000 ;
56+
57+ const nowISO = new Date ( now ) . toISOString ( ) ;
58+
59+ const from = new Date ( now ) ;
60+ from . setFullYear ( from . getFullYear ( ) - 1 ) ;
61+ const fromISO = from . toISOString ( ) ;
62+
63+ const response = await axios . post (
64+ 'https://api.github.com/graphql' ,
65+ {
66+ query : "query GetUserContributions($username: String!, $from: DateTime!, $to: DateTime!)" +
67+ "{ user(login: $username) { contributionsCollection(from: $from, to: $to)" +
68+ "{ contributionCalendar { weeks { contributionDays { date contributionCount } } } } } }" ,
69+ variables : {
70+ username : username ,
71+ from : fromISO ,
72+ to : nowISO
73+ }
74+ } ,
75+ {
76+ headers : { Authorization : `Bearer ${ process . env . GITHUB } ` } ,
77+ validateStatus : ( ) => true
6778 }
68- } ,
69- {
70- headers : { Authorization : `Bearer ${ process . env . GITHUB } ` } ,
71- validateStatus : ( ) => true
72- }
73- ) ;
74- if ( response . status !== 200 || ! response . data . data . user ) return null ;
75-
76- const data = response . data as GithubStreak ;
77- const days = data . data . user . contributionsCollection . contributionCalendar . weeks . flatMap ( week =>
78- week . contributionDays . map ( val => val . contributionCount )
79- ) ;
80-
81- let streak_start = - 1 ;
82- let streak_end = - 1 ;
83- let longest_streak = 0 ;
84- let total_contributions = 0 ;
85-
86- for ( let i = 0 ; i < days . length ; i ++ ) {
87- const day = days [ i ] ;
88- total_contributions += day ;
89-
90- if ( day !== 0 ) {
91- if ( streak_start === - 1 ) {
92- streak_start = i ;
79+ ) ;
80+ if ( response . status !== 200 || ! response . data . data . user ) return null ;
81+
82+ const data = response . data as GithubStreak ;
83+ const days = data . data . user . contributionsCollection . contributionCalendar . weeks . flatMap ( week =>
84+ week . contributionDays . map ( val => val . contributionCount )
85+ ) ;
86+
87+ let streak_start = - 1 ;
88+ let streak_end = - 1 ;
89+ let longest_streak = 0 ;
90+ let total_contributions = 0 ;
91+
92+ for ( let i = 0 ; i < days . length ; i ++ ) {
93+ const day = days [ i ] ;
94+ total_contributions += day ;
95+
96+ if ( day !== 0 ) {
97+ if ( streak_start === - 1 ) {
98+ streak_start = i ;
99+ streak_end = i ;
100+ }
93101 streak_end = i ;
94- }
95- streak_end = i ;
96- longest_streak = Math . max ( longest_streak , streak_end - streak_start ) ;
97- } else {
98- if ( i !== days . length - 1 ) {
99- streak_start = - 1 ;
100- streak_end = - 1 ;
102+ longest_streak = Math . max ( longest_streak , streak_end - streak_start ) ;
103+ } else {
104+ if ( i !== days . length - 1 ) {
105+ streak_start = - 1 ;
106+ streak_end = - 1 ;
107+ }
101108 }
102109 }
103- }
104- const streak_days = streak_end - streak_start ;
105- const result = {
106- streak : streak_days ,
107- longest : longest_streak ,
108- total_contributions
109- }
110+ const streak_days = streak_end - streak_start ;
111+ const result = {
112+ streak : streak_days ,
113+ longest : longest_streak ,
114+ total_contributions
115+ }
110116
111- await this . cacheManager . set ( `streak:${ username } ` , JSON . stringify ( result ) , 1000 * 60 * 60 ) ;
112- return result ;
117+ await this . cacheManager . set ( `streak:${ username } ` , JSON . stringify ( result ) , 1000 * 60 * 60 ) ;
118+ return result ;
119+ } catch ( e ) {
120+ console . error ( e )
121+ return null ;
122+ }
113123 }
114124
115125 async getWakatimeGlobal ( path : string ) : Promise < WakatimeGlobal | null > {
116- const cache = await this . cacheManager . get < string > ( `waka_global:${ path } ` ) ;
126+ try {
127+ const cache = await this . cacheManager . get < string > ( `waka_global:${ path } ` ) ;
117128
118- if ( cache ) {
119- return JSON . parse ( cache ) ;
120- }
129+ if ( cache ) {
130+ return JSON . parse ( cache ) ;
131+ }
121132
122- const response = await axios . get ( `https://wakatime.com/share${ path } ` , { validateStatus : ( ) => true } ) ;
133+ const response = await axios . get ( `https://wakatime.com/share${ path } ` , { validateStatus : ( ) => true } ) ;
123134
124- if ( response . status !== 200 ) {
135+ if ( response . status !== 200 ) {
136+ return null ;
137+ }
138+
139+ await this . cacheManager . set ( `waka_global:${ path } ` , JSON . stringify ( response . data ) , 1000 * 60 * 60 ) ;
140+ return response . data ;
141+ } catch ( e ) {
142+ console . error ( e ) ;
125143 return null ;
126144 }
127-
128- await this . cacheManager . set ( `waka_global:${ path } ` , JSON . stringify ( response . data ) , 1000 * 60 * 60 ) ;
129- return response . data ;
130145 }
131146
132147 async getWakatimeLanguages ( path : string ) : Promise < WakatimeLanguages | null > {
133- const cache = await this . cacheManager . get < string > ( `waka_langs:${ path } ` ) ;
148+ try {
149+ const cache = await this . cacheManager . get < string > ( `waka_langs:${ path } ` ) ;
134150
135- if ( cache ) {
136- return JSON . parse ( cache ) ;
137- }
151+ if ( cache ) {
152+ return JSON . parse ( cache ) ;
153+ }
154+
155+ const response = await axios . get ( `https://wakatime.com/share${ path } ` , { validateStatus : ( ) => true } ) ;
138156
139- const response = await axios . get ( `https://wakatime.com/share${ path } ` , { validateStatus : ( ) => true } ) ;
157+ if ( response . status !== 200 ) {
158+ return null ;
159+ }
140160
141- if ( response . status !== 200 ) {
161+ await this . cacheManager . set ( `waka_langs:${ path } ` , JSON . stringify ( response . data ) , 1000 * 60 * 60 ) ;
162+ return response . data ;
163+ } catch ( e ) {
164+ console . error ( e ) ;
142165 return null ;
143166 }
144-
145- await this . cacheManager . set ( `waka_langs:${ path } ` , JSON . stringify ( response . data ) , 1000 * 60 * 60 ) ;
146- return response . data ;
147167 }
148168
149169 async getWeather ( api : string , query : string ) : Promise < WeatherResponse | null > {
150- const cache = await this . cacheManager . get < string > ( `weather:${ query } ` ) ;
170+ try {
171+ const cache = await this . cacheManager . get < string > ( `weather:${ query } ` ) ;
151172
152- if ( cache ) {
153- return JSON . parse ( cache ) ;
154- }
173+ if ( cache ) {
174+ return JSON . parse ( cache ) ;
175+ }
176+
177+ const response = await axios . get ( `${ api } ${ query } ` , { validateStatus : ( ) => true } ) ;
155178
156- const response = await axios . get ( `${ api } ${ query } ` , { validateStatus : ( ) => true } ) ;
179+ if ( response . status !== 200 ) {
180+ return null ;
181+ }
157182
158- if ( response . status !== 200 ) {
183+ await this . cacheManager . set ( `weather:${ query } ` , JSON . stringify ( response . data ) , 1000 * 60 * 30 ) ;
184+ return response . data ;
185+ } catch ( e ) {
186+ console . error ( e ) ;
159187 return null ;
160188 }
161-
162- await this . cacheManager . set ( `weather:${ query } ` , JSON . stringify ( response . data ) , 1000 * 60 * 30 ) ;
163- return response . data ;
164189 }
165190
166191 async getActivity ( api : string , id : string ) : Promise < ActivityResponse | null > {
167- const cache = await this . cacheManager . get < string > ( `activity:${ id } ` ) ;
192+ try {
193+ const cache = await this . cacheManager . get < string > ( `activity:${ id } ` ) ;
168194
169- if ( cache ) {
170- return JSON . parse ( cache ) ;
171- }
195+ if ( cache ) {
196+ return JSON . parse ( cache ) ;
197+ }
172198
173- const response = await axios . get ( `${ api } ${ id } ` , { validateStatus : ( ) => true } ) ;
199+ const response = await axios . get ( `${ api } ${ id } ` , { validateStatus : ( ) => true } ) ;
174200
175- if ( response . status !== 200 ) {
201+ if ( response . status !== 200 ) {
202+ return null ;
203+ }
204+
205+ await this . cacheManager . set ( `activity:${ id } ` , JSON . stringify ( response . data ) , 1000 * 60 ) ;
206+ return response . data ;
207+ } catch ( e ) {
208+ console . log ( e ) ;
176209 return null ;
177210 }
178-
179- await this . cacheManager . set ( `activity:${ id } ` , JSON . stringify ( response . data ) , 1000 * 60 ) ;
180- return response . data ;
181211 }
182212}
0 commit comments