{"size": 4113, "ext": "js", "max_stars_repo_path": "frontend/src/utils/request.js", "max_stars_repo_name": "shizidushu/simple-report-data-table-vuetify", "max_stars_repo_stars_event_min_datetime": "2020-01-06T13:35:22.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-27T15:54:19.000Z", "max_issues_repo_path": "frontend/src/utils/request.js", "max_issues_repo_name": "shizidushu/simple-report-data-table-vuetify", "max_issues_repo_issues_event_min_datetime": "2021-03-10T03:27:24.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T22:02:32.000Z", "max_forks_repo_path": "frontend/src/utils/request.js", "max_forks_repo_name": "shizidushu/simple-report-data-table-vuetify", "max_forks_repo_forks_event_min_datetime": "2020-01-06T13:38:26.000Z", "max_forks_repo_forks_event_max_datetime": "2021-04-26T11:03:42.000Z"}
{"max_stars_count": 13, "max_issues_count": 5, "max_forks_count": 3, "avg_line_length": 33.4390243902, "max_line_length": 129, "alphanum_fraction": 0.6114758084}
import axios from "axios";

import router from "@/router";
import store from "@/store";

// create an axios instance
const service = axios.create({
  //baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  baseURL: process.env.VUE_APP_BASE_API, // "http://localhost:8000/api/v1/",
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 1000 * 60 * 10 // request timeout
});

// add token before every request if there is token in localStorage
service.interceptors.request.use(
  config => {
    let token = localStorage.getItem("token");

    if (token) {
      config.headers["Authorization"] = `Bearer ${token}`;
    }

    return config;
  },

  error => {
    console.log("error in service.interceptors.request.use"); // for debug
    return Promise.reject(error);
  }
);

service.interceptors.response.use(
  response => {
    return response;
  },
  async function(error) {
    if (error.response) {
      // The request was made and the server responded with a status code
      // that falls out of the range of 2xx
      const {
        config,
        response: { status, data }
      } = error;

      const originalRequest = config;

      console.log("in interceptors", JSON.stringify(error));

      // redict to login page no token provided in header
      if (data.detail === "Not authenticated") {
        router.push({ name: "login" });
        return Promise.reject(false);
      }

      // keep reject when failed to get a new token
      if (originalRequest.url.includes(`login/access-token`)) {
        return Promise.reject(error);
      }

      // redict to login page if exists but can't validate
      // this may happen when the backend change secert key of jwt
      if (status === 401 && data.detail === "Could not validate credentials") {
        console.log(
          'status === 401 && data.detail === "Could not validate credentials"'
        );
        store.dispatch("user/logout");
        router.push({ name: "login" });
        return Promise.reject(error);
      }

      // redict to login page if token expires
      if (status === 401 && data.detail === "Expired token") {
        console.log('status === 401 && data.detail === "Expired token"');
        // check https://github.com/konshensx16/vue-todo-frontend/blob/master/src/main.js when there is need to use refresh token
        router.push({ name: "login" });
        return Promise.reject(error);
      }

      // redict to login page if token expires
      if (status === 403 && data.detail === "No permission") {
        console.log('status === 401 && data.detail === "No permission"');
        // check https://github.com/konshensx16/vue-todo-frontend/blob/master/src/main.js when there is need to use refresh token
        router.push({ name: "NoPermission" });
        return Promise.reject(error);
      }
    } else if (error.request) {
      // The request was made but no response was received
      // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
      // http.ClientRequest in node.js
      console.log("The request was made but no response was received");
      console.log(error.toJSON());
      if (error.message !== "Request aborted") {
        await store.dispatch("app/changeAlertInfo", {
          props: { type: "error", dismissible: true },
          defaultSlot:
            "对不起，出错了。" +
            "Info: error.request " +
            "Detail: " +
            JSON.stringify(error.message)
        });
      }

      return Promise.reject(error);
    } else {
      // Something happened in setting up the request that triggered an Error
      console.log(
        "Something happened in setting up the request that triggered an Error"
      );
      await store.dispatch("app/changeAlertInfo", {
        props: { type: "error", dismissible: true },
        defaultSlot:
          "Sorry, there is an error." +
          "Info: setting up the request " +
          "Detail: " +
          JSON.stringify(error.message)
      });
      return Promise.reject(error);
    }
  }
);

export default service;
