Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: RequestClient updated to become more customizable #778

Merged
merged 3 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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