@@ -3,23 +3,25 @@ import { Inject, Injectable } from '@nestjs/common';
33import { Cache } from 'cache-manager' ;
44import axios from 'axios' ;
55
6-
76@Injectable ( )
87export class APIService {
9- constructor ( @Inject ( CACHE_MANAGER ) private cacheManager : Cache ) { }
8+ constructor ( @Inject ( CACHE_MANAGER ) private cacheManager : Cache ) { }
109
1110 async getGithubRepos ( username : string ) : Promise < GithubRepos | null > {
1211 try {
13- const cache = await this . cacheManager . get < string > ( `repos:${ username } ` ) ;
12+ const cache = await this . cacheManager . get < string > (
13+ `repos:${ username } `
14+ ) ;
1415
1516 if ( cache ) return JSON . parse ( cache ) ;
1617
1718 const response = await axios . post (
1819 'https://api.github.com/graphql' ,
1920 {
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 } } } } }" ,
21+ query :
22+ 'query GetUserDetails($username: String!) { user(login: $username)' +
23+ '{ name login followers { totalCount } repositories(first: 100, orderBy: {field: STARGAZERS, direction: DESC}, privacy: PUBLIC)' +
24+ '{ nodes { name owner { login } stargazerCount } } } } }' ,
2325 variables : {
2426 username : username
2527 }
@@ -33,20 +35,33 @@ export class APIService {
3335 if ( response . status !== 200 ) return null ;
3436
3537 const data : GithubRepos = response . data ;
36- data . total_stars = data ?. data ?. user ?. repositories ?. nodes ?. reduce ( ( acc , node ) => acc + node . stargazerCount , 0 ) ;
3738
38- await this . cacheManager . set ( `repos:${ username } ` , JSON . stringify ( data ) , 1000 * 60 * 60 ) ;
39+ data . total_stars = data ?. data ?. user ?. repositories ?. nodes ?. reduce (
40+ ( acc , node ) => acc + node . stargazerCount ,
41+ 0
42+ ) ;
43+
44+ await this . cacheManager . set (
45+ `repos:${ username } ` ,
46+ JSON . stringify ( data ) ,
47+ 1000 * 60 * 60
48+ ) ;
3949 return data ;
4050 } catch ( e ) {
41- console . error ( e )
42- return null
51+ console . error ( e ) ;
52+ return null ;
4353 }
4454 }
4555
46-
47- async getGithubStreak ( username : string ) : Promise < { streak : number ; longest : number ; total_contributions : number ; } | null > {
56+ async getGithubStreak ( username : string ) : Promise < {
57+ streak : number ;
58+ longest : number ;
59+ total_contributions : number ;
60+ } | null > {
4861 try {
49- const cache = await this . cacheManager . get < string > ( `streak:${ username } ` ) ;
62+ const cache = await this . cacheManager . get < string > (
63+ `streak:${ username } `
64+ ) ;
5065
5166 if ( cache ) return JSON . parse ( cache ) ;
5267
@@ -63,9 +78,10 @@ export class APIService {
6378 const response = await axios . post (
6479 'https://api.github.com/graphql' ,
6580 {
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 } } } } } }" ,
81+ query :
82+ 'query GetUserContributions($username: String!, $from: DateTime!, $to: DateTime!)' +
83+ '{ user(login: $username) { contributionsCollection(from: $from, to: $to)' +
84+ '{ contributionCalendar { weeks { contributionDays { date contributionCount } } } } } }' ,
6985 variables : {
7086 username : username ,
7187 from : fromISO ,
@@ -77,12 +93,17 @@ export class APIService {
7793 validateStatus : ( ) => true
7894 }
7995 ) ;
80- if ( response . status !== 200 || ! response . data . data . user ) return null ;
96+ if ( response . status !== 200 || ! response . data . data . user )
97+ return null ;
8198
8299 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- ) ;
100+ const days =
101+ data . data . user . contributionsCollection . contributionCalendar . weeks . flatMap (
102+ ( week ) =>
103+ week . contributionDays . map (
104+ ( val ) => val . contributionCount
105+ )
106+ ) ;
86107
87108 let streak_start = - 1 ;
88109 let streak_end = - 1 ;
@@ -99,7 +120,10 @@ export class APIService {
99120 streak_end = i ;
100121 }
101122 streak_end = i ;
102- longest_streak = Math . max ( longest_streak , streak_end - streak_start ) ;
123+ longest_streak = Math . max (
124+ longest_streak ,
125+ streak_end - streak_start
126+ ) ;
103127 } else {
104128 if ( i !== days . length - 1 ) {
105129 streak_start = - 1 ;
@@ -112,97 +136,141 @@ export class APIService {
112136 streak : streak_days ,
113137 longest : longest_streak ,
114138 total_contributions
115- }
139+ } ;
116140
117- await this . cacheManager . set ( `streak:${ username } ` , JSON . stringify ( result ) , 1000 * 60 * 60 ) ;
141+ await this . cacheManager . set (
142+ `streak:${ username } ` ,
143+ JSON . stringify ( result ) ,
144+ 1000 * 60 * 60
145+ ) ;
118146 return result ;
119147 } catch ( e ) {
120- console . error ( e )
148+ console . error ( e ) ;
121149 return null ;
122150 }
123151 }
124152
125153 async getWakatimeGlobal ( path : string ) : Promise < WakatimeGlobal | null > {
126154 try {
127- const cache = await this . cacheManager . get < string > ( `waka_global:${ path } ` ) ;
155+ const cache = await this . cacheManager . get < string > (
156+ `waka_global:${ path } `
157+ ) ;
128158
129159 if ( cache ) {
130160 return JSON . parse ( cache ) ;
131161 }
132162
133- const response = await axios . get ( `https://wakatime.com/share${ path } ` , { validateStatus : ( ) => true } ) ;
163+ const response = await axios . get (
164+ `https://wakatime.com/share${ path } ` ,
165+ { validateStatus : ( ) => true }
166+ ) ;
134167
135168 if ( response . status !== 200 ) {
136169 return null ;
137170 }
138171
139- await this . cacheManager . set ( `waka_global:${ path } ` , JSON . stringify ( response . data ) , 1000 * 60 * 60 ) ;
172+ await this . cacheManager . set (
173+ `waka_global:${ path } ` ,
174+ JSON . stringify ( response . data ) ,
175+ 1000 * 60 * 60
176+ ) ;
140177 return response . data ;
141178 } catch ( e ) {
142179 console . error ( e ) ;
143180 return null ;
144181 }
145182 }
146183
147- async getWakatimeLanguages ( path : string ) : Promise < WakatimeLanguages | null > {
184+ async getWakatimeLanguages (
185+ path : string
186+ ) : Promise < WakatimeLanguages | null > {
148187 try {
149- const cache = await this . cacheManager . get < string > ( `waka_langs:${ path } ` ) ;
188+ const cache = await this . cacheManager . get < string > (
189+ `waka_langs:${ path } `
190+ ) ;
150191
151192 if ( cache ) {
152193 return JSON . parse ( cache ) ;
153194 }
154195
155- const response = await axios . get ( `https://wakatime.com/share${ path } ` , { validateStatus : ( ) => true } ) ;
196+ const response = await axios . get (
197+ `https://wakatime.com/share${ path } ` ,
198+ { validateStatus : ( ) => true }
199+ ) ;
156200
157201 if ( response . status !== 200 ) {
158202 return null ;
159203 }
160204
161- await this . cacheManager . set ( `waka_langs:${ path } ` , JSON . stringify ( response . data ) , 1000 * 60 * 60 ) ;
205+ await this . cacheManager . set (
206+ `waka_langs:${ path } ` ,
207+ JSON . stringify ( response . data ) ,
208+ 1000 * 60 * 60
209+ ) ;
162210 return response . data ;
163211 } catch ( e ) {
164212 console . error ( e ) ;
165213 return null ;
166214 }
167215 }
168216
169- async getWeather ( api : string , query : string ) : Promise < WeatherResponse | null > {
217+ async getWeather (
218+ api : string ,
219+ query : string
220+ ) : Promise < WeatherResponse | null > {
170221 try {
171- const cache = await this . cacheManager . get < string > ( `weather:${ query } ` ) ;
222+ const cache = await this . cacheManager . get < string > (
223+ `weather:${ query } `
224+ ) ;
172225
173226 if ( cache ) {
174227 return JSON . parse ( cache ) ;
175228 }
176229
177- const response = await axios . get ( `${ api } ${ query } ` , { validateStatus : ( ) => true } ) ;
230+ const response = await axios . get ( `${ api } ${ query } ` , {
231+ validateStatus : ( ) => true
232+ } ) ;
178233
179234 if ( response . status !== 200 ) {
180235 return null ;
181236 }
182237
183- await this . cacheManager . set ( `weather:${ query } ` , JSON . stringify ( response . data ) , 1000 * 60 * 30 ) ;
238+ await this . cacheManager . set (
239+ `weather:${ query } ` ,
240+ JSON . stringify ( response . data ) ,
241+ 1000 * 60 * 30
242+ ) ;
184243 return response . data ;
185244 } catch ( e ) {
186245 console . error ( e ) ;
187246 return null ;
188247 }
189248 }
190249
191- async getActivity ( api : string , id : string ) : Promise < ActivityResponse | null > {
250+ async getActivity (
251+ api : string ,
252+ id : string
253+ ) : Promise < ActivityResponse | null > {
192254 try {
193255 const cache = await this . cacheManager . get < string > ( `activity:${ id } ` ) ;
194256
195257 if ( cache ) {
196258 return JSON . parse ( cache ) ;
197259 }
198260
199- const response = await axios . get ( `${ api } ${ id } ` , { validateStatus : ( ) => true } ) ;
261+ const response = await axios . get ( `${ api } ${ id } ` , {
262+ validateStatus : ( ) => true
263+ } ) ;
200264
201265 if ( response . status !== 200 ) {
202266 return null ;
203267 }
204268
205- await this . cacheManager . set ( `activity:${ id } ` , JSON . stringify ( response . data ) , 1000 * 60 ) ;
269+ await this . cacheManager . set (
270+ `activity:${ id } ` ,
271+ JSON . stringify ( response . data ) ,
272+ 1000 * 60
273+ ) ;
206274 return response . data ;
207275 } catch ( e ) {
208276 console . log ( e ) ;
0 commit comments