Skip to content

Commit e57716f

Browse files
authored
feat: Allow specifying the user agent per-sdk implementation. (#226)
1 parent 01f48e9 commit e57716f

File tree

8 files changed

+23
-3
lines changed

8 files changed

+23
-3
lines changed

packages/sdk/cloudflare/src/createPlatformInfo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class CloudflarePlatformInfo implements Info {
1313
return {
1414
name,
1515
version,
16+
userAgentBase: 'CloudflareEdgeSDK',
1617
};
1718
}
1819
}

packages/sdk/server-node/src/platform/NodeInfo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export default class NodeInfo implements platform.Info {
3737
return {
3838
name: packageJson.name,
3939
version: packageJson.version,
40+
userAgentBase: 'NodeJSClient',
4041
// No wrapper name/version at the moment.
4142
};
4243
}

packages/sdk/vercel/src/createPlatformInfo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class VercelPlatformInfo implements Info {
1111
return {
1212
name: '@launchdarkly/vercel-server-sdk',
1313
version: '__LD_VERSION__',
14+
userAgentBase: 'VercelEdgeSDK',
1415
};
1516
}
1617
}

packages/shared/akamai-edgeworker-sdk/src/__tests__/platform/info/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('Akamai Platform Info', () => {
2020
expect(platformData.sdkData()).toEqual({
2121
name: packageJson.name,
2222
version: packageJson.version,
23+
userAgentBase: 'AkamaiEdgeSDK',
2324
});
2425
});
2526
});

packages/shared/akamai-edgeworker-sdk/src/platform/info/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class AkamaiPlatformInfo implements Info {
1717
return {
1818
name: this.sdkName,
1919
version: this.sdkVersion,
20+
userAgentBase: 'AkamaiEdgeSDK',
2021
};
2122
}
2223
}

packages/shared/common/src/api/platform/Info.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ export interface SdkData {
4444
*/
4545
version?: string;
4646

47+
/**
48+
* If this is a top-level (not a wrapper) SDK this will be used to create the user agent string.
49+
* It will take the form 'userAgentBase/version`.
50+
*/
51+
userAgentBase?: string;
52+
4753
/**
4854
* Name of the wrapper SDK if present.
4955
*/

packages/shared/sdk-server/__tests__/data_sources/defaultHeaders.test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import { Info, PlatformData, SdkData } from '@launchdarkly/js-sdk-common';
33
import defaultHeaders from '../../src/data_sources/defaultHeaders';
44
import Configuration from '../../src/options/Configuration';
55

6-
const makeInfo = (wrapperName?: string, wrapperVersion?: string): Info => ({
6+
const makeInfo = (wrapperName?: string, wrapperVersion?: string, userAgentBase?: string): Info => ({
77
platformData(): PlatformData {
88
return {};
99
},
1010
sdkData(): SdkData {
1111
const sdkData: SdkData = {
1212
version: '2.2.2',
13+
userAgentBase,
1314
wrapperName,
1415
wrapperVersion,
1516
};
@@ -23,12 +24,18 @@ it('sets SDK key', () => {
2324
expect(h).toMatchObject({ authorization: 'my-sdk-key' });
2425
});
2526

26-
it('sets user agent', () => {
27+
it('sets the default user agent', () => {
2728
const config = new Configuration({});
2829
const h = defaultHeaders('my-sdk-key', config, makeInfo());
2930
expect(h).toMatchObject({ 'user-agent': 'NodeJSClient/2.2.2' });
3031
});
3132

33+
it('sets the SDK specific user agent', () => {
34+
const config = new Configuration({});
35+
const h = defaultHeaders('my-sdk-key', config, makeInfo(undefined, undefined, 'CATS'));
36+
expect(h).toMatchObject({ 'user-agent': 'CATS/2.2.2' });
37+
});
38+
3239
it('does not include wrapper header by default', () => {
3340
const config = new Configuration({});
3441
const h = defaultHeaders('my-sdk-key', config, makeInfo());

packages/shared/sdk-server/src/data_sources/defaultHeaders.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export default function defaultHeaders(
1212
const sdkData = info.sdkData();
1313
const headers: { [key: string]: string } = {
1414
authorization: sdkKey,
15-
'user-agent': `NodeJSClient/${sdkData.version}`,
15+
'user-agent': `${sdkData.userAgentBase ? sdkData.userAgentBase : 'NodeJSClient'}/${
16+
sdkData.version
17+
}`,
1618
};
1719

1820
if (sdkData.wrapperName) {

0 commit comments

Comments
 (0)