Skip to content

Commit

Permalink
Allow user to add httpOptions in the config (#81)
Browse files Browse the repository at this point in the history
* add httpOptions

* format fixes from npm run fmt

* improvements

* Update src/test/config.test.ts

Co-authored-by: Thomas Heartman <[email protected]>

* Update src/test/config.test.ts

Co-authored-by: Thomas Heartman <[email protected]>

* Removed underscore

Co-authored-by: Thomas Heartman <[email protected]>
  • Loading branch information
CaptainGlac1er and thomasheartman authored Jul 21, 2022
1 parent 9c3ee97 commit c41df56
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ class Client extends EventEmitter implements IClient {
tags: config.tags,
customHeadersFunction,
bootstrap: config.bootstrap,
...(!!config.httpOptions
? { httpOptions: config.httpOptions }
: {}),
});

// Custom metrics Instance
Expand All @@ -94,6 +97,9 @@ class Client extends EventEmitter implements IClient {
metricsInterval: config.metricsInterval,
url: config.unleashUrl,
customHeadersFunction,
...(!!config.httpOptions
? { httpOptions: config.httpOptions }
: {}),
});

this.metrics.on('error', (msg) => this.logger.error(`metrics: ${msg}`));
Expand Down
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Strategy, TagFilter } from 'unleash-client';
import { BootstrapOptions } from 'unleash-client/lib/repository/bootstrap-provider';
import { Logger, LogLevel, SimpleLogger } from './logger';
import { generateInstanceId } from './util';
import { HttpOptions } from 'unleash-client/lib/http-options';

export interface ServerSideSdkConfig {
tokens: string[];
Expand Down Expand Up @@ -33,6 +34,7 @@ export interface IProxyOption {
// experimental options
expBootstrap?: BootstrapOptions;
expServerSideSdkConfig?: ServerSideSdkConfig;
httpOptions?: HttpOptions;
}

export interface IProxyConfig {
Expand All @@ -57,6 +59,7 @@ export interface IProxyConfig {
serverSideSdkConfig?: ServerSideSdkConfig;
bootstrap?: BootstrapOptions;
cors: CorsOptions;
httpOptions?: HttpOptions;
}

function resolveStringToArray(value?: string): string[] | undefined {
Expand Down Expand Up @@ -258,5 +261,6 @@ export function createProxyConfig(option: IProxyOption): IProxyConfig {
enableOAS:
option.enableOAS || safeBoolean(process.env.ENABLE_OAS, false),
cors: loadCorsOptions(option),
...(!!option.httpOptions ? { httpOptions: option.httpOptions } : {}),
};
}
24 changes: 24 additions & 0 deletions src/test/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from 'path';
import { Strategy } from 'unleash-client';
import { createProxyConfig } from '../config';
import * as https from 'https';

test('should require "unleashUrl', () => {
const t = () => createProxyConfig({});
Expand Down Expand Up @@ -337,3 +338,26 @@ test('should load cors origin, maxAge and exposedHeaders default values', () =>
expect(config.cors.maxAge).toBe(172800);
expect(config.cors.exposedHeaders).toBe('ETag');
});

test('should load the passed-in http agent when config.httpOptions is provided', () => {
const config = createProxyConfig({
unleashUrl: 'some',
unleashApiToken: 'some',
clientKeys: ['s1'],
httpOptions: {
agent: () => https.globalAgent,
},
});
expect(config.httpOptions?.agent?.(new URL('https://example.com'))).toBe(
https.globalAgent,
);
});

test('should not set config.httpOptions if no http options are provided at creation', () => {
const config = createProxyConfig({
unleashUrl: 'some',
unleashApiToken: 'some',
clientKeys: ['s1'],
});
expect(config.httpOptions).toBeUndefined();
});

0 comments on commit c41df56

Please sign in to comment.