import Promise from 'bluebird'
import axios from 'axios'
import _ from 'lodash'
import { pullToken, logout } from './Auth'

const defaultOptions = {
  timeout: 2000
}

const createClient = (clientOptions, { toastErrors }) => {
  const client = axios.create({
    ...defaultOptions,
    ...clientOptions
  })

  client.interceptors.response.use(
    response => response,
    error => {
      const wrappedError = _.get(error, 'response.data')
      const errorCode = _.get(wrappedError, 'errorCode')
      if (errorCode) {
        if (errorCode === 'BP_0005') {
          return logout()
        }
        return Promise.reject(wrappedError)
      } else {
        return Promise.reject(error)
      }
    }
  )

  if (toastErrors) {
    client.interceptors.response.use(
      response => response,
      error => {
        const wrappedError = _.get(error, 'response.data')
        const message = _.get(wrappedError, 'message')

        if (wrappedError && message) {
          showToast(message)
        } else {
          error.message && showToast(error.message)
        }

        return Promise.reject(error)
      }
    )
  }
  return client
}

const overrideApiUrl = process.env.REACT_APP_API_URL
  ? { baseURL: `${process.env.REACT_APP_API_URL}/api/v1` }
  : { baseURL: `/api/v1` }

export default {
  getSecured({ token, toastErrors = true } = {}) {
    if (!token) {
      const ls = pullToken()
      token = ls && ls.token
    }

    return createClient(
      {
        headers: {
          Authorization: `Bearer ${token}`
        },
        ...overrideApiUrl
      },
      { toastErrors }
    )
  },

  getAnonymous({ toastErrors = true } = {}) {
    return createClient(overrideApiUrl, { toastErrors })
  }
}

@@ -823,37 +823,39 @@
    const wrapped
-Error
+Message
  = _.get(error, 
@@ -860,32 +860,40 @@
 , 'response.data
+.message
 ')%0A        const
@@ -891,84 +891,91 @@
     
-const message = _.get(wrappedError, 'm
+if (wrappedMessage) %7B%0A          showToast(wrappedM
 essage
-'
 )%0A
-%0A
+ 
        
- if (wrappedError && 
+%7D else if (error.
 mess
@@ -1001,16 +1001,22 @@
 owToast(
+error.
 message)
@@ -1047,48 +1047,69 @@
     
-error.message && showToast(error.message
+showToast('Something wrong happened. Please try again later.'
 )%0A  

fix pr comments