2020-03-27 21:59:38 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import axios from 'axios';
|
|
|
|
import LinkHeader from 'http-link-header';
|
|
|
|
import ready from './ready';
|
|
|
|
|
|
|
|
export const getLinks = response => {
|
|
|
|
const value = response.headers.link;
|
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
return { refs: [] };
|
|
|
|
}
|
|
|
|
|
|
|
|
return LinkHeader.parse(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
let csrfHeader = {};
|
|
|
|
|
|
|
|
function setCSRFHeader() {
|
|
|
|
const csrfToken = document.querySelector('meta[name=csrf-token]');
|
|
|
|
if (csrfToken) {
|
|
|
|
csrfHeader['X-CSRF-Token'] = csrfToken.content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ready(setCSRFHeader);
|
|
|
|
|
2020-04-29 21:06:26 +02:00
|
|
|
const getToken = (getState, authType) =>
|
|
|
|
getState().getIn(['auth', authType, 'access_token']);
|
|
|
|
|
|
|
|
export default (getState, authType = 'user') => {
|
|
|
|
const accessToken = getToken(getState, authType);
|
|
|
|
|
2020-04-04 22:28:57 +02:00
|
|
|
return axios.create({
|
2020-04-29 21:06:26 +02:00
|
|
|
headers: Object.assign(csrfHeader, accessToken ? {
|
|
|
|
'Authorization': `Bearer ${accessToken}`,
|
2020-04-04 22:28:57 +02:00
|
|
|
} : {}),
|
|
|
|
|
2020-04-14 23:47:35 +02:00
|
|
|
transformResponse: [function(data) {
|
2020-04-04 22:28:57 +02:00
|
|
|
try {
|
|
|
|
return JSON.parse(data);
|
|
|
|
} catch(Exception) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}],
|
|
|
|
});
|
|
|
|
};
|