Advertisement
kidto1412

axios

Jun 30th, 2024
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { boot } from 'quasar/wrappers'
  2. import axios, { AxiosInstance } from 'axios'
  3. import { LocalStorage } from 'quasar'
  4. import { AUTH_TOKEN_LOCAL } from 'src/constants/auth'
  5. import utility from 'src/tools/utility'
  6. import { useRouter } from 'vue-router'
  7. import { Router } from 'src/router'
  8. import { useAuthStore } from 'src/store/auth'
  9.  
  10. declare module '@vue/runtime-core' {
  11.   interface ComponentCustomProperties {
  12.     $axios: AxiosInstance
  13.   }
  14. }
  15.  
  16. // Be careful when using SSR for cross-request state pollution
  17. // due to creating a Singleton instance here;
  18. // If any client changes this (global) instance, it might be a
  19. // good idea to move this instance creation inside of the
  20. // "export default () => {}" function below (which runs individually
  21. // for each client)
  22. // const api = axios.create({ baseURL: 'https://api.example.com' });
  23. const api = axios.create({ baseURL: process.env.API_URL })
  24.  
  25. api.interceptors.request.use(function (config) {
  26.   const token = LocalStorage.getItem(AUTH_TOKEN_LOCAL)
  27.   // console.log(utility.ReadSession(AUTH_TOKEN_LOCAL))
  28.   if (token) {
  29.     config.headers.common.Authorization =
  30.       'Bearer ' + utility.ReadSession(AUTH_TOKEN_LOCAL)
  31.   }
  32.   return config
  33. })
  34.  
  35. api.interceptors.response.use(
  36.   function (response) {
  37.     api.defaults.headers.post['Content-Type'] =
  38.       'application/x-www-form-urlencoded'
  39.     return response
  40.   },
  41.   function (error) {
  42.     return Promise.reject(error)
  43.     // return Promise.reject(error)
  44.   }
  45. )
  46.  
  47. export default boot(({ app, router }) => {
  48.   const $router = useRouter()
  49.  
  50.   // for use inside Vue files (Options API) through this.$axios and this.$api
  51.  
  52.   app.config.globalProperties.$axios = axios
  53.   // ^ ^ ^ this will allow you to use this.$axios (for Vue Options API form)
  54.   //       so you won't necessarily have to import axios in each vue file
  55.  
  56.   app.config.globalProperties.$api = api
  57.   // ^ ^ ^ this will allow you to use this.$api (for Vue Options API form)
  58.   //       so you can easily perform requests against your app's API
  59.  
  60.   api.interceptors.response.use(
  61.     function (response) {
  62.       return response
  63.     },
  64.     function (error) {
  65.       const authStore = useAuthStore()
  66.       const route = router.options.routes[0].name
  67.       if (error.response.status === 401) {
  68.         authStore.setIsLoggedin(false)
  69.         localStorage.clear()
  70.         router.push({ name: 'login' })
  71.         // this.$router.go();
  72.  
  73.         // if (route !== "login") {
  74.         //   utility.notifySuccess("Login Expired");
  75.         // }
  76.       }
  77.       return Promise.reject(error)
  78.     }
  79.   )
  80. })
  81.  
  82. export { api }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement