diff options
Diffstat (limited to 'src/react-native-app/utils')
| -rw-r--r-- | src/react-native-app/utils/Localhost.ts | 14 | ||||
| -rw-r--r-- | src/react-native-app/utils/Request.ts | 41 | ||||
| -rw-r--r-- | src/react-native-app/utils/SessionIdProcessor.ts | 32 | ||||
| -rw-r--r-- | src/react-native-app/utils/Settings.ts | 22 |
4 files changed, 109 insertions, 0 deletions
diff --git a/src/react-native-app/utils/Localhost.ts b/src/react-native-app/utils/Localhost.ts new file mode 100644 index 0000000..8b3eaef --- /dev/null +++ b/src/react-native-app/utils/Localhost.ts @@ -0,0 +1,14 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 +import DeviceInfo from "react-native-device-info"; +import { Platform } from "react-native"; + +const getLocalhost = async (): Promise<string> => { + const isEmulator = await DeviceInfo.isEmulator(); + + // The Android emulator has a special loopback for localhost + // https://developer.android.com/studio/run/emulator-networking#networkaddresses + return Platform.OS === "android" && isEmulator ? "10.0.2.2" : "localhost"; +}; + +export default getLocalhost; diff --git a/src/react-native-app/utils/Request.ts b/src/react-native-app/utils/Request.ts new file mode 100644 index 0000000..77fadc8 --- /dev/null +++ b/src/react-native-app/utils/Request.ts @@ -0,0 +1,41 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 +/** + * Copied with modification from src/frontend/utils/Request.ts + */ +import getFrontendProxyURL from "@/utils/Settings"; + +interface IRequestParams { + url: string; + body?: object; + method?: "GET" | "POST" | "PUT" | "DELETE"; + queryParams?: Record<string, any>; + headers?: Record<string, string>; +} + +const request = async <T>({ + url = "", + method = "GET", + body, + queryParams = {}, + headers = { + "content-type": "application/json", + }, +}: IRequestParams): Promise<T> => { + const proxyURL = await getFrontendProxyURL(); + const requestURL = `${proxyURL}${url}?${new URLSearchParams(queryParams).toString()}`; + const requestBody = body ? JSON.stringify(body) : undefined; + const response = await fetch(requestURL, { + method, + body: requestBody, + headers, + }); + + const responseText = await response.text(); + + if (!!responseText) return JSON.parse(responseText); + + return undefined as unknown as T; +}; + +export default request; diff --git a/src/react-native-app/utils/SessionIdProcessor.ts b/src/react-native-app/utils/SessionIdProcessor.ts new file mode 100644 index 0000000..217ff42 --- /dev/null +++ b/src/react-native-app/utils/SessionIdProcessor.ts @@ -0,0 +1,32 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 +/** + * Copied with modification from src/frontend/utils/telemetry/SessionIdProcessor.ts + */ +import { Context } from "@opentelemetry/api"; +import { + ReadableSpan, + Span, + SpanProcessor, +} from "@opentelemetry/sdk-trace-web"; +import SessionGateway from "@/gateways/Session.gateway"; + +export class SessionIdProcessor implements SpanProcessor { + forceFlush(): Promise<void> { + return Promise.resolve(); + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + onStart(span: Span, parentContext: Context): void { + SessionGateway.getSession().then(({ userId }) => { + span.setAttribute("session.id", userId); + }); + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function + onEnd(span: ReadableSpan): void {} + + shutdown(): Promise<void> { + return Promise.resolve(); + } +} diff --git a/src/react-native-app/utils/Settings.ts b/src/react-native-app/utils/Settings.ts new file mode 100644 index 0000000..1a9c7ff --- /dev/null +++ b/src/react-native-app/utils/Settings.ts @@ -0,0 +1,22 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 +import AsyncStorage from "@react-native-async-storage/async-storage"; +import getLocalhost from "@/utils/Localhost"; + +const FRONTEND_PROXY_URL_SETTING = 'frontend_proxy_url'; + +export const getFrontendProxyURL = async (): Promise<string> => { + const proxyURL = await AsyncStorage.getItem(FRONTEND_PROXY_URL_SETTING); + if (proxyURL) { + return proxyURL + } else { + const localhost = await getLocalhost(); + return `http://${localhost}:${process.env.EXPO_PUBLIC_FRONTEND_PROXY_PORT}`; + } +}; + +export const setFrontendProxyURL = async (url: string) => { + await AsyncStorage.setItem(FRONTEND_PROXY_URL_SETTING, url); +} + +export default getFrontendProxyURL; |
