"use client"; import axios from "axios"; import Cookies from "js-cookie"; import qs from "qs"; import axiosInstance from "./axiosInstance"; import axiosInstanceJson from "./axiosInstanceJson"; import axiosInterceptor from "./axiosInterceptor"; import { data } from "@/app/[locale]/(protected)/charts/rechart/charts-rechart-bar/data"; import { url } from "inspector"; 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 getAPI(url: any, token: any) { const headers = { Authorization: `Bearer ${token || tokenAuth}`, }; const response = await axios .get(baseURL + url, { 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 postAPIWithJson(url: any, data: any, token: any) { const headers = { Authorization: `Bearer ${token || tokenAuth}`, }; const response = await axiosInstanceJson .post(url, data, { headers }) .catch((error) => error.response); if (response?.status > 300) { return { error: true, message: response?.data?.message, data: null, }; } return { error: false, message: "success", data: response?.data, }; } export async function deleteAPIWithJson(url: any) { const response = await axiosInstanceJson .delete(url) .catch((error) => error.response); if (response?.status > 300) { return { error: true, message: response?.data?.message, data: null, }; } return { error: false, message: "success", data: response?.data, }; } export async function getAPIInterceptor(url: any) { const response = await axiosInterceptor .get(url) .catch((error) => error.response); if (response?.status == 401) { Object.keys(Cookies.get()).forEach((cookieName) => { Cookies.remove(cookieName); }); window.location.href = "/"; } else if (response?.status > 300 && response?.status != 401) { return { error: true, message: response?.data?.message, data: null, }; } else if (response?.data.success) { return { error: false, message: "success", data: response?.data, }; } return { error: true, message: response?.data?.message || null, data: null, }; } export async function postAPIInterceptor(url: any, data: any) { const response = await axiosInterceptor .post(url, data) .catch((error) => error.response); if (response?.status == 401) { Object.keys(Cookies.get()).forEach((cookieName) => { Cookies.remove(cookieName); }); window.location.href = "/"; } else if (response?.status > 300 && response?.status != 401) { return { error: true, message: response?.data?.message, data: null, }; } else if (response?.data.success) { return { error: false, message: "success", data: response?.data, }; } return { error: true, message: response?.data.message, data: null, }; } export async function deleteAPIInterceptor(url: any, data: any) { const response = await axiosInterceptor .delete(url, { data }) .catch((error) => error.response); if (response?.status == 401) { Object.keys(Cookies.get()).forEach((cookieName) => { Cookies.remove(cookieName); }); window.location.href = "/"; } else if (response?.status > 300 && response?.status != 401) { return { error: true, message: response?.data?.message, data: null, }; } else if (response?.data.success) { return { error: false, message: "success", data: response?.data, }; } return { error: true, message: response?.data?.message, data: null, }; } export async function getAPIInterceptorProfile(url: any) { const response = await axiosInterceptor .get(url) .catch((error) => error.response); if (response?.status > 300) { return { error: true, message: response?.data?.message, data: null, }; } return { error: false, message: response?.data?.message, data: response?.data, }; } // export async function postFileApiInterceptor(url: any, data: any) { // const response = await axiosInterceptor // .post(url, data, { // onUploadProgress: (progressEvent) => { // const percentage = Math.round( // (progressEvent.loaded * 100) / progressEvent.total // ); // console.log(percentage); // }, // }) // .catch((error) => error.response); // if (response?.status > 300) { // return { // error: true, // message: response?.data?.message, // data: null, // }; // } // if (response?.data.success) { // return { // error: false, // message: "success", // data: response?.data, // }; // } // return { // error: true, // message: response?.data.message, // data: null, // }; // }