Skip to content

Commit 4232150

Browse files
authored
fix: add extra user agent fields resolvable by react native (#1477)
## CLA - [ ] I have signed the [Stream CLA](https://docs.google.com/forms/d/e/1FAIpQLScFKsKkAJI7mhCr7K9rEIOpqIDThrWxuvxnwUq2XkHyG154vQ/viewform) (required). - [ ] Code changes are tested ## Description of the changes, What, Why and How? ## Changelog -
1 parent 48d2fff commit 4232150

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

src/client.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import {
7979
DeleteCommandResponse,
8080
DeleteUserOptions,
8181
Device,
82+
DeviceIdentifier,
8283
EndpointName,
8384
ErrorFromResponse,
8485
Event,
@@ -277,6 +278,7 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
277278
defaultWSTimeoutWithFallback: number;
278279
defaultWSTimeout: number;
279280
sdkIdentifier?: SdkIdentifier;
281+
deviceIdentifier?: DeviceIdentifier;
280282
private nextRequestAbortController: AbortController | null = null;
281283

282284
/**
@@ -2922,12 +2924,31 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
29222924
if (this.userAgent) {
29232925
return this.userAgent;
29242926
}
2927+
29252928
const version = process.env.PKG_VERSION;
2929+
const clientBundle = process.env.CLIENT_BUNDLE;
2930+
2931+
let userAgentString = '';
29262932
if (this.sdkIdentifier) {
2927-
return `stream-chat-${this.sdkIdentifier.name}-v${this.sdkIdentifier.version}-llc-v${version}`;
2933+
userAgentString = `stream-chat-${this.sdkIdentifier.name}-v${this.sdkIdentifier.version}-llc-v${version}`;
29282934
} else {
2929-
return `stream-chat-js-v${version}-${this.node ? 'node' : 'browser'}`;
2930-
}
2935+
userAgentString = `stream-chat-js-v${version}-${this.node ? 'node' : 'browser'}`;
2936+
}
2937+
2938+
const { os, model } = this.deviceIdentifier ?? {};
2939+
2940+
return ([
2941+
// reports the device OS, if provided
2942+
['os', os],
2943+
// reports the device model, if provided
2944+
['device_model', model],
2945+
// reports which bundle is being picked from the exports
2946+
['client_bundle', clientBundle],
2947+
] as const).reduce(
2948+
(withArguments, [key, value]) =>
2949+
value && value.length > 0 ? withArguments.concat(`|${key}=${value}`) : withArguments,
2950+
userAgentString,
2951+
);
29312952
}
29322953

29332954
/**

src/types.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -3816,4 +3816,14 @@ export type PromoteChannelParams<SCG extends ExtendableGenerics = DefaultGeneric
38163816
channelToMoveIndexWithinChannels?: number;
38173817
};
38183818

3819-
export type SdkIdentifier = { name: 'react' | 'react-native' | 'angular'; version: string };
3819+
/**
3820+
* An identifier containing information about the downstream SDK using stream-chat. It
3821+
* is used to resolve the user agent.
3822+
*/
3823+
export type SdkIdentifier = { name: 'react' | 'react-native' | 'expo' | 'angular'; version: string };
3824+
3825+
/**
3826+
* An identifier containing information about the downstream device using stream-chat, if
3827+
* available. Is used by the react-native SDKs to enrich the user agent further.
3828+
*/
3829+
export type DeviceIdentifier = { os: string; model?: string };

test/unit/client.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -663,13 +663,29 @@ describe('X-Stream-Client header', () => {
663663
expect(userAgent).to.be.equal('stream-chat-js-v1.2.3-browser');
664664
});
665665

666-
it('SDK integration', () => {
666+
it('SDK integration without deviceIdentifier', () => {
667667
client.sdkIdentifier = { name: 'react', version: '2.3.4' };
668668
const userAgent = client.getUserAgent();
669669

670670
expect(userAgent).to.be.equal('stream-chat-react-v2.3.4-llc-v1.2.3');
671671
});
672672

673+
it('SDK integration with deviceIdentifier', () => {
674+
client.sdkIdentifier = { name: 'react-native', version: '2.3.4' };
675+
client.deviceIdentifier = { os: 'iOS 15.0', model: 'iPhone17,4' };
676+
const userAgent = client.getUserAgent();
677+
678+
expect(userAgent).to.be.equal('stream-chat-react-native-v2.3.4-llc-v1.2.3|os=iOS 15.0|device_model=iPhone17,4');
679+
});
680+
681+
it('SDK integration with process.env.CLIENT_BUNDLE', () => {
682+
process.env.CLIENT_BUNDLE = 'browser';
683+
client.sdkIdentifier = { name: 'react', version: '2.3.4' };
684+
const userAgent = client.getUserAgent();
685+
686+
expect(userAgent).to.be.equal('stream-chat-react-v2.3.4-llc-v1.2.3|client_bundle=browser');
687+
});
688+
673689
it('setUserAgent is now deprecated', () => {
674690
client.setUserAgent('deprecated');
675691
const userAgent = client.getUserAgent();

0 commit comments

Comments
 (0)