1- import got , { Headers , Method , OptionsOfJSONResponseBody } from 'got' ;
1+ import got , { Got , Headers , Method , OptionsOfJSONResponseBody } from 'got' ;
22import { FuusorDataSet , IFuusorDataSetOptions } from './data-set' ;
33import { FuusorUser } from './user' ;
44import { FuusorUserGroup } from './user-group' ;
55import { HttpsAgent } from 'agentkeepalive' ;
6+ import * as https from 'https' ;
7+ import CacheableLookup from 'cacheable-lookup' ;
68
9+ // Create global https agent
710const httpsAgent = new HttpsAgent ( ) ;
811
912export interface IFuusorApiClientOptions {
@@ -20,11 +23,20 @@ export interface IFuusorApiClientOptions {
2023
2124 /** Request timeout, defaults to 120000 (120 secs) */
2225 timeout ?: number ;
26+
27+ /** Instance of `https.Agent` or `true` to enable internal Keep Alive Agent, defaults to `true` */
28+ keepAliveAgent ?: boolean | https . Agent ;
29+
30+ /** Instance of `cacheable-lookup` or `true` to enable internal DNS cache, defaults to `true` */
31+ dnsCache ?: boolean | CacheableLookup ;
2332}
2433
2534export class FuusorApiClient {
2635 options : IFuusorApiClientOptions ;
2736
37+ /** Got instance to be used when making requests */
38+ gotInstance : Got ;
39+
2840 readonly users : FuusorUser ;
2941 readonly userGroups : FuusorUserGroup ;
3042
@@ -34,36 +46,42 @@ export class FuusorApiClient {
3446 /** @private */
3547 accessTokensTimeout : any ;
3648
37- /** @private */
38- httpsAgent : HttpsAgent = httpsAgent ;
39-
4049 constructor ( options : IFuusorApiClientOptions ) {
41- // Set default connect URI
42- options . uriConnect = options . uriConnect || 'https://api.fuusor.fi/connect/token' ;
43- options . uriBase = options . uriBase || 'https://api.fuusor.fi/api/v1' ;
44- options . uriUploadFile = options . uriUploadFile || `${ options . uriBase } /uploadfile` ;
45- options . uriDataset = options . uriDataset || `${ options . uriBase } /dataset` ;
46-
47- // Set default timeout
48- options . timeout = options . timeout || 120000 ;
49-
50- if ( ! options . clientId ) {
51- throw new Error ( 'Missing options.clientId' ) ;
52- }
53-
54- if ( ! options . clientSecret ) {
55- throw new Error ( 'Missing options.clientSecret' ) ;
50+ this . options = options || { } ;
51+
52+ // Check that needed options are included
53+ if ( ! this . options . clientId ) throw new Error ( 'Missing options.clientId' ) ;
54+ if ( ! this . options . clientSecret ) throw new Error ( 'Missing options.clientSecret' ) ;
55+ if ( ! this . options . username ) throw new Error ( 'Missing options.username' ) ;
56+ if ( ! this . options . password ) throw new Error ( 'Missing options.password' ) ;
57+
58+ // Set default connect URIs if none was provided
59+ if ( ! this . options . uriConnect ) this . options . uriConnect = 'https://api.fuusor.fi/connect/token' ;
60+ if ( ! this . options . uriBase ) this . options . uriBase = 'https://api.fuusor.fi/api/v1' ;
61+ if ( ! this . options . uriUploadFile ) this . options . uriUploadFile = `${ this . options . uriBase } /uploadfile` ;
62+ if ( ! this . options . uriDataset ) this . options . uriDataset = `${ this . options . uriBase } /dataset` ;
63+
64+ // Set default timeout if none was provided
65+ if ( ! this . options . timeout ) this . options . timeout = 120000 ;
66+
67+ // Use internal keepAliveAgent by default
68+ if ( this . options . keepAliveAgent === true || this . options . keepAliveAgent === undefined ) {
69+ this . options . keepAliveAgent = httpsAgent ;
5670 }
5771
58- if ( ! options . username ) {
59- throw new Error ( 'Missing options.username' ) ;
72+ // Use internal dnsCache by default (falls back to got's dnsCache)
73+ if ( this . options . dnsCache === true || this . options . dnsCache === undefined ) {
74+ this . options . dnsCache = true ;
6075 }
6176
62- if ( ! options . password ) {
63- throw new Error ( 'Missing options.password' ) ;
64- }
77+ // Set gotInstance defaults, can also include other options
78+ this . gotInstance = got . extend ( {
79+ // Agent options
80+ agent : { https : this . options . keepAliveAgent || undefined } ,
6581
66- this . options = options ;
82+ // DNS caching options
83+ dnsCache : this . options . dnsCache || undefined
84+ } ) ;
6785
6886 this . users = new FuusorUser ( this ) ;
6987 this . userGroups = new FuusorUserGroup ( this ) ;
@@ -105,25 +123,19 @@ export class FuusorApiClient {
105123 async saveDataSet ( accessToken : string , data : any ) : Promise < void > {
106124 const json = this . _minimizeObjectKeys ( data ) ;
107125
108- await got . post ( this . options . uriDataset || '' , {
126+ await this . gotInstance . post ( this . options . uriDataset || '' , {
109127 json,
110-
111128 timeout : this . options . timeout ,
112-
113129 headers : {
114130 Authorization : `Bearer ${ accessToken } `
115- } ,
116-
117- agent : {
118- https : this . httpsAgent
119131 }
120132 } ) ;
121133
122134 return ;
123135 }
124136
125137 async fetchAccessTokenForDataSetUpload ( ) : Promise < string > {
126- const { access_token } = ( await got
138+ const { access_token } = ( await this . gotInstance
127139 . post ( this . options . uriConnect || '' , {
128140 form : {
129141 scope : 'fileupload' ,
@@ -133,10 +145,6 @@ export class FuusorApiClient {
133145 username : this . options . username ,
134146 password : this . options . password ,
135147 filetype : 'JsonTransformer'
136- } ,
137-
138- agent : {
139- https : this . httpsAgent
140148 }
141149 } )
142150 . json ( ) ) as any ;
@@ -153,7 +161,7 @@ export class FuusorApiClient {
153161 async refreshAccessToken ( scope : string ) : Promise < void > {
154162 // Check if access token is expired
155163 if ( ! this . accessTokens ?. [ scope ] ) {
156- const response : any = await got
164+ const response : any = await this . gotInstance
157165 . post ( this . options . uriConnect || '' , {
158166 form : {
159167 scope,
@@ -163,10 +171,6 @@ export class FuusorApiClient {
163171 username : this . options . username ,
164172 password : this . options . password ,
165173 filetype : 'JsonTransformer'
166- } ,
167-
168- agent : {
169- https : this . httpsAgent
170174 }
171175 } )
172176 . json ( ) ;
@@ -195,11 +199,7 @@ export class FuusorApiClient {
195199 timeout : this . options . timeout ,
196200 headers : await this . getDefaultHttpHeaders ( scope ) ,
197201 responseType : 'json' ,
198- throwHttpErrors : false ,
199-
200- agent : {
201- https : this . httpsAgent
202- }
202+ throwHttpErrors : false
203203 } ;
204204
205205 // If json body is defined
@@ -212,7 +212,7 @@ export class FuusorApiClient {
212212 gotOptions . searchParams = params ;
213213 }
214214
215- const response = await got ( { ...gotOptions } ) ;
215+ const response = await this . gotInstance ( { ...gotOptions } ) ;
216216
217217 if ( response . statusCode !== 200 ) {
218218 throw new Error ( `Fuusor HTTP error ${ response . statusCode } (${ response . statusMessage } ): ${ response . body } ` ) ;
0 commit comments