diff options
| author | Saumit <justsaumit@protonmail.com> | 2025-09-27 02:14:26 +0530 |
|---|---|---|
| committer | Saumit <justsaumit@protonmail.com> | 2025-09-27 02:14:26 +0530 |
| commit | 82e03978b89938219958032efb1448cc76baa181 (patch) | |
| tree | 626f3e54d52ecd49be0ed3bee30abacc0453d081 /src/frontend/utils/Request.ts | |
Initial snapshot - OpenTelemetry demo 2.1.3 -f
Diffstat (limited to 'src/frontend/utils/Request.ts')
| -rw-r--r-- | src/frontend/utils/Request.ts | 34 |
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; |
