75 lines
2.2 KiB
TypeScript
75 lines
2.2 KiB
TypeScript
|
|
import { handleError, handleSuccess, ApiClient, getDefaultHeaderData } from "./ApiClient";
|
||
|
|
import { AxiosRequestConfig, InternalAxiosRequestConfig } from "axios";
|
||
|
|
import { getDeviceId, getSessionToken } from "./NetworkUtils";
|
||
|
|
import { BASE_URL } from "./NetworkConstant";
|
||
|
|
import axios from "axios";
|
||
|
|
import { getBuildConfigDetails } from "../App/common/utilities/CacheUtils";
|
||
|
|
|
||
|
|
export const get = async <T>(
|
||
|
|
url: string,
|
||
|
|
config?: AxiosRequestConfig
|
||
|
|
): Promise<T> => {
|
||
|
|
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
|
||
|
|
})
|
||
|
|
const response = await axiosInstance.get<T>(baseUrl + url, requestConfig);
|
||
|
|
return handleSuccess<T>(response);
|
||
|
|
} catch (error) {
|
||
|
|
return handleError(error);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
export const post = async <T>(
|
||
|
|
url: string,
|
||
|
|
data?: any,
|
||
|
|
config?: AxiosRequestConfig
|
||
|
|
): Promise<T> => {
|
||
|
|
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
|
||
|
|
})
|
||
|
|
const response = await axiosInstance.post<T>(baseUrl + url, data, requestConfig);
|
||
|
|
return handleSuccess<T>(response);
|
||
|
|
} catch (error) {
|
||
|
|
return handleError(error);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
export const patch = async <T>(
|
||
|
|
url: string,
|
||
|
|
data?: any,
|
||
|
|
config?: AxiosRequestConfig
|
||
|
|
): Promise<T> => {
|
||
|
|
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
|
||
|
|
})
|
||
|
|
const response = await axiosInstance.patch<T>(baseUrl + url, data, requestConfig);
|
||
|
|
return handleSuccess<T>(response);
|
||
|
|
} catch (error) {
|
||
|
|
return handleError(error);
|
||
|
|
}
|
||
|
|
};
|