Skip to content

Commit c23cf73

Browse files
authored
fix(types): Allow null metricsServer and version (#286)
null can be nicer as an explicit "this is not set".
1 parent d26b111 commit c23cf73

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/AppConfig.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ export interface AppConfig {
1313
/**
1414
* Version of the application
1515
*
16-
* This is used to tag custom metrics. It will typically be a CI build
17-
* number.
16+
* This is used to tag custom metrics. It will typically include a CI build
17+
* number and/or a commit hash.
1818
*/
19-
version?: string;
19+
version?: string | null | undefined;
2020

2121
/**
2222
* Environment of the application

src/createStatsDClient.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,16 @@ describe('createStatsDClient', () => {
1212
}),
1313
).toBeInstanceOf(Object);
1414
});
15+
16+
it('should handle null config values', () => {
17+
expect(
18+
createStatsDClient(StatsD, {
19+
name: 'test',
20+
environment: 'jest',
21+
version: null,
22+
23+
metricsServer: null,
24+
}),
25+
).toBeInstanceOf(Object);
26+
});
1527
});

src/createStatsDClient.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface StatsDConfig extends AppConfig {
2626
* For Gantry this is `localhost`. If this is not specified then a mock
2727
* client will be constructed.
2828
*/
29-
metricsServer?: string;
29+
metricsServer?: string | null | undefined;
3030
}
3131

3232
/**
@@ -44,10 +44,13 @@ export const createStatsDClient = <T extends InternalStatsD>(
4444
config: StatsDConfig,
4545
errorHandler?: (err: Error) => void,
4646
): T => {
47+
// istanbul ignore next: Jest is not picking up coalesce coverage
48+
const host = config.metricsServer ?? undefined;
49+
4750
const client = new StatsD({
4851
// Disable ourselves if there's no configured metrics server
4952
mock: !config.metricsServer,
50-
host: config.metricsServer,
53+
host,
5154
errorHandler,
5255

5356
prefix: `${config.name}.`,

0 commit comments

Comments
 (0)