Skip to content

Commit 11d9ed8

Browse files
authored
Merge pull request #1 from rantalainen/AB#1016
Updated dnsCache and https keep alive options
2 parents e5f4fbd + f29701c commit 11d9ed8

File tree

5 files changed

+93
-95
lines changed

5 files changed

+93
-95
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ const doks = new DoksApiClient({
4343
apiVersion: 'v1.89', // lock api version
4444
apiBaseUrl: 'https://url', // change base url, defaults to https://data.doks.fi/api
4545

46-
// optional - set to true if you want to use internal cache within Doks API Client
47-
dnsCache: dnsCache
46+
// Optional: keep alive agent
47+
keepAliveAgent: true,
48+
49+
// Optional: DNS cache
50+
dnsCache: true,
4851
});
4952
```
5053

@@ -151,3 +154,4 @@ const validBusinessId = doks.isValidBusinessId('1234567-8'); // true or false
151154
- 1.3.0 Added buyResponsiblePersonsByCustomerId and buyTradeRegisterByCustomerId
152155
- 1.3.4 Instantiate HTTPS KeepAliveAgent outside the class to get the most out of it
153156
- 1.4.0 Add dns caching possibility with cacheableLookup
157+
- 2.0.0 Added httpsAgent keep alive support and updated DNS caching options

package-lock.json

Lines changed: 49 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rantalainen/doks-api-client",
3-
"version": "1.5.0",
3+
"version": "2.1.0",
44
"description": "Doks third party API client",
55
"main": "dist/index.js",
66
"scripts": {
@@ -23,7 +23,7 @@
2323
"dependencies": {
2424
"agentkeepalive": "^4.1.4",
2525
"cacheable-lookup": "^5.0.4",
26-
"got": "^11.8.2"
26+
"got": "^11.8.6"
2727
},
2828
"files": [
2929
"dist/**/*"

src/index.ts

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import got, { Headers, Method, OptionsOfJSONResponseBody } from 'got';
1+
import got, { Got, Headers, Method, OptionsOfJSONResponseBody } from 'got';
22
import {
33
IDoksActualBeneficiaryDocumentWithMetadata,
44
IDoksApiClientOptions,
@@ -19,16 +19,15 @@ import {
1919

2020
import { HttpsAgent } from 'agentkeepalive';
2121
import * as validators from './helpers/validators';
22-
import CacheableLookup from 'cacheable-lookup';
2322

23+
// Create global https agent and cacheable lookup
2424
const httpsAgent = new HttpsAgent();
25-
const cacheableLookup = new CacheableLookup();
2625

2726
export 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

Comments
 (0)