diff --git a/packages/session-replay-browser/src/config/joined-config.ts b/packages/session-replay-browser/src/config/joined-config.ts index 0c878489d..19554d66b 100644 --- a/packages/session-replay-browser/src/config/joined-config.ts +++ b/packages/session-replay-browser/src/config/joined-config.ts @@ -1,4 +1,6 @@ import { RemoteConfigFetch, createRemoteConfigFetch } from '@amplitude/analytics-remote-config'; +import { Logger } from '@amplitude/analytics-types'; +import { getDebugConfig } from '../helpers'; import { SessionReplayOptions } from '../typings/session-replay'; import { SessionReplayLocalConfig } from './local-config'; import { @@ -7,8 +9,6 @@ import { SessionReplayJoinedConfig, SessionReplayRemoteConfig, } from './types'; -import { getDebugConfig } from '../helpers'; -import { Logger } from '@amplitude/analytics-types'; export const removeInvalidSelectorsFromPrivacyConfig = (privacyConfig: PrivacyConfig, loggerProvider: Logger) => { // This allows us to not search the DOM. @@ -38,18 +38,12 @@ export const removeInvalidSelectorsFromPrivacyConfig = (privacyConfig: PrivacyCo return privacyConfig; }; export class SessionReplayJoinedConfigGenerator { - localConfig: ISessionReplayLocalConfig; - remoteConfigFetch: RemoteConfigFetch | undefined; + private readonly localConfig: ISessionReplayLocalConfig; + private readonly remoteConfigFetch: RemoteConfigFetch; - constructor(apiKey: string, options: SessionReplayOptions) { - this.localConfig = new SessionReplayLocalConfig(apiKey, options); - } - - async initialize() { - this.remoteConfigFetch = await createRemoteConfigFetch({ - localConfig: this.localConfig, - configKeys: ['sessionReplay'], - }); + constructor(remoteConfigFetch: RemoteConfigFetch, localConfig: ISessionReplayLocalConfig) { + this.localConfig = localConfig; + this.remoteConfigFetch = remoteConfigFetch; } async generateJoinedConfig(sessionId?: number): Promise { @@ -61,11 +55,6 @@ export class SessionReplayJoinedConfigGenerator { config.captureEnabled = true; let remoteConfig: SessionReplayRemoteConfig | undefined; try { - if (!this.remoteConfigFetch) { - config.captureEnabled = false; - return config; - } - const samplingConfig = await this.remoteConfigFetch.getRemoteConfig( 'sessionReplay', 'sr_sampling_config', @@ -197,7 +186,11 @@ export class SessionReplayJoinedConfigGenerator { } export const createSessionReplayJoinedConfigGenerator = async (apiKey: string, options: SessionReplayOptions) => { - const joinedConfigGenerator = new SessionReplayJoinedConfigGenerator(apiKey, options); - await joinedConfigGenerator.initialize(); - return joinedConfigGenerator; + const localConfig = new SessionReplayLocalConfig(apiKey, options); + const remoteConfigFetch = await createRemoteConfigFetch({ + localConfig, + configKeys: ['sessionReplay'], + }); + + return new SessionReplayJoinedConfigGenerator(remoteConfigFetch, localConfig); }; diff --git a/packages/session-replay-browser/test/config/joined-config.test.ts b/packages/session-replay-browser/test/config/joined-config.test.ts index d3a4f8738..d022fd5c7 100644 --- a/packages/session-replay-browser/test/config/joined-config.test.ts +++ b/packages/session-replay-browser/test/config/joined-config.test.ts @@ -8,6 +8,7 @@ import { } from '../../src/config/joined-config'; import { SessionReplayLocalConfig } from '../../src/config/local-config'; import { PrivacyConfig, SessionReplayRemoteConfig } from '../../src/config/types'; +import { createRemoteConfigFetch } from '@amplitude/analytics-remote-config'; type MockedLogger = jest.Mocked; const samplingConfig = { @@ -141,17 +142,6 @@ describe('SessionReplayJoinedConfigGenerator', () => { }); }); }); - describe('without first initializing', () => { - test('should set captureEnabled to true', async () => { - joinedConfigGenerator = new SessionReplayJoinedConfigGenerator('static_key', mockOptions); - const config = await joinedConfigGenerator.generateJoinedConfig(123); - expect(config).toEqual({ - ...mockLocalConfig, - optOut: mockLocalConfig.optOut, - captureEnabled: false, - }); - }); - }); describe('with successful privacy config fetch', () => { const privacySelectorTest = async ( remotePrivacyConfig?: PrivacyConfig, @@ -162,10 +152,12 @@ describe('SessionReplayJoinedConfigGenerator', () => { const result = key === 'sr_privacy_config' ? remotePrivacyConfig : undefined; return Promise.resolve(result); }); - if (configGenerator.remoteConfigFetch) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - configGenerator.remoteConfigFetch.getRemoteConfig = getRemoteConfigMock; - } + const remoteConfigFetch = await createRemoteConfigFetch({ + localConfig: mockLocalConfig, + configKeys: ['sessionReplay'], + }); + remoteConfigFetch.getRemoteConfig = getRemoteConfigMock; + new SessionReplayJoinedConfigGenerator(remoteConfigFetch, mockLocalConfig); return configGenerator.generateJoinedConfig(123); }; @@ -263,15 +255,13 @@ describe('SessionReplayJoinedConfigGenerator', () => { }); test('should update to remote config when local privacy config is undefined', async () => { - const configGenerator = await createSessionReplayJoinedConfigGenerator('static_key', { - ...mockOptions, - privacyConfig: undefined, + const mockRemoteConfigFetch = await createRemoteConfigFetch({ + localConfig: mockLocalConfig, + configKeys: ['sessionReplay'], }); getRemoteConfigMockImplementation({ privacyConfig }); - if (configGenerator.remoteConfigFetch) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - configGenerator.remoteConfigFetch.getRemoteConfig = getRemoteConfigMock; - } + mockRemoteConfigFetch.getRemoteConfig = getRemoteConfigMock; + const configGenerator = new SessionReplayJoinedConfigGenerator(mockRemoteConfigFetch, mockLocalConfig); const config = await configGenerator.generateJoinedConfig(123); expect(config).toEqual({ ...mockLocalConfig,