api: convert to typescript

This commit is contained in:
Alex Gleason 2022-03-14 18:06:42 -05:00
parent a801a8a7c8
commit b2a8f3c1bc
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 14 additions and 15 deletions

View File

@ -5,11 +5,12 @@
*/ */
'use strict'; 'use strict';
import axios from 'axios'; import axios, { AxiosInstance, AxiosResponse } from 'axios';
import LinkHeader from 'http-link-header'; import LinkHeader from 'http-link-header';
import { createSelector } from 'reselect'; import { createSelector } from 'reselect';
import { BACKEND_URL, FE_SUBDIRECTORY } from 'soapbox/build_config'; import * as BuildConfig from 'soapbox/build_config';
import { RootState } from 'soapbox/store';
import { getAccessToken, getAppToken, parseBaseURL } from 'soapbox/utils/auth'; import { getAccessToken, getAppToken, parseBaseURL } from 'soapbox/utils/auth';
import { isURL } from 'soapbox/utils/auth'; import { isURL } from 'soapbox/utils/auth';
@ -19,17 +20,15 @@ import { isURL } from 'soapbox/utils/auth';
@param {object} response - Axios response object @param {object} response - Axios response object
@returns {object} Link object @returns {object} Link object
*/ */
export const getLinks = response => { export const getLinks = (response: AxiosResponse): LinkHeader => {
const value = response.headers.link; return new LinkHeader(response.headers?.link);
if (!value) return { refs: [] };
return LinkHeader.parse(value);
}; };
const getToken = (state, authType) => { const getToken = (state: RootState, authType: string) => {
return authType === 'app' ? getAppToken(state) : getAccessToken(state); return authType === 'app' ? getAppToken(state) : getAccessToken(state);
}; };
const maybeParseJSON = data => { const maybeParseJSON = (data: string) => {
try { try {
return JSON.parse(data); return JSON.parse(data);
} catch(Exception) { } catch(Exception) {
@ -38,8 +37,8 @@ const maybeParseJSON = data => {
}; };
const getAuthBaseURL = createSelector([ const getAuthBaseURL = createSelector([
(state, me) => state.getIn(['accounts', me, 'url']), (state: RootState, me: string | false | null) => state.accounts.getIn([me, 'url']),
(state, me) => state.getIn(['auth', 'me']), (state: RootState, _me: string | false | null) => state.auth.get('me'),
], (accountUrl, authUserUrl) => { ], (accountUrl, authUserUrl) => {
const baseURL = parseBaseURL(accountUrl) || parseBaseURL(authUserUrl); const baseURL = parseBaseURL(accountUrl) || parseBaseURL(authUserUrl);
return baseURL !== window.location.origin ? baseURL : ''; return baseURL !== window.location.origin ? baseURL : '';
@ -51,10 +50,10 @@ const getAuthBaseURL = createSelector([
* @param {string} baseURL * @param {string} baseURL
* @returns {object} Axios instance * @returns {object} Axios instance
*/ */
export const baseClient = (accessToken, baseURL = '') => { export const baseClient = (accessToken: string, baseURL: string = ''): AxiosInstance => {
return axios.create({ return axios.create({
// When BACKEND_URL is set, always use it. // When BACKEND_URL is set, always use it.
baseURL: isURL(BACKEND_URL) ? BACKEND_URL : baseURL, baseURL: isURL(BuildConfig.BACKEND_URL) ? BuildConfig.BACKEND_URL : baseURL,
headers: Object.assign(accessToken ? { headers: Object.assign(accessToken ? {
'Authorization': `Bearer ${accessToken}`, 'Authorization': `Bearer ${accessToken}`,
} : {}), } : {}),
@ -69,7 +68,7 @@ export const baseClient = (accessToken, baseURL = '') => {
* No authorization is needed. * No authorization is needed.
*/ */
export const staticClient = axios.create({ export const staticClient = axios.create({
baseURL: FE_SUBDIRECTORY, baseURL: BuildConfig.FE_SUBDIRECTORY,
transformResponse: [maybeParseJSON], transformResponse: [maybeParseJSON],
}); });
@ -80,10 +79,10 @@ export const staticClient = axios.create({
* @param {string} authType - Either 'user' or 'app' * @param {string} authType - Either 'user' or 'app'
* @returns {object} Axios instance * @returns {object} Axios instance
*/ */
export default (getState, authType = 'user') => { export default (getState: () => RootState, authType: string = 'user'): AxiosInstance => {
const state = getState(); const state = getState();
const accessToken = getToken(state, authType); const accessToken = getToken(state, authType);
const me = state.get('me'); const me = state.me;
const baseURL = getAuthBaseURL(state, me); const baseURL = getAuthBaseURL(state, me);
return baseClient(accessToken, baseURL); return baseClient(accessToken, baseURL);