2018-09-06 17:46:09 +00:00
|
|
|
import axios from 'axios'
|
2022-01-14 20:52:00 +00:00
|
|
|
import {getToken} from '@/helpers/auth'
|
2018-09-06 17:46:09 +00:00
|
|
|
|
2022-01-14 20:52:00 +00:00
|
|
|
export function HTTPFactory() {
|
2022-09-04 14:30:11 +00:00
|
|
|
const instance = axios.create({baseURL: window.API_URL})
|
|
|
|
|
|
|
|
instance.interceptors.request.use((config) => {
|
|
|
|
// by setting the baseURL fresh for every request
|
|
|
|
// we make sure that it is never outdated in case it is updated
|
|
|
|
config.baseURL = window.API_URL
|
|
|
|
|
|
|
|
return config
|
2020-10-11 10:13:35 +00:00
|
|
|
})
|
2022-09-04 14:30:11 +00:00
|
|
|
|
|
|
|
return instance
|
2020-10-11 10:13:35 +00:00
|
|
|
}
|
2022-01-14 20:52:00 +00:00
|
|
|
|
2022-09-04 14:30:11 +00:00
|
|
|
export function AuthenticatedHTTPFactory() {
|
|
|
|
const instance = HTTPFactory()
|
|
|
|
|
|
|
|
instance.interceptors.request.use((config) => {
|
|
|
|
config.headers = {
|
|
|
|
...config.headers,
|
2022-01-14 20:52:00 +00:00
|
|
|
'Content-Type': 'application/json',
|
2022-09-04 14:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the default auth header if we have a token
|
|
|
|
const token = getToken()
|
|
|
|
if (token !== null) {
|
|
|
|
config.headers['Authorization'] = `Bearer ${token}`
|
|
|
|
}
|
|
|
|
return config
|
2022-01-14 20:52:00 +00:00
|
|
|
})
|
2022-09-04 14:30:11 +00:00
|
|
|
|
|
|
|
return instance
|
2022-01-14 20:52:00 +00:00
|
|
|
}
|