Fixed auth token header, Moved dispatch out of constructors
This commit is contained in:
@@ -10,43 +10,45 @@ const localStorage = global.process && process.env.NODE_ENV === "test"
|
||||
|
||||
function headers() {
|
||||
const token = localStorage.getItem("userToken") || "";
|
||||
|
||||
return {
|
||||
let header = {
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Token ${token}`
|
||||
"Content-Type": "application/json"
|
||||
};
|
||||
if (token) {
|
||||
header["Authorization"] = `Token ${token}`;
|
||||
}
|
||||
return header;
|
||||
}
|
||||
|
||||
const apiInstance = axios.create({
|
||||
baseURL: API_ENDPOINT,
|
||||
timeout: 3000,
|
||||
timeout: 3000
|
||||
});
|
||||
|
||||
export function get(url, params = {}) {
|
||||
return apiInstance
|
||||
.get(url, {params, headers: headers()})
|
||||
.get(url, { params, headers: headers() })
|
||||
.then(response => response.data)
|
||||
.catch(error => Promise.reject(error));
|
||||
}
|
||||
|
||||
export function post(url, data) {
|
||||
return apiInstance
|
||||
.post(url, data, {headers: headers()})
|
||||
.post(url, data, { headers: headers() })
|
||||
.then(response => response.data)
|
||||
.catch(error => Promise.reject(error));
|
||||
}
|
||||
|
||||
export function patch(url, data) {
|
||||
return apiInstance
|
||||
.patch(url, data, {headers: headers()})
|
||||
.patch(url, data, { headers: headers() })
|
||||
.then(response => response.data)
|
||||
.catch(error => Promise.reject(error));
|
||||
}
|
||||
|
||||
export function del(url) {
|
||||
return apiInstance
|
||||
.delete(url, {headers: headers()})
|
||||
.delete(url, { headers: headers() })
|
||||
.then(response => response.data)
|
||||
.catch(error => Promise.reject(error));
|
||||
}
|
||||
|
Reference in New Issue
Block a user