import { handleError, handleSuccess } from "./ApiClient"; import { AxiosRequestConfig } from "axios"; import { BASE_URL } from "./NetworkConstant"; import axios from "axios"; import { getBuildConfigDetails } from "../App/common/utilities/CacheUtils"; import { addBundleVersionToHeader } from "./NetworkUtils"; function newAbortSignal(timeoutMs: number): AbortSignal { const abortController = new AbortController(); setTimeout(() => abortController.abort(), timeoutMs || 10000); return abortController.signal; } export const get = async ( url: string, config?: AxiosRequestConfig, params?: Record, ): Promise => { try { let requestConfig: AxiosRequestConfig = config || {}; let baseUrl: string | undefined; await getBuildConfigDetails().then(response => { baseUrl = response?.baseUrl; }); let axiosInstance = axios.create({ baseURL: baseUrl ? baseUrl : BASE_URL, timeout: 10000, headers: requestConfig.headers, signal: newAbortSignal(11000), }); if (params) { requestConfig.params = params; } addBundleVersionToHeader(axiosInstance); if (params) { requestConfig.params = params; } const response = await axiosInstance.get(baseUrl + url, requestConfig); return handleSuccess(response); } catch (error) { return handleError(error); } }; export const post = async ( url: string, data?: any, config?: AxiosRequestConfig, ): Promise => { try { let requestConfig: AxiosRequestConfig = config || {}; let baseUrl: string | undefined; await getBuildConfigDetails().then(response => { baseUrl = response?.baseUrl; }); let axiosInstance = axios.create({ baseURL: baseUrl ? baseUrl : BASE_URL, timeout: 10000, headers: requestConfig.headers, signal: newAbortSignal(11000), }); addBundleVersionToHeader(axiosInstance); const response = await axiosInstance.post( baseUrl + url, data, requestConfig, ); return handleSuccess(response); } catch (error) { return handleError(error); } }; export const patch = async ( url: string, data?: any, config?: AxiosRequestConfig, ): Promise => { try { let requestConfig: AxiosRequestConfig = config || {}; let baseUrl: string | undefined; await getBuildConfigDetails().then(response => { baseUrl = response?.baseUrl; }); let axiosInstance = axios.create({ baseURL: baseUrl ? baseUrl : BASE_URL, timeout: 10000, headers: requestConfig.headers, signal: newAbortSignal(11000), }); addBundleVersionToHeader(axiosInstance); const response = await axiosInstance.patch( baseUrl + url, data, requestConfig, ); return handleSuccess(response); } catch (error) { return handleError(error); } };