Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. import axios from 'axios'
  2. import { getToken, logout } from './auth'
  3. import humps from 'humps'
  4.  
  5. const env = process.env.REACT_APP_API
  6.  
  7. export const getBaseURL = () => {
  8. const url = {
  9. development: '',
  10. production: '',
  11. }
  12.  
  13. return url[env]
  14. }
  15.  
  16. const baseURL = getBaseURL()
  17.  
  18. const api = axios.create({ baseURL })
  19.  
  20. api.interceptors.request.use(async config => {
  21. const token = getToken()
  22. if (token) {
  23. config.headers.Authorization = `Bearer ${token}`
  24. }
  25.  
  26. config.data = humps.decamelizeKeys(config.data)
  27. config.params = humps.decamelizeKeys(config.params)
  28.  
  29. return config
  30. })
  31.  
  32. api.interceptors.response.use(
  33. resp => {
  34. const response = humps.camelizeKeys(resp.data)
  35. return response
  36. },
  37. error => {
  38. if (error.response.status === 401) {
  39. return logout()
  40. }
  41. return Promise.reject(error.response)
  42. },
  43. )
  44.  
  45. export default api
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement