diff --git a/packages/core/__mocks__/react-native.ts b/packages/core/__mocks__/react-native.ts index db10d7f31..22f771f99 100644 --- a/packages/core/__mocks__/react-native.ts +++ b/packages/core/__mocks__/react-native.ts @@ -58,6 +58,20 @@ actualRN.NativeModules.DdSdk = { clearAllData: jest.fn().mockImplementation( () => new Promise(resolve => resolve()) ) as jest.MockedFunction, + setAccountInfo: jest.fn().mockImplementation( + () => new Promise(resolve => resolve()) + ) as jest.MockedFunction, + clearAccountInfo: jest.fn().mockImplementation( + () => new Promise(resolve => resolve()) + ) as jest.MockedFunction, + addAccountExtraInfo: jest.fn().mockImplementation( + () => new Promise(resolve => resolve()) + ) as jest.MockedFunction, + getConstants: jest + .fn() + .mockImplementation(() => ({})) as jest.MockedFunction< + DdNativeSdkType['getConstants'] + >, addListener: jest.fn().mockImplementation((_: string) => { /* empty */ }) as jest.MockedFunction, diff --git a/packages/core/src/DdSdkReactNative.tsx b/packages/core/src/DdSdkReactNative.tsx index 2f1941193..aae1422e6 100644 --- a/packages/core/src/DdSdkReactNative.tsx +++ b/packages/core/src/DdSdkReactNative.tsx @@ -38,7 +38,7 @@ import { AttributesSingleton } from './sdk/AttributesSingleton/AttributesSinglet import type { Attributes } from './sdk/AttributesSingleton/types'; import { registerNativeBridge } from './sdk/DatadogInternalBridge/DdSdkInternalNativeBridge'; import { BufferSingleton } from './sdk/DatadogProvider/Buffer/BufferSingleton'; -import { NativeDdSdk } from './sdk/DdSdkInternal'; +import { DdSdk } from './sdk/DdSdk'; import { GlobalState } from './sdk/GlobalState/GlobalState'; import { UserInfoSingleton } from './sdk/UserInfoSingleton/UserInfoSingleton'; import type { UserInfo } from './sdk/UserInfoSingleton/types'; @@ -86,7 +86,7 @@ export class DdSdkReactNative { SdkVerbosity.WARN ); if (!__DEV__) { - NativeDdSdk.telemetryDebug( + DdSdk.telemetryDebug( 'RN SDK was already initialized in javascript' ); } @@ -97,7 +97,7 @@ export class DdSdkReactNative { registerNativeBridge(); - await NativeDdSdk.initialize( + await DdSdk.initialize( DdSdkReactNative.buildConfiguration(configuration, params) ); @@ -218,7 +218,7 @@ export class DdSdkReactNative { `Adding attribute ${JSON.stringify(value)} for key ${key}`, SdkVerbosity.DEBUG ); - await NativeDdSdk.addAttribute(key, { value }); + await DdSdk.addAttribute(key, { value }); AttributesSingleton.getInstance().addAttribute(key, value); }; @@ -231,7 +231,7 @@ export class DdSdkReactNative { `Removing attribute for key ${key}`, SdkVerbosity.DEBUG ); - await NativeDdSdk.removeAttribute(key); + await DdSdk.removeAttribute(key); AttributesSingleton.getInstance().removeAttribute(key); }; @@ -245,7 +245,7 @@ export class DdSdkReactNative { `Adding attributes ${JSON.stringify(attributes)}`, SdkVerbosity.DEBUG ); - await NativeDdSdk.addAttributes(attributes); + await DdSdk.addAttributes(attributes); AttributesSingleton.getInstance().addAttributes(attributes); }; @@ -258,7 +258,7 @@ export class DdSdkReactNative { `Removing attributes for keys ${JSON.stringify(keys)}`, SdkVerbosity.DEBUG ); - await NativeDdSdk.removeAttributes(keys); + await DdSdk.removeAttributes(keys); AttributesSingleton.getInstance().removeAttributes(keys); }; @@ -285,7 +285,7 @@ export class DdSdkReactNative { SdkVerbosity.DEBUG ); - await NativeDdSdk.setUserInfo(userInfo); + await DdSdk.setUserInfo(userInfo); UserInfoSingleton.getInstance().setUserInfo(userInfo); }; @@ -295,7 +295,7 @@ export class DdSdkReactNative { */ static clearUserInfo = async (): Promise => { InternalLog.log('Clearing user info', SdkVerbosity.DEBUG); - await NativeDdSdk.clearUserInfo(); + await DdSdk.clearUserInfo(); UserInfoSingleton.getInstance().clearUserInfo(); }; @@ -312,7 +312,7 @@ export class DdSdkReactNative { SdkVerbosity.DEBUG ); - await NativeDdSdk.addUserExtraInfo(extraUserInfo); + await DdSdk.addUserExtraInfo(extraUserInfo); UserInfoSingleton.getInstance().addUserExtraInfo(extraUserInfo); }; @@ -333,7 +333,7 @@ export class DdSdkReactNative { SdkVerbosity.DEBUG ); - await NativeDdSdk.setAccountInfo(accountInfo); + await DdSdk.setAccountInfo(accountInfo); AccountInfoSingleton.getInstance().setAccountInfo(accountInfo); }; @@ -343,7 +343,7 @@ export class DdSdkReactNative { */ static clearAccountInfo = async (): Promise => { InternalLog.log('Clearing account info', SdkVerbosity.DEBUG); - await NativeDdSdk.clearAccountInfo(); + await DdSdk.clearAccountInfo(); AccountInfoSingleton.getInstance().clearAccountInfo(); }; @@ -375,7 +375,7 @@ export class DdSdkReactNative { ...extraAccountInfo }; - await NativeDdSdk.addAccountExtraInfo(extraInfo); + await DdSdk.addAccountExtraInfo(extraInfo); AccountInfoSingleton.getInstance().addAccountExtraInfo( extraAccountInfo ); @@ -388,7 +388,7 @@ export class DdSdkReactNative { */ static setTrackingConsent = (consent: TrackingConsent): Promise => { InternalLog.log(`Setting consent ${consent}`, SdkVerbosity.DEBUG); - return NativeDdSdk.setTrackingConsent(consent); + return DdSdk.setTrackingConsent(consent); }; /** @@ -397,7 +397,7 @@ export class DdSdkReactNative { */ static clearAllData = (): Promise => { InternalLog.log('Clearing all data', SdkVerbosity.DEBUG); - return NativeDdSdk.clearAllData(); + return DdSdk.clearAllData(); }; private static buildConfiguration = ( diff --git a/packages/core/src/__tests__/DdSdkReactNative.test.tsx b/packages/core/src/__tests__/DdSdkReactNative.test.tsx index 433035fb0..01cd44371 100644 --- a/packages/core/src/__tests__/DdSdkReactNative.test.tsx +++ b/packages/core/src/__tests__/DdSdkReactNative.test.tsx @@ -22,9 +22,10 @@ import { DdRumUserInteractionTracking } from '../rum/instrumentation/interaction import { DdRumResourceTracking } from '../rum/instrumentation/resourceTracking/DdRumResourceTracking'; import { PropagatorType, RumActionType } from '../rum/types'; import { AttributesSingleton } from '../sdk/AttributesSingleton/AttributesSingleton'; -import { NativeDdSdk } from '../sdk/DdSdkInternal'; +import { DdSdkWrapper } from '../sdk/DdSdkInternal'; import { GlobalState } from '../sdk/GlobalState/GlobalState'; import { UserInfoSingleton } from '../sdk/UserInfoSingleton/UserInfoSingleton'; +import NativeDdSdkSpec from '../specs/NativeDdSdk'; import type { LogEvent } from '../types'; import { ErrorSource } from '../types'; import { version as sdkVersion } from '../version'; @@ -1184,8 +1185,8 @@ describe('DdSdkReactNative', () => { await DdSdkReactNative.addAttribute(key, value); // THEN - expect(NativeDdSdk.addAttribute).toHaveBeenCalledTimes(1); - expect(NativeDdSdk.addAttribute).toHaveBeenCalledWith(key, { + expect(NativeDdSdkSpec.addAttribute).toHaveBeenCalledTimes(1); + expect(NativeDdSdkSpec.addAttribute).toHaveBeenCalledWith(key, { value }); expect(AttributesSingleton.getInstance().getAttribute(key)).toEqual( @@ -1205,8 +1206,8 @@ describe('DdSdkReactNative', () => { await DdSdkReactNative.removeAttribute(key); // THEN - expect(NativeDdSdk.removeAttribute).toHaveBeenCalledTimes(1); - expect(NativeDdSdk.removeAttribute).toHaveBeenCalledWith(key); + expect(NativeDdSdkSpec.removeAttribute).toHaveBeenCalledTimes(1); + expect(NativeDdSdkSpec.removeAttribute).toHaveBeenCalledWith(key); expect(AttributesSingleton.getInstance().getAttribute(key)).toEqual( undefined ); @@ -1223,8 +1224,10 @@ describe('DdSdkReactNative', () => { await DdSdkReactNative.addAttributes(attributes); // THEN - expect(NativeDdSdk.addAttributes).toHaveBeenCalledTimes(1); - expect(NativeDdSdk.addAttributes).toHaveBeenCalledWith(attributes); + expect(NativeDdSdkSpec.addAttributes).toHaveBeenCalledTimes(1); + expect(NativeDdSdkSpec.addAttributes).toHaveBeenCalledWith( + attributes + ); expect(AttributesSingleton.getInstance().getAttributes()).toEqual({ foo: 'bar' }); @@ -1241,8 +1244,8 @@ describe('DdSdkReactNative', () => { await DdSdkReactNative.removeAttributes(['foo', 'baz']); // THEN - expect(NativeDdSdk.removeAttributes).toHaveBeenCalledTimes(1); - expect(NativeDdSdk.removeAttributes).toHaveBeenCalledWith([ + expect(NativeDdSdkSpec.removeAttributes).toHaveBeenCalledTimes(1); + expect(NativeDdSdkSpec.removeAttributes).toHaveBeenCalledWith([ 'foo', 'baz' ]); @@ -1268,8 +1271,8 @@ describe('DdSdkReactNative', () => { await DdSdkReactNative.setUserInfo(userInfo); // THEN - expect(NativeDdSdk.setUserInfo).toHaveBeenCalledTimes(1); - expect(NativeDdSdk.setUserInfo).toHaveBeenCalledWith(userInfo); + expect(NativeDdSdkSpec.setUserInfo).toHaveBeenCalledTimes(1); + expect(NativeDdSdkSpec.setUserInfo).toHaveBeenCalledWith(userInfo); expect(UserInfoSingleton.getInstance().getUserInfo()).toEqual( userInfo ); @@ -1289,8 +1292,8 @@ describe('DdSdkReactNative', () => { await DdSdkReactNative.addUserExtraInfo(extraInfo); // THEN - expect(NativeDdSdk.addUserExtraInfo).toHaveBeenCalledTimes(1); - expect(NativeDdSdk.addUserExtraInfo).toHaveBeenCalledWith( + expect(NativeDdSdkSpec.addUserExtraInfo).toHaveBeenCalledTimes(1); + expect(NativeDdSdkSpec.addUserExtraInfo).toHaveBeenCalledWith( extraInfo ); expect(UserInfoSingleton.getInstance().getUserInfo()).toEqual({ @@ -1310,7 +1313,7 @@ describe('DdSdkReactNative', () => { await DdSdkReactNative.addUserExtraInfo(extraInfo); // THEN - expect(NativeDdSdk.addUserExtraInfo).toHaveBeenCalledWith( + expect(NativeDdSdkSpec.addUserExtraInfo).toHaveBeenCalledWith( extraInfo ); expect(UserInfoSingleton.getInstance().getUserInfo()).toEqual({ @@ -1339,8 +1342,8 @@ describe('DdSdkReactNative', () => { await DdSdkReactNative.clearUserInfo(); // THEN - expect(NativeDdSdk.clearUserInfo).toHaveBeenCalledTimes(1); - expect(NativeDdSdk.setUserInfo).toHaveBeenCalled(); + expect(NativeDdSdkSpec.clearUserInfo).toHaveBeenCalledTimes(1); + expect(NativeDdSdkSpec.setUserInfo).toHaveBeenCalled(); expect( UserInfoSingleton.getInstance().getUserInfo() ).toBeUndefined(); @@ -1357,8 +1360,8 @@ describe('DdSdkReactNative', () => { DdSdkReactNative.setTrackingConsent(consent); // THEN - expect(NativeDdSdk.setTrackingConsent).toHaveBeenCalledTimes(1); - expect(NativeDdSdk.setTrackingConsent).toHaveBeenCalledWith( + expect(NativeDdSdkSpec.setTrackingConsent).toHaveBeenCalledTimes(1); + expect(NativeDdSdkSpec.setTrackingConsent).toHaveBeenCalledWith( consent ); }); @@ -1370,7 +1373,7 @@ describe('DdSdkReactNative', () => { DdSdkReactNative.clearAllData(); // THEN - expect(NativeDdSdk.clearAllData).toHaveBeenCalledTimes(1); + expect(NativeDdSdkSpec.clearAllData).toHaveBeenCalledTimes(1); }); }); @@ -1500,4 +1503,121 @@ describe('DdSdkReactNative', () => { }); } ); + describe('routing through DdSdkWrapper', () => { + it('DdSdkReactNative.addAttribute reaches the native spec via the wrapper', async () => { + const spy = jest.spyOn(DdSdkWrapper.prototype, 'addAttribute'); + + await DdSdkReactNative.addAttribute('key', 'value'); + + expect(spy).toHaveBeenCalled(); + spy.mockRestore(); + }); + + it('DdSdkReactNative.removeAttribute reaches the native spec via the wrapper', async () => { + const spy = jest.spyOn(DdSdkWrapper.prototype, 'removeAttribute'); + + await DdSdkReactNative.removeAttribute('key'); + + expect(spy).toHaveBeenCalled(); + spy.mockRestore(); + }); + + it('DdSdkReactNative.addAttributes reaches the native spec via the wrapper', async () => { + const spy = jest.spyOn(DdSdkWrapper.prototype, 'addAttributes'); + + await DdSdkReactNative.addAttributes({ key: 'value' }); + + expect(spy).toHaveBeenCalled(); + spy.mockRestore(); + }); + + it('DdSdkReactNative.removeAttributes reaches the native spec via the wrapper', async () => { + const spy = jest.spyOn(DdSdkWrapper.prototype, 'removeAttributes'); + + await DdSdkReactNative.removeAttributes(['key']); + + expect(spy).toHaveBeenCalled(); + spy.mockRestore(); + }); + + it('DdSdkReactNative.setUserInfo reaches the native spec via the wrapper', async () => { + const spy = jest.spyOn(DdSdkWrapper.prototype, 'setUserInfo'); + + await DdSdkReactNative.setUserInfo({ id: 'user-id' }); + + expect(spy).toHaveBeenCalled(); + spy.mockRestore(); + }); + + it('DdSdkReactNative.clearUserInfo reaches the native spec via the wrapper', async () => { + const spy = jest.spyOn(DdSdkWrapper.prototype, 'clearUserInfo'); + + await DdSdkReactNative.clearUserInfo(); + + expect(spy).toHaveBeenCalled(); + spy.mockRestore(); + }); + + it('DdSdkReactNative.addUserExtraInfo reaches the native spec via the wrapper', async () => { + const spy = jest.spyOn(DdSdkWrapper.prototype, 'addUserExtraInfo'); + + await DdSdkReactNative.addUserExtraInfo({ role: 'admin' }); + + expect(spy).toHaveBeenCalled(); + spy.mockRestore(); + }); + + it('DdSdkReactNative.setAccountInfo reaches the native spec via the wrapper', async () => { + const spy = jest.spyOn(DdSdkWrapper.prototype, 'setAccountInfo'); + + await DdSdkReactNative.setAccountInfo({ id: 'account-id' }); + + expect(spy).toHaveBeenCalled(); + spy.mockRestore(); + }); + + it('DdSdkReactNative.clearAccountInfo reaches the native spec via the wrapper', async () => { + const spy = jest.spyOn(DdSdkWrapper.prototype, 'clearAccountInfo'); + + await DdSdkReactNative.clearAccountInfo(); + + expect(spy).toHaveBeenCalled(); + spy.mockRestore(); + }); + + it('DdSdkReactNative.addAccountExtraInfo reaches the native spec via the wrapper', async () => { + // addAccountExtraInfo bails out early unless an account ID is set. + await DdSdkReactNative.setAccountInfo({ id: 'account-id' }); + const spy = jest.spyOn( + DdSdkWrapper.prototype, + 'addAccountExtraInfo' + ); + + await DdSdkReactNative.addAccountExtraInfo({ tier: 'premium' }); + + expect(spy).toHaveBeenCalled(); + spy.mockRestore(); + }); + + it('DdSdkReactNative.setTrackingConsent reaches the native spec via the wrapper', async () => { + const spy = jest.spyOn( + DdSdkWrapper.prototype, + 'setTrackingConsent' + ); + + await DdSdkReactNative.setTrackingConsent(TrackingConsent.GRANTED); + + expect(spy).toHaveBeenCalled(); + spy.mockRestore(); + }); + + it('DdSdkReactNative.clearAllData reaches the native spec via the wrapper', async () => { + const spy = jest.spyOn(DdSdkWrapper.prototype, 'clearAllData'); + + await DdSdkReactNative.clearAllData(); + + expect(spy).toHaveBeenCalled(); + spy.mockRestore(); + }); + }); }); diff --git a/packages/core/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx b/packages/core/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx index 61edf5664..ef25f72ba 100644 --- a/packages/core/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +++ b/packages/core/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx @@ -21,7 +21,7 @@ import React from 'react'; import type { DdNativeRumType } from '../../../nativeModulesTypes'; import { DdRumUserInteractionTracking } from '../../../rum/instrumentation/interactionTracking/DdRumUserInteractionTracking'; import { BufferSingleton } from '../../../sdk/DatadogProvider/Buffer/BufferSingleton'; -import { NativeDdSdk } from '../../../sdk/DdSdkInternal'; +import NativeDdSdkSpec from '../../../specs/NativeDdSdk'; const styles = StyleSheet.create({ button: { @@ -467,7 +467,7 @@ describe('startTracking', () => { jest.setMock('react/jsx-runtime', {}); DdRumUserInteractionTracking.startTracking({}); expect(DdRumUserInteractionTracking['isTracking']).toBe(true); - expect(NativeDdSdk.telemetryDebug).toBeCalledWith( + expect(NativeDdSdkSpec.telemetryDebug).toBeCalledWith( 'React jsx runtime does not export new jsx transform' ); }); @@ -477,7 +477,7 @@ describe('startTracking', () => { DdRumUserInteractionTracking.startTracking({}); expect(DdRumUserInteractionTracking['isTracking']).toBe(true); - expect(NativeDdSdk.telemetryDebug).toBeCalledWith( + expect(NativeDdSdkSpec.telemetryDebug).toBeCalledWith( 'React version does not support new jsx transform' ); }); diff --git a/packages/core/src/rum/DdRum.ts b/packages/core/src/rum/DdRum.ts index 9a7f15dae..a9cfb9f9f 100644 --- a/packages/core/src/rum/DdRum.ts +++ b/packages/core/src/rum/DdRum.ts @@ -13,7 +13,7 @@ import type { DdNativeRumType } from '../nativeModulesTypes'; import { encodeAttributes } from '../sdk/AttributesEncoding/attributesEncoding'; import type { Attributes } from '../sdk/AttributesSingleton/types'; import { bufferVoidNativeCall } from '../sdk/DatadogProvider/Buffer/bufferNativeCall'; -import { NativeDdSdk } from '../sdk/DdSdkInternal'; +import { DdSdk } from '../sdk/DdSdk'; import { GlobalState } from '../sdk/GlobalState/GlobalState'; import type { ErrorSource, FeatureOperationFailure } from '../types'; import { getGlobalInstance } from '../utils/singletonUtils'; @@ -583,7 +583,7 @@ class DdRumWrapper implements DdRumType { } if (isOldStopActionAPI(args)) { if (this.lastActionData) { - NativeDdSdk.telemetryDebug( + DdSdk.telemetryDebug( 'DDdRum.stopAction called with the old signature' ); const { type, name } = this.lastActionData; diff --git a/packages/core/src/rum/__tests__/DdRum.test.ts b/packages/core/src/rum/__tests__/DdRum.test.ts index 7d0a3688b..dc5c64bae 100644 --- a/packages/core/src/rum/__tests__/DdRum.test.ts +++ b/packages/core/src/rum/__tests__/DdRum.test.ts @@ -11,8 +11,8 @@ import type { GestureResponderEvent } from 'react-native'; import { InternalLog } from '../../InternalLog'; import { SdkVerbosity } from '../../config/types'; import { BufferSingleton } from '../../sdk/DatadogProvider/Buffer/BufferSingleton'; -import { NativeDdSdk } from '../../sdk/DdSdkInternal'; import { GlobalState } from '../../sdk/GlobalState/GlobalState'; +import NativeDdSdkSpec from '../../specs/NativeDdSdk'; import { ErrorSource } from '../../types'; import { DdRum } from '../DdRum'; import type { ActionEventMapper } from '../eventMappers/actionEventMapper'; @@ -1444,7 +1444,7 @@ describe('DdRum', () => { test('does not call the native SDK when startAction has not been called before and using old API', async () => { await DdRum.stopAction({ user: 'me' }, 789); expect(NativeModules.DdRum.stopAction).not.toHaveBeenCalled(); - expect(NativeDdSdk.telemetryDebug).not.toHaveBeenCalled(); + expect(NativeDdSdkSpec.telemetryDebug).not.toHaveBeenCalled(); }); test('calls the native SDK when called with old API', async () => { @@ -1456,7 +1456,7 @@ describe('DdRum', () => { { user: 'me' }, 789 ); - expect(NativeDdSdk.telemetryDebug).toHaveBeenCalledWith( + expect(NativeDdSdkSpec.telemetryDebug).toHaveBeenCalledWith( 'DDdRum.stopAction called with the old signature' ); }); @@ -1470,7 +1470,7 @@ describe('DdRum', () => { {}, 456 ); - expect(NativeDdSdk.telemetryDebug).toHaveBeenCalledWith( + expect(NativeDdSdkSpec.telemetryDebug).toHaveBeenCalledWith( 'DDdRum.stopAction called with the old signature' ); }); diff --git a/packages/core/src/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.tsx b/packages/core/src/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.tsx index f1ad64ef6..6665a0390 100644 --- a/packages/core/src/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.tsx +++ b/packages/core/src/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.tsx @@ -9,7 +9,7 @@ import React from 'react'; import { InternalLog } from '../../../InternalLog'; import { SdkVerbosity } from '../../../config/types/SdkVerbosity'; import { getErrorMessage } from '../../../sdk/AttributesEncoding/errorUtils'; -import { NativeDdSdk } from '../../../sdk/DdSdkInternal'; +import { DdSdk } from '../../../sdk/DdSdk'; import { BABEL_PLUGIN_TELEMETRY } from '../../constants'; import { DdBabelInteractionTracking } from './DdBabelInteractionTracking'; @@ -82,7 +82,7 @@ export class DdRumUserInteractionTracking { return; } - NativeDdSdk?.sendTelemetryLog( + DdSdk?.sendTelemetryLog( BABEL_PLUGIN_TELEMETRY, DdBabelInteractionTracking.getTelemetryConfig(), { onlyOnce: true } @@ -126,7 +126,7 @@ export class DdRumUserInteractionTracking { }; } } catch (e) { - NativeDdSdk.telemetryDebug(getErrorMessage(e)); + DdSdk.telemetryDebug(getErrorMessage(e)); } const originalMemo = React.memo; diff --git a/packages/core/src/sdk/DatadogProvider/Buffer/BoundedBuffer.ts b/packages/core/src/sdk/DatadogProvider/Buffer/BoundedBuffer.ts index 071129044..75fdbb131 100644 --- a/packages/core/src/sdk/DatadogProvider/Buffer/BoundedBuffer.ts +++ b/packages/core/src/sdk/DatadogProvider/Buffer/BoundedBuffer.ts @@ -7,7 +7,7 @@ import { InternalLog } from '../../../InternalLog'; import { SdkVerbosity } from '../../../config/types/SdkVerbosity'; import { getErrorStackTrace } from '../../AttributesEncoding/errorUtils'; -import { NativeDdSdk } from '../../DdSdkInternal'; +import { DdSdk } from '../../DdSdk'; import { DatadogBuffer } from './DatadogBuffer'; @@ -206,7 +206,7 @@ export class BoundedBuffer extends DatadogBuffer { private drainTelemetry = () => { Object.values(this.telemetryBuffer).forEach( ({ message, stack, kind, occurrences }) => { - NativeDdSdk.telemetryError( + DdSdk.telemetryError( `${message} happened ${occurrences} times.`, stack, kind diff --git a/packages/core/src/sdk/DatadogProvider/Buffer/__tests__/BoundedBuffer.test.ts b/packages/core/src/sdk/DatadogProvider/Buffer/__tests__/BoundedBuffer.test.ts index 87fd37798..6a6eeb8c6 100644 --- a/packages/core/src/sdk/DatadogProvider/Buffer/__tests__/BoundedBuffer.test.ts +++ b/packages/core/src/sdk/DatadogProvider/Buffer/__tests__/BoundedBuffer.test.ts @@ -5,7 +5,7 @@ */ import { InternalLog } from '../../../../InternalLog'; -import { NativeDdSdk } from '../../../DdSdkInternal'; +import NativeDdSdkSpec from '../../../../specs/NativeDdSdk'; import { BoundedBuffer } from '../BoundedBuffer'; describe('BoundedBuffer', () => { @@ -126,7 +126,7 @@ describe('BoundedBuffer', () => { await buffer.drain(); expect(callbackWithId).toHaveBeenCalledTimes(1); expect(callbackWithId).toHaveBeenNthCalledWith(1, 'callbackId1'); - expect(NativeDdSdk.telemetryError).toHaveBeenCalledWith( + expect(NativeDdSdkSpec.telemetryError).toHaveBeenCalledWith( 'Could not generate enough random numbers happened 2 times.', '', 'RandomIdGenerationError' @@ -146,7 +146,7 @@ describe('BoundedBuffer', () => { await buffer.drain(); expect(fakeCallback).toHaveBeenCalledTimes(3); - expect(NativeDdSdk.telemetryError).toHaveBeenCalledWith( + expect(NativeDdSdkSpec.telemetryError).toHaveBeenCalledWith( 'Buffer overflow happened 1 times.', '', 'BufferOverflow' @@ -171,7 +171,7 @@ describe('BoundedBuffer', () => { expect(fakeCallback).toHaveBeenCalledTimes(1); expect(callbackReturningId).not.toHaveBeenCalled(); expect(callbackWithId).not.toHaveBeenCalled(); - expect(NativeDdSdk.telemetryError).toHaveBeenCalledWith( + expect(NativeDdSdkSpec.telemetryError).toHaveBeenCalledWith( 'Buffer overflow happened 2 times.', '', 'BufferOverflow' @@ -196,7 +196,7 @@ describe('BoundedBuffer', () => { expect(fakeCallback).toHaveBeenCalledTimes(1); expect(callbackReturningId).toHaveBeenCalledTimes(1); expect(callbackWithId).toHaveBeenCalledTimes(1); - expect(NativeDdSdk.telemetryError).not.toHaveBeenCalled(); + expect(NativeDdSdkSpec.telemetryError).not.toHaveBeenCalled(); }); }); }); diff --git a/packages/core/src/sdk/DdSdkInternal.ts b/packages/core/src/sdk/DdSdkInternal.ts index ad6e57af0..2c1d74685 100644 --- a/packages/core/src/sdk/DdSdkInternal.ts +++ b/packages/core/src/sdk/DdSdkInternal.ts @@ -10,16 +10,11 @@ import type { DdNativeSdkType } from '../nativeModulesTypes'; import type { AttributeEncoder } from './AttributesEncoding/types'; // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires -const NativeDdSdk: DdNativeSdkType = require('../specs/NativeDdSdk').default; +const NativeDdSdkSpec: DdNativeSdkType = require('../specs/NativeDdSdk') + .default; -export type DdSdkType = { +export type DdSdkType = DdNativeSdkType & { readonly attributeEncoders: AttributeEncoder[]; - - /** - * Initializes Datadog's features. - * @param configuration: The configuration to use. - */ - initialize(configuration: DdSdkNativeConfiguration): Promise; }; export class DdSdkWrapper implements DdNativeSdkType { @@ -30,49 +25,49 @@ export class DdSdkWrapper implements DdNativeSdkType { initialize(configuration: DdSdkNativeConfiguration): Promise { this._attributeEncoders = [...configuration.attributeEncoders]; - return NativeDdSdk.initialize(configuration); + return NativeDdSdkSpec.initialize(configuration); } getConstants() { - return NativeDdSdk.getConstants(); + return NativeDdSdkSpec.getConstants(); } setUserInfo(user: object): Promise { - return NativeDdSdk.setUserInfo(user); + return NativeDdSdkSpec.setUserInfo(user); } clearUserInfo(): Promise { - return NativeDdSdk.clearUserInfo(); + return NativeDdSdkSpec.clearUserInfo(); } addUserExtraInfo(extraInfo: object): Promise { - return NativeDdSdk.addUserExtraInfo(extraInfo); + return NativeDdSdkSpec.addUserExtraInfo(extraInfo); } addAttribute(key: string, value: object): Promise { - return NativeDdSdk.addAttribute(key, value); + return NativeDdSdkSpec.addAttribute(key, value); } removeAttribute(key: string): Promise { - return NativeDdSdk.removeAttribute(key); + return NativeDdSdkSpec.removeAttribute(key); } addAttributes(attributes: object): Promise { - return NativeDdSdk.addAttributes(attributes); + return NativeDdSdkSpec.addAttributes(attributes); } removeAttributes(keys: string[]): Promise { - return NativeDdSdk.removeAttributes(keys); + return NativeDdSdkSpec.removeAttributes(keys); } setAccountInfo(account: object): Promise { - return NativeDdSdk.setAccountInfo(account); + return NativeDdSdkSpec.setAccountInfo(account); } clearAccountInfo(): Promise { - return NativeDdSdk.clearAccountInfo(); + return NativeDdSdkSpec.clearAccountInfo(); } addAccountExtraInfo(extraInfo: object): Promise { - return NativeDdSdk.addAccountExtraInfo(extraInfo); + return NativeDdSdkSpec.addAccountExtraInfo(extraInfo); } setTrackingConsent(trackingConsent: string): Promise { - return NativeDdSdk.setTrackingConsent(trackingConsent); + return NativeDdSdkSpec.setTrackingConsent(trackingConsent); } sendTelemetryLog( @@ -80,11 +75,11 @@ export class DdSdkWrapper implements DdNativeSdkType { attributes: object, config: object ): Promise { - return NativeDdSdk.sendTelemetryLog(message, attributes, config); + return NativeDdSdkSpec.sendTelemetryLog(message, attributes, config); } telemetryDebug(message: string): Promise { - return NativeDdSdk.telemetryDebug(message); + return NativeDdSdkSpec.telemetryDebug(message); } telemetryError( @@ -92,23 +87,23 @@ export class DdSdkWrapper implements DdNativeSdkType { stack: string, kind: string ): Promise { - return NativeDdSdk.telemetryError(message, stack, kind); + return NativeDdSdkSpec.telemetryError(message, stack, kind); } consumeWebviewEvent(message: string): Promise { - return NativeDdSdk.consumeWebviewEvent(message); + return NativeDdSdkSpec.consumeWebviewEvent(message); } clearAllData(): Promise { - return NativeDdSdk.clearAllData(); + return NativeDdSdkSpec.clearAllData(); } addListener(eventType: string): void { - return NativeDdSdk.addListener(eventType); + return NativeDdSdkSpec.addListener(eventType); } removeListeners(count: number): void { - return NativeDdSdk.removeListeners(count); + return NativeDdSdkSpec.removeListeners(count); } _setAttributeEncodersForTesting( @@ -117,5 +112,3 @@ export class DdSdkWrapper implements DdNativeSdkType { this._attributeEncoders = [...attributeEncoders]; } } - -export { NativeDdSdk }; diff --git a/packages/core/src/sdk/EventMappers/EventMapper.ts b/packages/core/src/sdk/EventMappers/EventMapper.ts index 1d8adfa1f..dc05614fd 100644 --- a/packages/core/src/sdk/EventMappers/EventMapper.ts +++ b/packages/core/src/sdk/EventMappers/EventMapper.ts @@ -10,7 +10,7 @@ import { AccountInfoSingleton } from '../AccountInfoSingleton/AccountInfoSinglet import type { AccountInfo } from '../AccountInfoSingleton/types'; import { AttributesSingleton } from '../AttributesSingleton/AttributesSingleton'; import type { Attributes } from '../AttributesSingleton/types'; -import { NativeDdSdk } from '../DdSdkInternal'; +import { DdSdk } from '../DdSdk'; import { UserInfoSingleton } from '../UserInfoSingleton/UserInfoSingleton'; import type { UserInfo } from '../UserInfoSingleton/types'; @@ -92,7 +92,7 @@ export class EventMapper { )}: ${error}`, SdkVerbosity.WARN ); - NativeDdSdk.telemetryDebug('Error while running the event mapper'); + DdSdk.telemetryDebug('Error while running the event mapper'); return this.formatMapperEventForNative(backupEvent, backupEvent); } }; diff --git a/packages/core/src/sdk/EventMappers/__tests__/EventMapper.test.ts b/packages/core/src/sdk/EventMappers/__tests__/EventMapper.test.ts index 93333f7de..42077abce 100644 --- a/packages/core/src/sdk/EventMappers/__tests__/EventMapper.test.ts +++ b/packages/core/src/sdk/EventMappers/__tests__/EventMapper.test.ts @@ -4,7 +4,7 @@ * Copyright 2016-Present Datadog, Inc. */ -import { NativeDdSdk } from '../../DdSdkInternal'; +import NativeDdSdkSpec from '../../../specs/NativeDdSdk'; import { EventMapper } from '../EventMapper'; describe('EventMapper', () => { @@ -26,7 +26,7 @@ describe('EventMapper', () => { ).toEqual({ someData: 'some data' }); - expect(NativeDdSdk.telemetryDebug).toHaveBeenCalledWith( + expect(NativeDdSdkSpec.telemetryDebug).toHaveBeenCalledWith( 'Error while running the event mapper' ); });