Refactor API client

This commit is contained in:
Alex Gleason 2021-03-31 14:47:54 -05:00
parent a42ece6df4
commit 2500dcf77a
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 25 additions and 26 deletions

View File

@ -1,4 +1,4 @@
import api from '../api'; import api, { baseClient } from '../api';
import { importFetchedAccount } from './importer'; import { importFetchedAccount } from './importer';
import snackbar from 'soapbox/actions/snackbar'; import snackbar from 'soapbox/actions/snackbar';
import { createAccount } from 'soapbox/actions/accounts'; import { createAccount } from 'soapbox/actions/accounts';
@ -138,15 +138,7 @@ export function verifyCredentials(token) {
return (dispatch, getState) => { return (dispatch, getState) => {
dispatch({ type: VERIFY_CREDENTIALS_REQUEST }); dispatch({ type: VERIFY_CREDENTIALS_REQUEST });
const request = { return baseClient(token).get('/api/v1/accounts/verify_credentials').then(({ data: account }) => {
method: 'get',
url: '/api/v1/accounts/verify_credentials',
headers: {
'Authorization': `Bearer ${token}`,
},
};
return api(getState).request(request).then(({ data: account }) => {
dispatch(importFetchedAccount(account)); dispatch(importFetchedAccount(account));
dispatch({ type: VERIFY_CREDENTIALS_SUCCESS, token, account }); dispatch({ type: VERIFY_CREDENTIALS_SUCCESS, token, account });
if (account.id === getState().get('me')) dispatch(fetchMeSuccess(account)); if (account.id === getState().get('me')) dispatch(fetchMeSuccess(account));

View File

@ -2,6 +2,7 @@
import axios from 'axios'; import axios from 'axios';
import LinkHeader from 'http-link-header'; import LinkHeader from 'http-link-header';
import { getAccessToken, getAppToken } from 'soapbox/utils/auth';
export const getLinks = response => { export const getLinks = response => {
const value = response.headers.link; const value = response.headers.link;
@ -11,28 +12,28 @@ export const getLinks = response => {
const getToken = (getState, authType) => { const getToken = (getState, authType) => {
const state = getState(); const state = getState();
if (authType === 'app') { return authType === 'app' ? getAppToken(state) : getAccessToken(state);
return state.getIn(['auth', 'app', 'access_token']);
} else {
const me = state.get('me');
return state.getIn(['auth', 'users', me, 'access_token']);
}
}; };
export default (getState, authType = 'user') => { const maybeParseJSON = data => {
const accessToken = getToken(getState, authType);
return axios.create({
headers: Object.assign(accessToken ? {
'Authorization': `Bearer ${accessToken}`,
} : {}),
transformResponse: [function(data) {
try { try {
return JSON.parse(data); return JSON.parse(data);
} catch(Exception) { } catch(Exception) {
return data; 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);
};

View File

@ -2,7 +2,13 @@ export const isLoggedIn = getState => {
return typeof getState().get('me') === 'string'; 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 => { export const getAccessToken = state => {
const me = state.get('me'); const me = state.get('me');
return state.getIn(['auth', 'users', me, 'access_token']); return getUserToken(state, me);
}; };