Permintaan dan Pencegahan Tanggapan Axios

import axios, { AxiosResponse } from "axios";
import { useNavigate } from "react-router-dom";

export const axiosInstance = axios.create({
  baseURL: "http://localhost:3001/api",
});

const responseIntercepter = (response: AxiosResponse) => {
  return response;
};
const requestIntercepter = (config) => {
  config.headers.Authorization =
    "Bearer " + localStorage.getItem("accessToken");
  return config;
};

axiosInstance.interceptors.request.use(requestIntercepter);
axiosInstance.interceptors.response.use(responseIntercepter, (err) => {
  const expectedErrors =
    err.response && err.response.status >= 400 && err.response.status < 500;
  if (!expectedErrors) {
    console.log(err, "err");
    return;
  } else {
    if (err.response.status === 401) {
      localStorage.removeItem("accessToken");
      localStorage.removeItem("userId");
      const navigate = useNavigate();
      navigate("/login");
    }
    return Promise.reject(err);
  }
});
Bloody Bear