Updated fetch cookie
This commit is contained in:
@@ -1,43 +1,32 @@
|
||||
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
||||
// import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
||||
|
||||
const serviceHost = process.env.SERVICE_HOST || 'http://localhost';
|
||||
const servicePort = process.env.SERVICE_PORT || 5000;
|
||||
const baseURL = `${serviceHost}:${servicePort}`;
|
||||
|
||||
function createAxiosClient(): AxiosInstance {
|
||||
const axiosClient = axios.create({
|
||||
baseURL: `${serviceHost}:${servicePort}`
|
||||
export async function get(endpoint: string, params: Record<string, any> = {}): Promise<Response> {
|
||||
// Remove undefined params
|
||||
Object.keys(params).forEach((key) => params[key] === undefined && delete params[key]);
|
||||
const urlParams = new URLSearchParams(params);
|
||||
const url = urlParams ? `${baseURL}/${endpoint}?${urlParams}` : `${baseURL}/${endpoint}`;
|
||||
const response = await fetch(url, {
|
||||
method: 'GET',
|
||||
credentials: 'include'
|
||||
});
|
||||
return response;
|
||||
}
|
||||
|
||||
axiosClient.interceptors.request.use(
|
||||
(request) => {
|
||||
request.withCredentials = true;
|
||||
return request;
|
||||
export async function post(endpoint: string, body = {}): Promise<Response> {
|
||||
const url = `${baseURL}/${endpoint}`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
(error) => {
|
||||
console.error(error);
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
return axiosClient;
|
||||
}
|
||||
|
||||
const axiosClient = createAxiosClient();
|
||||
|
||||
export async function getRequest(
|
||||
url: string,
|
||||
config?: AxiosRequestConfig<any>
|
||||
): Promise<AxiosResponse<any, any> | undefined> {
|
||||
const response = await axiosClient.get(`/${url}`, config);
|
||||
return response || undefined;
|
||||
}
|
||||
|
||||
export async function postRequest(
|
||||
url: string,
|
||||
data?: any,
|
||||
config?: AxiosRequestConfig<any>
|
||||
): Promise<AxiosResponse<any, any> | undefined> {
|
||||
const response = await axiosClient.post(`/${url}`, data, config);
|
||||
return response || undefined;
|
||||
credentials: 'include',
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
return response;
|
||||
}
|
||||
|
||||
export interface Metadata {
|
||||
|
||||
Reference in New Issue
Block a user