axiosConfig.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import axios from "axios";
  2. const token = localStorage.getItem("token");
  3. // const instance = axios.create({
  4. // baseURL: "https://test-app-a-level.herokuapp.com/api/",
  5. // headers: {
  6. // "Content-Type": "application/json",
  7. // Authorization: `Bearer ${token}`
  8. // }
  9. // });
  10. const AxiosInstance = axios.create({
  11. baseURL: "https://test-app-a-level.herokuapp.com/api/",
  12. headers: {
  13. "Content-Type": "application/json",
  14. Authorization: `Bearer ${token}`
  15. }
  16. });
  17. AxiosInstance.interceptors.request.use(
  18. config => {
  19. const token = localStorage.getItem("token");
  20. if (token) {
  21. return {
  22. ...config,
  23. headers: { ...config.headers, Authorization: `Bearer ${token}` }
  24. };
  25. }
  26. return config;
  27. },
  28. error => Promise.reject(error)
  29. );
  30. AxiosInstance.interceptors.response.use(
  31. response => response,
  32. error => {
  33. // const { response = {} } = error;
  34. // if (response.status === 403) logOut();
  35. return Promise.reject(error);
  36. }
  37. );
  38. // export default info => {
  39. // const { url, method = "GET", params, data } = info;
  40. // const result = AxiosInstance({ url, method, params, data });
  41. // return result;
  42. // };
  43. export default AxiosInstance;