bigbuffet-rw/app/soapbox/api.js

39 lines
897 B
JavaScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
'use strict';
import axios from 'axios';
import LinkHeader from 'http-link-header';
export const getLinks = response => {
const value = response.headers.link;
if (!value) return { refs: [] };
2020-03-27 13:59:38 -07:00
return LinkHeader.parse(value);
};
const getToken = (getState, authType) => {
const state = getState();
if (authType === 'app') {
return state.getIn(['auth', 'app', 'access_token']);
} else {
const me = state.get('me');
return state.getIn(['auth', 'users', me, 'access_token']);
}
};
2020-04-29 12:06:26 -07:00
export default (getState, authType = 'user') => {
const accessToken = getToken(getState, authType);
2020-04-04 13:28:57 -07:00
return axios.create({
2020-06-09 12:45:48 -07:00
headers: Object.assign(accessToken ? {
2020-04-29 12:06:26 -07:00
'Authorization': `Bearer ${accessToken}`,
2020-04-04 13:28:57 -07:00
} : {}),
transformResponse: [function(data) {
2020-04-04 13:28:57 -07:00
try {
return JSON.parse(data);
} catch(Exception) {
return data;
}
}],
});
};