@@ -3,23 +3,25 @@ import { Inject, Injectable } from '@nestjs/common';
3
3
import { Cache } from 'cache-manager' ;
4
4
import axios from 'axios' ;
5
5
6
-
7
6
@Injectable ( )
8
7
export class APIService {
9
- constructor ( @Inject ( CACHE_MANAGER ) private cacheManager : Cache ) { }
8
+ constructor ( @Inject ( CACHE_MANAGER ) private cacheManager : Cache ) { }
10
9
11
10
async getGithubRepos ( username : string ) : Promise < GithubRepos | null > {
12
11
try {
13
- const cache = await this . cacheManager . get < string > ( `repos:${ username } ` ) ;
12
+ const cache = await this . cacheManager . get < string > (
13
+ `repos:${ username } `
14
+ ) ;
14
15
15
16
if ( cache ) return JSON . parse ( cache ) ;
16
17
17
18
const response = await axios . post (
18
19
'https://api.github.com/graphql' ,
19
20
{
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 } } } } }' ,
23
25
variables : {
24
26
username : username
25
27
}
@@ -33,20 +35,33 @@ export class APIService {
33
35
if ( response . status !== 200 ) return null ;
34
36
35
37
const data : GithubRepos = response . data ;
36
- data . total_stars = data ?. data ?. user ?. repositories ?. nodes ?. reduce ( ( acc , node ) => acc + node . stargazerCount , 0 ) ;
37
38
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
+ ) ;
39
49
return data ;
40
50
} catch ( e ) {
41
- console . error ( e )
42
- return null
51
+ console . error ( e ) ;
52
+ return null ;
43
53
}
44
54
}
45
55
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 > {
48
61
try {
49
- const cache = await this . cacheManager . get < string > ( `streak:${ username } ` ) ;
62
+ const cache = await this . cacheManager . get < string > (
63
+ `streak:${ username } `
64
+ ) ;
50
65
51
66
if ( cache ) return JSON . parse ( cache ) ;
52
67
@@ -63,9 +78,10 @@ export class APIService {
63
78
const response = await axios . post (
64
79
'https://api.github.com/graphql' ,
65
80
{
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 } } } } } }' ,
69
85
variables : {
70
86
username : username ,
71
87
from : fromISO ,
@@ -77,12 +93,17 @@ export class APIService {
77
93
validateStatus : ( ) => true
78
94
}
79
95
) ;
80
- if ( response . status !== 200 || ! response . data . data . user ) return null ;
96
+ if ( response . status !== 200 || ! response . data . data . user )
97
+ return null ;
81
98
82
99
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
+ ) ;
86
107
87
108
let streak_start = - 1 ;
88
109
let streak_end = - 1 ;
@@ -99,7 +120,10 @@ export class APIService {
99
120
streak_end = i ;
100
121
}
101
122
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
+ ) ;
103
127
} else {
104
128
if ( i !== days . length - 1 ) {
105
129
streak_start = - 1 ;
@@ -112,97 +136,141 @@ export class APIService {
112
136
streak : streak_days ,
113
137
longest : longest_streak ,
114
138
total_contributions
115
- }
139
+ } ;
116
140
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
+ ) ;
118
146
return result ;
119
147
} catch ( e ) {
120
- console . error ( e )
148
+ console . error ( e ) ;
121
149
return null ;
122
150
}
123
151
}
124
152
125
153
async getWakatimeGlobal ( path : string ) : Promise < WakatimeGlobal | null > {
126
154
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
+ ) ;
128
158
129
159
if ( cache ) {
130
160
return JSON . parse ( cache ) ;
131
161
}
132
162
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
+ ) ;
134
167
135
168
if ( response . status !== 200 ) {
136
169
return null ;
137
170
}
138
171
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
+ ) ;
140
177
return response . data ;
141
178
} catch ( e ) {
142
179
console . error ( e ) ;
143
180
return null ;
144
181
}
145
182
}
146
183
147
- async getWakatimeLanguages ( path : string ) : Promise < WakatimeLanguages | null > {
184
+ async getWakatimeLanguages (
185
+ path : string
186
+ ) : Promise < WakatimeLanguages | null > {
148
187
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
+ ) ;
150
191
151
192
if ( cache ) {
152
193
return JSON . parse ( cache ) ;
153
194
}
154
195
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
+ ) ;
156
200
157
201
if ( response . status !== 200 ) {
158
202
return null ;
159
203
}
160
204
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
+ ) ;
162
210
return response . data ;
163
211
} catch ( e ) {
164
212
console . error ( e ) ;
165
213
return null ;
166
214
}
167
215
}
168
216
169
- async getWeather ( api : string , query : string ) : Promise < WeatherResponse | null > {
217
+ async getWeather (
218
+ api : string ,
219
+ query : string
220
+ ) : Promise < WeatherResponse | null > {
170
221
try {
171
- const cache = await this . cacheManager . get < string > ( `weather:${ query } ` ) ;
222
+ const cache = await this . cacheManager . get < string > (
223
+ `weather:${ query } `
224
+ ) ;
172
225
173
226
if ( cache ) {
174
227
return JSON . parse ( cache ) ;
175
228
}
176
229
177
- const response = await axios . get ( `${ api } ${ query } ` , { validateStatus : ( ) => true } ) ;
230
+ const response = await axios . get ( `${ api } ${ query } ` , {
231
+ validateStatus : ( ) => true
232
+ } ) ;
178
233
179
234
if ( response . status !== 200 ) {
180
235
return null ;
181
236
}
182
237
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
+ ) ;
184
243
return response . data ;
185
244
} catch ( e ) {
186
245
console . error ( e ) ;
187
246
return null ;
188
247
}
189
248
}
190
249
191
- async getActivity ( api : string , id : string ) : Promise < ActivityResponse | null > {
250
+ async getActivity (
251
+ api : string ,
252
+ id : string
253
+ ) : Promise < ActivityResponse | null > {
192
254
try {
193
255
const cache = await this . cacheManager . get < string > ( `activity:${ id } ` ) ;
194
256
195
257
if ( cache ) {
196
258
return JSON . parse ( cache ) ;
197
259
}
198
260
199
- const response = await axios . get ( `${ api } ${ id } ` , { validateStatus : ( ) => true } ) ;
261
+ const response = await axios . get ( `${ api } ${ id } ` , {
262
+ validateStatus : ( ) => true
263
+ } ) ;
200
264
201
265
if ( response . status !== 200 ) {
202
266
return null ;
203
267
}
204
268
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
+ ) ;
206
274
return response . data ;
207
275
} catch ( e ) {
208
276
console . log ( e ) ;
0 commit comments