@@ -2,6 +2,19 @@ import { CACHE_MANAGER } from '@nestjs/cache-manager';
2
2
import { Inject , Injectable } from '@nestjs/common' ;
3
3
import { Cache } from 'cache-manager' ;
4
4
import axios from 'axios' ;
5
+ import http from 'http' ;
6
+ import https from 'https' ;
7
+
8
+ const agentOptions = {
9
+ keepAlive : true ,
10
+ timeout : 5000
11
+ } ;
12
+
13
+ const instance = axios . create ( {
14
+ timeout : 5000 ,
15
+ httpAgent : new http . Agent ( agentOptions ) ,
16
+ httpsAgent : new https . Agent ( agentOptions )
17
+ } ) ;
5
18
6
19
@Injectable ( )
7
20
export class APIService {
@@ -15,7 +28,7 @@ export class APIService {
15
28
16
29
if ( cache ) return JSON . parse ( cache ) ;
17
30
18
- const response = await axios . post (
31
+ const response = await instance . post (
19
32
'https://api.github.com/graphql' ,
20
33
{
21
34
query :
@@ -74,7 +87,7 @@ export class APIService {
74
87
from . setFullYear ( from . getFullYear ( ) - 1 ) ;
75
88
const fromISO = from . toISOString ( ) ;
76
89
77
- const response = await axios . post (
90
+ const response = await instance . post (
78
91
'https://api.github.com/graphql' ,
79
92
{
80
93
query :
@@ -159,7 +172,7 @@ export class APIService {
159
172
return JSON . parse ( cache ) ;
160
173
}
161
174
162
- const response = await axios . get (
175
+ const response = await instance . get (
163
176
`https://wakatime.com/share${ path } ` ,
164
177
{ validateStatus : ( ) => true }
165
178
) ;
@@ -192,7 +205,7 @@ export class APIService {
192
205
return JSON . parse ( cache ) ;
193
206
}
194
207
195
- const response = await axios . get (
208
+ const response = await instance . get (
196
209
`https://wakatime.com/share${ path } ` ,
197
210
{ validateStatus : ( ) => true }
198
211
) ;
@@ -226,7 +239,7 @@ export class APIService {
226
239
return JSON . parse ( cache ) ;
227
240
}
228
241
229
- const response = await axios . get ( `${ api } ${ query } ` , {
242
+ const response = await instance . get ( `${ api } ${ query } ` , {
230
243
validateStatus : ( ) => true
231
244
} ) ;
232
245
@@ -257,7 +270,7 @@ export class APIService {
257
270
return JSON . parse ( cache ) ;
258
271
}
259
272
260
- const response = await axios . get ( `${ api } ${ id } ` , {
273
+ const response = await instance . get ( `${ api } ${ id } ` , {
261
274
validateStatus : ( ) => true
262
275
} ) ;
263
276
0 commit comments