diff options
Diffstat (limited to 'src/react-native-app/utils/Request.ts')
| -rw-r--r-- | src/react-native-app/utils/Request.ts | 41 |
1 files changed, 41 insertions, 0 deletions
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; |
