Refactor API client
This commit is contained in:
parent
a42ece6df4
commit
2500dcf77a
|
@ -1,4 +1,4 @@
|
|||
import api from '../api';
|
||||
import api, { baseClient } from '../api';
|
||||
import { importFetchedAccount } from './importer';
|
||||
import snackbar from 'soapbox/actions/snackbar';
|
||||
import { createAccount } from 'soapbox/actions/accounts';
|
||||
|
@ -138,15 +138,7 @@ export function verifyCredentials(token) {
|
|||
return (dispatch, getState) => {
|
||||
dispatch({ type: VERIFY_CREDENTIALS_REQUEST });
|
||||
|
||||
const request = {
|
||||
method: 'get',
|
||||
url: '/api/v1/accounts/verify_credentials',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
},
|
||||
};
|
||||
|
||||
return api(getState).request(request).then(({ data: account }) => {
|
||||
return baseClient(token).get('/api/v1/accounts/verify_credentials').then(({ data: account }) => {
|
||||
dispatch(importFetchedAccount(account));
|
||||
dispatch({ type: VERIFY_CREDENTIALS_SUCCESS, token, account });
|
||||
if (account.id === getState().get('me')) dispatch(fetchMeSuccess(account));
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
import axios from 'axios';
|
||||
import LinkHeader from 'http-link-header';
|
||||
import { getAccessToken, getAppToken } from 'soapbox/utils/auth';
|
||||
|
||||
export const getLinks = response => {
|
||||
const value = response.headers.link;
|
||||
|
@ -11,28 +12,28 @@ export const getLinks = response => {
|
|||
|
||||
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']);
|
||||
}
|
||||
return authType === 'app' ? getAppToken(state) : getAccessToken(state);
|
||||
};
|
||||
|
||||
export default (getState, authType = 'user') => {
|
||||
const accessToken = getToken(getState, authType);
|
||||
|
||||
return axios.create({
|
||||
headers: Object.assign(accessToken ? {
|
||||
'Authorization': `Bearer ${accessToken}`,
|
||||
} : {}),
|
||||
|
||||
transformResponse: [function(data) {
|
||||
const maybeParseJSON = data => {
|
||||
try {
|
||||
return JSON.parse(data);
|
||||
} catch(Exception) {
|
||||
return data;
|
||||
}
|
||||
}],
|
||||
};
|
||||
|
||||
export const baseClient = accessToken => {
|
||||
return axios.create({
|
||||
headers: Object.assign(accessToken ? {
|
||||
'Authorization': `Bearer ${accessToken}`,
|
||||
} : {}),
|
||||
|
||||
transformResponse: [maybeParseJSON],
|
||||
});
|
||||
};
|
||||
|
||||
export default (getState, authType = 'user') => {
|
||||
const accessToken = getToken(getState, authType);
|
||||
return baseClient(accessToken);
|
||||
};
|
||||
|
|
|
@ -2,7 +2,13 @@ export const isLoggedIn = getState => {
|
|||
return typeof getState().get('me') === 'string';
|
||||
};
|
||||
|
||||
export const getAppToken = state => state.getIn(['auth', 'app', 'access_token']);
|
||||
|
||||
export const getUserToken = (state, accountId) => {
|
||||
return state.getIn(['auth', 'users', accountId, 'access_token']);
|
||||
};
|
||||
|
||||
export const getAccessToken = state => {
|
||||
const me = state.get('me');
|
||||
return state.getIn(['auth', 'users', me, 'access_token']);
|
||||
return getUserToken(state, me);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue