diff --git a/packages/react-native-webview/src/NativeDdSdk.ts b/packages/react-native-webview/src/NativeDdSdk.ts new file mode 100644 index 000000000..53ec26002 --- /dev/null +++ b/packages/react-native-webview/src/NativeDdSdk.ts @@ -0,0 +1,20 @@ +/* + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. + * This product includes software developed at Datadog (https://www.datadoghq.com/). + * Copyright 2016-Present Datadog, Inc. + */ + +import type { TurboModule } from 'react-native'; +import { TurboModuleRegistry } from 'react-native'; + +/** + * We have to redefine the spec for the Native SDK here to be able to use the new architecture. + * We don't declare it in a spec file so we don't end up with a duplicate definition of the native module. + */ +interface PartialNativeDdSdkSpec extends TurboModule { + consumeWebviewEvent(message: string): Promise; + telemetryError(message: string, stack: string, kind: string): Promise; +} +export const NativeDdSdk = TurboModuleRegistry.get( + 'DdSdk' +); diff --git a/packages/react-native-webview/src/__utils__/formatAllowedHosts.ts b/packages/react-native-webview/src/__utils__/formatAllowedHosts.ts index 9c7971400..25142ec89 100644 --- a/packages/react-native-webview/src/__utils__/formatAllowedHosts.ts +++ b/packages/react-native-webview/src/__utils__/formatAllowedHosts.ts @@ -3,14 +3,14 @@ * This product includes software developed at Datadog (https://www.datadoghq.com/). * Copyright 2016-Present Datadog, Inc. */ -import { NativeModules } from 'react-native'; +import { NativeDdSdk } from '../NativeDdSdk'; export function formatAllowedHosts(allowedHosts?: string[]): string { try { return `'${JSON.stringify(allowedHosts)}'`; } catch (e: any) { - if (NativeModules.DdSdk && NativeModules.DdSdk.telemetryError) { - NativeModules.DdSdk.telemetryError( + if (NativeDdSdk) { + NativeDdSdk.telemetryError( getErrorMessage(e), getErrorStackTrace(e), 'AllowedHostsError' diff --git a/packages/react-native-webview/src/index.tsx b/packages/react-native-webview/src/index.tsx index 26dd270b5..b99750ec0 100644 --- a/packages/react-native-webview/src/index.tsx +++ b/packages/react-native-webview/src/index.tsx @@ -5,10 +5,9 @@ */ import type { WebViewMessageEvent, WebViewProps } from 'react-native-webview'; import { WebView as RNWebView } from 'react-native-webview'; -import type { TurboModule } from 'react-native'; -import { TurboModuleRegistry } from 'react-native'; import React, { forwardRef, useCallback } from 'react'; +import { NativeDdSdk } from './NativeDdSdk'; import { DATADOG_MESSAGE_PREFIX, getInjectedJavaScriptBeforeContentLoaded @@ -50,12 +49,3 @@ const WebViewComponent = (props: Props, ref: React.Ref>) => { export const WebView = forwardRef(WebViewComponent); export default WebView; - -/** - * We have to redefine the spec for the Native SDK here to be able to use the new architecture. - * We don't declare it in a separate file so we don't end up with a duplicate definition of the native module. - */ -interface PartialNativeDdSdkSpec extends TurboModule { - consumeWebviewEvent(message: string): Promise; -} -const NativeDdSdk = TurboModuleRegistry.get('DdSdk');