Skip to content

Commit 9104ad6

Browse files
Updated dnsCache and https keep alive options (#5)
* Updated dnsCache and https keep alive options * Merge branch 'main' into AB#1019 * v3.1.0
1 parent c773507 commit 9104ad6

File tree

4 files changed

+63
-56
lines changed

4 files changed

+63
-56
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ const fuusorApiClient = new FuusorApiClient({
3838
password: 'environment-password-from-fuusor',
3939

4040
// Optional: change timeout
41-
timeout: 60000
42-
}
41+
timeout: 60000,
42+
43+
// Optional: keep alive agent
44+
keepAliveAgent: true,
45+
46+
// Optional: DNS cache
47+
dnsCache: true,
48+
})
4349
```
4450

4551
### Changing timeout on the fly
@@ -457,3 +463,4 @@ await fuusorApiClient.userGroups.removeUsers('3fa85f64-5717-4562-b3fc-2c963f66af
457463
- 1.1.3 Better logging in case of invalid hierarchy items or dimension items
458464
- 1.2.0 pushDimensionFieldDimension option to allow empty dimensions
459465
- 2.0.0 Add internal httpsAgent (from agentkeepalive package) by default
466+
- 3.0.0 Added DNS cahcing support and updated httpsAgent to support options

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rantalainen/fuusor-api-client",
3-
"version": "2.1.0",
3+
"version": "3.1.0",
44
"description": "Fuusor API client library",
55
"main": "dist/index.js",
66
"scripts": {

src/index.ts

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import got, { Headers, Method, OptionsOfJSONResponseBody } from 'got';
1+
import got, { Got, Headers, Method, OptionsOfJSONResponseBody } from 'got';
22
import { FuusorDataSet, IFuusorDataSetOptions } from './data-set';
33
import { FuusorUser } from './user';
44
import { FuusorUserGroup } from './user-group';
55
import { HttpsAgent } from 'agentkeepalive';
6+
import * as https from 'https';
7+
import CacheableLookup from 'cacheable-lookup';
68

9+
// Create global https agent
710
const httpsAgent = new HttpsAgent();
811

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

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

Comments
 (0)