SHARE
TWEET

Untitled

a guest Aug 19th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top