Skip to content

Commit

Permalink
feat: RequestClient updated to become more customizable (#778)
Browse files Browse the repository at this point in the history
* [DI-2179] RequestClient updated to become more extensible

* [DI-2179] Fixing .d.ts types for RequestClientOptions
  • Loading branch information
mattcole19 authored Aug 11, 2022
1 parent f5adf1c commit c7d5f33
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
28 changes: 26 additions & 2 deletions lib/base/RequestClient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ declare class RequestClient {
constructor(opts?: RequestClient.RequestClientOptions);
/**
* Make an HTTP request
* @param opts The request options
* @param opts The options https.Agent takes in
*/
request<TData>(
opts: RequestClient.RequestOptions<TData>
Expand Down Expand Up @@ -59,9 +59,33 @@ declare namespace RequestClient {
export interface RequestClientOptions {
/**
* A timeout in milliseconds. This will be used as the HTTPS agent's socket
* timeout, and as the default request timeout.
* timeout, AND as the default request timeout.
*/
timeout?: number;
/**
* https.Agent keepAlive option
*/
keepAlive?: boolean;
/**
* https.Agent keepAliveMSecs option
*/
keepAliveMsecs?: number;
/**
* https.Agent maxSockets option
*/
maxSockets?: number;
/**
* https.Agent maxTotalSockets option
*/
maxTotalSockets?: number;
/**
* https.Agent maxFreeSockets option
*/
maxFreeSockets?: number;
/**
* https.Agent scheduling option
*/
scheduling?: string;
}

export interface Headers {
Expand Down
16 changes: 14 additions & 2 deletions lib/base/RequestClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ const DEFAULT_TIMEOUT = 30000;

/**
* Make http request
* @param {object} opts - The options argument
* @param {string} opts.timeout - A custom timeout to use. This will be used as the socket timeout, and as the default request timeout.
* @param {object} opts - The options passed to https.Agent
* @param {number} opts.timeout - https.Agent timeout option. Used as the socket timeout, AND as the default request timeout.
* @param {boolean} opts.keepAlive - https.Agent keepAlive option
* @param {number} opts.keepAliveMsecs - https.Agent keepAliveMsecs option
* @param {number} opts.maxSockets - https.Agent maxSockets option
* @param {number} opts.maxTotalSockets - https.Agent maxTotalSockets option
* @param {number} opts.maxFreeSockets - https.Agent maxFreeSockets option
* @param {string} opts.scheduling - https.Agent scheduling option
*/
var RequestClient = function (opts) {
opts = opts || {};
Expand All @@ -26,6 +32,12 @@ var RequestClient = function (opts) {
// construct an https agent
let agentOpts = {
timeout: this.defaultTimeout,
keepAlive: opts.keepAlive,
keepAliveMsecs: opts.keepAliveMsecs,
maxSockets: opts.maxSockets,
maxTotalSockets: opts.maxTotalSockets,
maxFreeSockets: opts.maxFreeSockets,
scheduling: opts.scheduling,
};

let agent;
Expand Down
32 changes: 30 additions & 2 deletions spec/unit/base/RequestClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ describe('RequestClient constructor', function() {
});
expect(requestClient.axios.defaults.httpsAgent).not.toBeInstanceOf(HttpsProxyAgent);
expect(requestClient.axios.defaults.httpsAgent.options.timeout).toEqual(30000);
expect(requestClient.axios.defaults.httpsAgent.options.keepAlive).toBe(undefined);
expect(requestClient.axios.defaults.httpsAgent.options.keepAliveMsecs).toBe(undefined);
expect(requestClient.axios.defaults.httpsAgent.options.maxSockets).toBe(undefined);
expect(requestClient.axios.defaults.httpsAgent.options.maxTotalSockets).toBe(undefined);
expect(requestClient.axios.defaults.httpsAgent.options.maxFreeSockets).toBe(undefined);
expect(requestClient.axios.defaults.httpsAgent.options.scheduling).toBe(undefined);
});

it('should initialize with a proxy', function() {
Expand All @@ -67,14 +73,36 @@ describe('RequestClient constructor', function() {
expect(requestClient.axios.defaults.httpsAgent.proxy.host).toEqual('example.com');
});

it('should initialize with a timeout', function() {
const requestClient = new RequestClientMock({ timeout: 5000 });
it('should initialize custom https settings (all settings customized)', function() {
const requestClient = new RequestClientMock({ timeout: 5000, keepAlive: true, keepAliveMsecs: 1500, maxSockets: 100, maxTotalSockets: 1000, maxFreeSockets: 10, scheduling: 'fifo'});
expect(requestClient.defaultTimeout).toEqual(5000);
expect(requestClient.axios.defaults.headers.post).toEqual({
'Content-Type': 'application/x-www-form-urlencoded',
});
expect(requestClient.axios.defaults.httpsAgent).not.toBeInstanceOf(HttpsProxyAgent);
expect(requestClient.axios.defaults.httpsAgent.options.timeout).toEqual(5000);
expect(requestClient.axios.defaults.httpsAgent.options.keepAlive).toBe(true);
expect(requestClient.axios.defaults.httpsAgent.options.keepAliveMsecs).toEqual(1500);
expect(requestClient.axios.defaults.httpsAgent.options.maxSockets).toEqual(100);
expect(requestClient.axios.defaults.httpsAgent.options.maxTotalSockets).toEqual(1000);
expect(requestClient.axios.defaults.httpsAgent.options.maxFreeSockets).toEqual(10);
expect(requestClient.axios.defaults.httpsAgent.options.scheduling).toEqual('fifo');
});

it('should initialize custom https settings (some settings customized)', function() {
const requestClient = new RequestClientMock({ timeout: 5000, keepAlive: false, maxTotalSockets: 1500, scheduling: 'lifo'});
expect(requestClient.defaultTimeout).toEqual(5000);
expect(requestClient.axios.defaults.headers.post).toEqual({
'Content-Type': 'application/x-www-form-urlencoded',
});
expect(requestClient.axios.defaults.httpsAgent).not.toBeInstanceOf(HttpsProxyAgent);
expect(requestClient.axios.defaults.httpsAgent.options.timeout).toEqual(5000);
expect(requestClient.axios.defaults.httpsAgent.options.keepAlive).toBe(false);
expect(requestClient.axios.defaults.httpsAgent.options.keepAliveMsecs).toBe(undefined);
expect(requestClient.axios.defaults.httpsAgent.options.maxSockets).toBe(undefined);
expect(requestClient.axios.defaults.httpsAgent.options.maxTotalSockets).toEqual(1500);
expect(requestClient.axios.defaults.httpsAgent.options.maxFreeSockets).toBe(undefined);
expect(requestClient.axios.defaults.httpsAgent.options.scheduling).toEqual('lifo');
});
});

Expand Down

0 comments on commit c7d5f33

Please sign in to comment.