1- import got , { Headers , Method , OptionsOfJSONResponseBody } from 'got' ;
1+ import got , { Got , Headers , Method , OptionsOfJSONResponseBody } from 'got' ;
22import {
33 IDoksActualBeneficiaryDocumentWithMetadata ,
44 IDoksApiClientOptions ,
@@ -19,16 +19,15 @@ import {
1919
2020import { HttpsAgent } from 'agentkeepalive' ;
2121import * as validators from './helpers/validators' ;
22- import CacheableLookup from 'cacheable-lookup' ;
2322
23+ // Create global https agent and cacheable lookup
2424const httpsAgent = new HttpsAgent ( ) ;
25- const cacheableLookup = new CacheableLookup ( ) ;
2625
2726export class DoksApiClient {
2827 options ! : IDoksApiClientOptions ;
2928
30- /** @private */
31- keepAliveAgent : HttpsAgent = httpsAgent ;
29+ /** Got instance to be used when making requests */
30+ gotInstance : Got ;
3231
3332 /** @private */
3433 accessToken : string | undefined ;
@@ -39,26 +38,32 @@ export class DoksApiClient {
3938 constructor ( options : IDoksApiClientOptions ) {
4039 this . options = options ;
4140
42- if ( ! this . options . apikey ) {
43- throw new Error ( 'Missing options.apikey' ) ;
44- }
41+ // Check that needed options are included
42+ if ( ! this . options . apikey ) throw new Error ( 'Missing options.apikey' ) ;
43+ if ( ! this . options . email ) throw new Error ( 'Missing options.email' ) ;
4544
46- if ( ! this . options . email ) {
47- throw new Error ( 'Missing options.email' ) ;
48- }
45+ // Set default base url and api version if none was provided
46+ if ( ! this . options . apiBaseUrl ) this . options . apiBaseUrl = 'https://data.doks.fi/api' ;
47+ if ( ! this . options . apiVersion ) this . options . apiVersion = 'current' ;
4948
50- if ( ! this . options . apiBaseUrl ) {
51- this . options . apiBaseUrl = 'https://data.doks.fi/api' ;
49+ // Use internal keepAliveAgent by default
50+ if ( this . options . keepAliveAgent === true || this . options . keepAliveAgent === undefined ) {
51+ this . options . keepAliveAgent = httpsAgent ;
5252 }
5353
54- if ( ! this . options . apiVersion ) {
55- this . options . apiVersion = 'current' ;
54+ // Use internal dnsCache by default (falls back to got's dnsCache)
55+ if ( this . options . dnsCache === true || this . options . dnsCache === undefined ) {
56+ this . options . dnsCache = true ;
5657 }
5758
58- // If dnsCache is true, then fallback to internal instance of cacheableLookup
59- if ( this . options . dnsCache === true ) {
60- this . options . dnsCache = cacheableLookup ;
61- }
59+ // Set gotInstance defaults, can also include other options
60+ this . gotInstance = got . extend ( {
61+ // Agent options
62+ agent : { https : this . options . keepAliveAgent || undefined } ,
63+
64+ // DNS caching options
65+ dnsCache : this . options . dnsCache || undefined
66+ } ) ;
6267 }
6368
6469 /** @private */
@@ -70,17 +75,13 @@ export class DoksApiClient {
7075 async refreshAccessToken ( ) : Promise < void > {
7176 // If refreshing access token is necessary
7277 if ( ! this . accessToken ) {
73- const accessTokenResult : IDoksApiResponse = await got ( {
78+ const accessTokenResult : IDoksApiResponse = await this . gotInstance ( {
7479 url : `${ this . options . apiBaseUrl } /${ this . options . apiVersion } /user/auth` ,
7580 method : 'POST' ,
76- agent : { https : this . keepAliveAgent } ,
7781 json : {
7882 apikey : this . options . apikey ,
7983 email : this . options . email
8084 } ,
81-
82- dnsCache : this . options . dnsCache || undefined ,
83-
8485 resolveBodyOnly : true
8586 } ) . json ( ) ;
8687
@@ -109,17 +110,10 @@ export class DoksApiClient {
109110 async request ( method : Method , uri : string , json ?: any , params ?: any ) : Promise < any > {
110111 const gotOptions : OptionsOfJSONResponseBody = {
111112 url : this . constructUrl ( uri ) ,
112-
113113 headers : await this . getDefaultHttpHeaders ( ) ,
114- agent : { https : this . keepAliveAgent } ,
115-
116- dnsCache : this . options . dnsCache || undefined ,
117-
118114 responseType : 'json' ,
119115 throwHttpErrors : false ,
120-
121116 searchParams : params ,
122-
123117 method
124118 } ;
125119
@@ -128,7 +122,7 @@ export class DoksApiClient {
128122 gotOptions . json = json ;
129123 }
130124
131- const response : IDoksApiResponse = await got ( { ...gotOptions , resolveBodyOnly : true } ) ;
125+ const response : IDoksApiResponse = await this . gotInstance ( { ...gotOptions , resolveBodyOnly : true } ) ;
132126
133127 if ( ! response . status ) {
134128 return this . httpErrorHandler ( response ) ;
@@ -314,13 +308,11 @@ export class DoksApiClient {
314308 // Fetch short lived access token for PDF fetching
315309 await this . refreshAccessToken ( ) ;
316310
317- return await got ( {
311+ return await this . gotInstance ( {
318312 method : 'GET' ,
319313 url : this . constructUrl ( `user/customers/${ customerId } /pdf` ) ,
320314 searchParams : { jwt : this . accessToken , ...params } ,
321- resolveBodyOnly : true ,
322- dnsCache : this . options . dnsCache || undefined ,
323- agent : { https : this . keepAliveAgent }
315+ resolveBodyOnly : true
324316 } ) . buffer ( ) ;
325317 }
326318
@@ -335,13 +327,11 @@ export class DoksApiClient {
335327 ) : Promise < Buffer > {
336328 await this . refreshAccessToken ( ) ;
337329
338- return await got ( {
330+ return await this . gotInstance ( {
339331 method : 'GET' ,
340332 url : this . constructUrl ( `user/customers/${ customerId } /requests/${ informationRequestId } /pdf` ) ,
341333 searchParams : { jwt : this . accessToken , ...params } ,
342- resolveBodyOnly : true ,
343- dnsCache : this . options . dnsCache || undefined ,
344- agent : { https : this . keepAliveAgent }
334+ resolveBodyOnly : true
345335 } ) . buffer ( ) ;
346336 }
347337
0 commit comments