2020-01-20 05:54:29 -09:00
|
|
|
import { fetchUtils } from 'react-admin'
|
2020-05-11 17:27:00 -08:00
|
|
|
import baseUrl from '../utils/baseUrl'
|
|
|
|
|
import config from '../config'
|
2020-01-20 05:54:29 -09:00
|
|
|
|
2020-04-06 12:23:47 -08:00
|
|
|
const customAuthorizationHeader = 'X-ND-Authorization'
|
2020-02-14 05:02:32 -09:00
|
|
|
|
2020-01-20 05:54:29 -09:00
|
|
|
const httpClient = (url, options = {}) => {
|
2020-04-03 13:50:42 -08:00
|
|
|
url = baseUrl(url)
|
2020-01-20 05:54:29 -09:00
|
|
|
if (!options.headers) {
|
|
|
|
|
options.headers = new Headers({ Accept: 'application/json' })
|
|
|
|
|
}
|
|
|
|
|
const token = localStorage.getItem('token')
|
|
|
|
|
if (token) {
|
2020-04-06 12:23:47 -08:00
|
|
|
options.headers.set(customAuthorizationHeader, `Bearer ${token}`)
|
2020-01-20 05:54:29 -09:00
|
|
|
}
|
|
|
|
|
return fetchUtils.fetchJson(url, options).then((response) => {
|
2020-04-06 12:23:47 -08:00
|
|
|
const token = response.headers.get(customAuthorizationHeader)
|
2020-01-20 05:54:29 -09:00
|
|
|
if (token) {
|
|
|
|
|
localStorage.setItem('token', token)
|
2020-04-08 09:20:02 -08:00
|
|
|
// Avoid going to create admin dialog after logout/login without a refresh
|
|
|
|
|
config.firstTime = false
|
2020-01-20 05:54:29 -09:00
|
|
|
}
|
|
|
|
|
return response
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 17:27:00 -08:00
|
|
|
export default httpClient
|