Skip to content

Commit

Permalink
Updated dnsCache and https keep alive options (#5)
Browse files Browse the repository at this point in the history
* Updated dnsCache and https keep alive options

* Merge branch 'main' into AB#1019

* v3.1.0
  • Loading branch information
markus-peltola authored Jun 19, 2024
1 parent c773507 commit 9104ad6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 56 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ const fuusorApiClient = new FuusorApiClient({
password: 'environment-password-from-fuusor',

// Optional: change timeout
timeout: 60000
}
timeout: 60000,

// Optional: keep alive agent
keepAliveAgent: true,

// Optional: DNS cache
dnsCache: true,
})
```

### Changing timeout on the fly
Expand Down Expand Up @@ -457,3 +463,4 @@ await fuusorApiClient.userGroups.removeUsers('3fa85f64-5717-4562-b3fc-2c963f66af
- 1.1.3 Better logging in case of invalid hierarchy items or dimension items
- 1.2.0 pushDimensionFieldDimension option to allow empty dimensions
- 2.0.0 Add internal httpsAgent (from agentkeepalive package) by default
- 3.0.0 Added DNS cahcing support and updated httpsAgent to support options
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rantalainen/fuusor-api-client",
"version": "2.1.0",
"version": "3.1.0",
"description": "Fuusor API client library",
"main": "dist/index.js",
"scripts": {
Expand Down
96 changes: 48 additions & 48 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import got, { Headers, Method, OptionsOfJSONResponseBody } from 'got';
import got, { Got, Headers, Method, OptionsOfJSONResponseBody } from 'got';
import { FuusorDataSet, IFuusorDataSetOptions } from './data-set';
import { FuusorUser } from './user';
import { FuusorUserGroup } from './user-group';
import { HttpsAgent } from 'agentkeepalive';
import * as https from 'https';
import CacheableLookup from 'cacheable-lookup';

// Create global https agent
const httpsAgent = new HttpsAgent();

export interface IFuusorApiClientOptions {
Expand All @@ -20,11 +23,20 @@ export interface IFuusorApiClientOptions {

/** Request timeout, defaults to 120000 (120 secs) */
timeout?: number;

/** Instance of `https.Agent` or `true` to enable internal Keep Alive Agent, defaults to `true` */
keepAliveAgent?: boolean | https.Agent;

/** Instance of `cacheable-lookup` or `true` to enable internal DNS cache, defaults to `true` */
dnsCache?: boolean | CacheableLookup;
}

export class FuusorApiClient {
options: IFuusorApiClientOptions;

/** Got instance to be used when making requests */
gotInstance: Got;

readonly users: FuusorUser;
readonly userGroups: FuusorUserGroup;

Expand All @@ -34,36 +46,42 @@ export class FuusorApiClient {
/** @private */
accessTokensTimeout: any;

/** @private */
httpsAgent: HttpsAgent = httpsAgent;

constructor(options: IFuusorApiClientOptions) {
// Set default connect URI
options.uriConnect = options.uriConnect || 'https://api.fuusor.fi/connect/token';
options.uriBase = options.uriBase || 'https://api.fuusor.fi/api/v1';
options.uriUploadFile = options.uriUploadFile || `${options.uriBase}/uploadfile`;
options.uriDataset = options.uriDataset || `${options.uriBase}/dataset`;

// Set default timeout
options.timeout = options.timeout || 120000;

if (!options.clientId) {
throw new Error('Missing options.clientId');
}

if (!options.clientSecret) {
throw new Error('Missing options.clientSecret');
this.options = options || {};

// Check that needed options are included
if (!this.options.clientId) throw new Error('Missing options.clientId');
if (!this.options.clientSecret) throw new Error('Missing options.clientSecret');
if (!this.options.username) throw new Error('Missing options.username');
if (!this.options.password) throw new Error('Missing options.password');

// Set default connect URIs if none was provided
if (!this.options.uriConnect) this.options.uriConnect = 'https://api.fuusor.fi/connect/token';
if (!this.options.uriBase) this.options.uriBase = 'https://api.fuusor.fi/api/v1';
if (!this.options.uriUploadFile) this.options.uriUploadFile = `${this.options.uriBase}/uploadfile`;
if (!this.options.uriDataset) this.options.uriDataset = `${this.options.uriBase}/dataset`;

// Set default timeout if none was provided
if (!this.options.timeout) this.options.timeout = 120000;

// Use internal keepAliveAgent by default
if (this.options.keepAliveAgent === true || this.options.keepAliveAgent === undefined) {
this.options.keepAliveAgent = httpsAgent;
}

if (!options.username) {
throw new Error('Missing options.username');
// Use internal dnsCache by default (falls back to got's dnsCache)
if (this.options.dnsCache === true || this.options.dnsCache === undefined) {
this.options.dnsCache = true;
}

if (!options.password) {
throw new Error('Missing options.password');
}
// Set gotInstance defaults, can also include other options
this.gotInstance = got.extend({
// Agent options
agent: { https: this.options.keepAliveAgent || undefined },

this.options = options;
// DNS caching options
dnsCache: this.options.dnsCache || undefined
});

this.users = new FuusorUser(this);
this.userGroups = new FuusorUserGroup(this);
Expand Down Expand Up @@ -105,25 +123,19 @@ export class FuusorApiClient {
async saveDataSet(accessToken: string, data: any): Promise<void> {
const json = this._minimizeObjectKeys(data);

await got.post(this.options.uriDataset || '', {
await this.gotInstance.post(this.options.uriDataset || '', {
json,

timeout: this.options.timeout,

headers: {
Authorization: `Bearer ${accessToken}`
},

agent: {
https: this.httpsAgent
}
});

return;
}

async fetchAccessTokenForDataSetUpload(): Promise<string> {
const { access_token } = (await got
const { access_token } = (await this.gotInstance
.post(this.options.uriConnect || '', {
form: {
scope: 'fileupload',
Expand All @@ -133,10 +145,6 @@ export class FuusorApiClient {
username: this.options.username,
password: this.options.password,
filetype: 'JsonTransformer'
},

agent: {
https: this.httpsAgent
}
})
.json()) as any;
Expand All @@ -153,7 +161,7 @@ export class FuusorApiClient {
async refreshAccessToken(scope: string): Promise<void> {
// Check if access token is expired
if (!this.accessTokens?.[scope]) {
const response: any = await got
const response: any = await this.gotInstance
.post(this.options.uriConnect || '', {
form: {
scope,
Expand All @@ -163,10 +171,6 @@ export class FuusorApiClient {
username: this.options.username,
password: this.options.password,
filetype: 'JsonTransformer'
},

agent: {
https: this.httpsAgent
}
})
.json();
Expand Down Expand Up @@ -195,11 +199,7 @@ export class FuusorApiClient {
timeout: this.options.timeout,
headers: await this.getDefaultHttpHeaders(scope),
responseType: 'json',
throwHttpErrors: false,

agent: {
https: this.httpsAgent
}
throwHttpErrors: false
};

// If json body is defined
Expand All @@ -212,7 +212,7 @@ export class FuusorApiClient {
gotOptions.searchParams = params;
}

const response = await got({ ...gotOptions });
const response = await this.gotInstance({ ...gotOptions });

if (response.statusCode !== 200) {
throw new Error(`Fuusor HTTP error ${response.statusCode} (${response.statusMessage}): ${response.body}`);
Expand Down

0 comments on commit 9104ad6

Please sign in to comment.