Skip to content

Commit 6ebd65e

Browse files
authored
fix: remove non-required parameters from native calls (#750)
1 parent 7981efa commit 6ebd65e

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

packages/core/src/__tests__/context.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,18 @@ describe('#getContext', () => {
9696
expect(AnalyticsReactNative.getContextInfo).toHaveBeenCalledTimes(1);
9797
expect(context).toEqual({ ...contextResult, traits: userTraits });
9898
});
99+
100+
it('strip non-required config from native calls', async () => {
101+
const { AnalyticsReactNative } = NativeModules;
102+
await getContext(undefined, {
103+
writeKey: 'notRequiredInNative',
104+
collectDeviceId: true,
105+
flushPolicies: [], // Shouldn't get to native as this is RN specific
106+
});
107+
108+
expect(AnalyticsReactNative.getContextInfo).toHaveBeenCalledTimes(1);
109+
expect(AnalyticsReactNative.getContextInfo).toHaveBeenCalledWith({
110+
collectDeviceId: true,
111+
});
112+
});
99113
});

packages/core/src/context.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { libraryInfo } from './info';
22

33
import type {
4+
Config,
45
Context,
56
ContextDevice,
67
NativeContextInfo,
@@ -10,7 +11,7 @@ import { getNativeModule } from './util';
1011
import { getUUID } from './uuid';
1112

1213
interface GetContextConfig {
13-
collectDeviceId?: boolean;
14+
collectDeviceId: boolean;
1415
}
1516

1617
const defaultContext = {
@@ -35,8 +36,13 @@ const defaultContext = {
3536

3637
export const getContext = async (
3738
userTraits: UserTraits = {},
38-
config: GetContextConfig = {}
39+
config?: Config
3940
): Promise<Context> => {
41+
// We need to remove all stuff from the config that is not actually required by the native module
42+
const nativeConfig: GetContextConfig = {
43+
collectDeviceId: config?.collectDeviceId ?? false,
44+
};
45+
4046
const {
4147
appName,
4248
appVersion,
@@ -56,8 +62,9 @@ export const getContext = async (
5662
deviceType,
5763
screenDensity,
5864
}: NativeContextInfo =
59-
(await getNativeModule('AnalyticsReactNative')?.getContextInfo(config)) ??
60-
defaultContext;
65+
(await getNativeModule('AnalyticsReactNative')?.getContextInfo(
66+
nativeConfig
67+
)) ?? defaultContext;
6168

6269
const device: ContextDevice = {
6370
id: deviceId,

0 commit comments

Comments
 (0)