44 lines
1.0 KiB
JavaScript
44 lines
1.0 KiB
JavaScript
import axios from "axios";
|
|
|
|
export const getToken = () => {
|
|
return localStorage.token;
|
|
};
|
|
|
|
const Chapar = axios.create({
|
|
baseURL: process.env.NEXT_PUBLIC_API_URL,
|
|
timeout: 10000,
|
|
headers: {
|
|
common: {
|
|
"Content-type": "application/json",
|
|
"Access-Control-Allow-Origin": "*",
|
|
...(typeof window !== "undefined" &&
|
|
localStorage.token && {
|
|
Authorization: getToken(),
|
|
}),
|
|
},
|
|
},
|
|
});
|
|
|
|
Chapar.interceptors.response.use(
|
|
function (response) {
|
|
// Any status code that lie within the range of 2xx cause this function to trigger
|
|
// Do something with response data
|
|
|
|
return response.data;
|
|
},
|
|
function (error, status) {
|
|
// Any status codes that falls outside the range of 2xx cause this function to trigger
|
|
// Do something with response error
|
|
// ;
|
|
|
|
// if (error.response.status === 401) {
|
|
// localStorage.removeItem("token");
|
|
// window.location.href = "/login";
|
|
// }
|
|
|
|
return Promise.reject({ error, status: error?.response?.status });
|
|
}
|
|
);
|
|
|
|
export default Chapar;
|