rest-axios.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import axios from 'axios'
  2. // https://github.com/alvar0hurtad0/drupal-vuejs-todo/blob/master/frontend/src/api/axiosInterceptor.js
  3. // console.log('drupalSettings', drupalSettings)
  4. // console.log('window.location.origin', window.location.origin)
  5. // axios.interceptors.response.use(
  6. // response => {
  7. // return Promise.resolve(response)
  8. // },
  9. // error => {
  10. // const { status } = error.response
  11. // console.warn('error in rest-axios', status)
  12. // if (status === 403) {
  13. // window.location = '/'
  14. // }
  15. // return Promise.reject(error)
  16. // }
  17. // )
  18. const REST = axios.create({
  19. baseURL: window.location.origin + '/' + drupalSettings.path.pathPrefix,
  20. withCredentials: true,
  21. headers: {
  22. // Authorization: 'Bearer {token}',
  23. 'Content-Type': 'application/json'
  24. }
  25. })
  26. REST.interceptors.response.use(
  27. response => {
  28. return Promise.resolve(response)
  29. },
  30. error => {
  31. const { status } = error.response
  32. console.warn('error in rest-axios', status)
  33. if (status === 403) {
  34. window.location = '/'
  35. }
  36. return Promise.reject(error)
  37. }
  38. )
  39. export default REST