mediahub-fe/service/http-config/http-base-service.ts

101 lines
2.7 KiB
TypeScript
Raw Normal View History

import axiosInstance from "@/config/axiosInstance";
import axiosBaseInstance from "./axios-base-instance";
import axios from "axios";
import Cookies from "js-cookie";
import qs from "qs";
// export async function httpPost(pathUrl: any, headers: any, data?: any) {
// const response = await axiosBaseInstance
// .post(pathUrl, data, { headers })
// .catch(function (error: any) {
// console.log(error);
// return error.response;
// });
// console.log("Response base svc : ", response);
// if (response?.status == 200 || response?.status == 201) {
// return {
// error: false,
// message: "success",
// data: response?.data,
// };
// } else {
// return {
// error: true,
// message: response?.data?.message || response?.data || null,
// data: null,
// };
// }
// }
const baseURL = "https://netidhub.com/api/";
const tokenAuth = Cookies.get("access_token")
? Cookies.get("access_token")
: null;
export async function postAPI(url: any, data: any) {
const headers = {
Authorization: `Bearer ${tokenAuth}`,
};
const response = await axiosInstance
.post(url, qs.stringify(data), { headers })
.catch((error) => error.response);
if (response?.status > 300) {
return {
error: true,
message: response?.data.error_description,
data: null,
};
}
return {
error: false,
message: "success",
data: response?.data,
};
}
export async function httpGet(pathUrl: any, headers: any) {
const response = await axiosBaseInstance
.get(pathUrl, { headers })
.catch(function (error: any) {
console.log(error);
return error.response;
});
console.log("Response base svc : ", response);
if (response?.status == 200 || response?.status == 201) {
return {
error: false,
message: "success",
data: response?.data,
};
} else {
return {
error: true,
message: response?.data?.message || response?.data || null,
data: null,
};
}
}
export async function httpPut(pathUrl: any, headers: any, data?: any) {
const response = await axiosBaseInstance
.put(pathUrl, data, { headers })
.catch(function (error: any) {
console.log(error);
return error.response;
});
console.log("Response base svc : ", response);
if (response?.status == 200 || response?.status == 201) {
return {
error: false,
message: "success",
data: response?.data,
};
} else {
return {
error: true,
message: response?.data?.message || response?.data || null,
data: null,
};
}
}