summaryrefslogtreecommitdiff
path: root/src/frontend/utils/Request.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/frontend/utils/Request.ts')
-rw-r--r--src/frontend/utils/Request.ts34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/frontend/utils/Request.ts b/src/frontend/utils/Request.ts
new file mode 100644
index 0000000..b0ff20e
--- /dev/null
+++ b/src/frontend/utils/Request.ts
@@ -0,0 +1,34 @@
+// Copyright The OpenTelemetry Authors
+// SPDX-License-Identifier: Apache-2.0
+
+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 response = await fetch(`${url}?${new URLSearchParams(queryParams).toString()}`, {
+ method,
+ body: body ? JSON.stringify(body) : undefined,
+ headers,
+ });
+
+ const responseText = await response.text();
+
+ if (!!responseText) return JSON.parse(responseText);
+
+ return undefined as unknown as T;
+};
+
+export default request;