bigbuffet-rw/app/soapbox/api.ts

94 lines
2.9 KiB
TypeScript
Raw Normal View History

/**
* API: HTTP client and utilities.
* @see {@link https://github.com/axios/axios}
* @module soapbox/api
*/
2020-03-27 13:59:38 -07:00
'use strict';
2022-03-14 16:06:42 -07:00
import axios, { AxiosInstance, AxiosResponse } from 'axios';
2020-03-27 13:59:38 -07:00
import LinkHeader from 'http-link-header';
2021-08-21 18:41:29 -07:00
import { createSelector } from 'reselect';
2022-03-14 16:06:42 -07:00
import * as BuildConfig from 'soapbox/build_config';
import { RootState } from 'soapbox/store';
import { getAccessToken, getAppToken, parseBaseURL } from 'soapbox/utils/auth';
import { isURL } from 'soapbox/utils/auth';
2020-03-27 13:59:38 -07:00
/**
Parse Link headers, mostly for pagination.
@see {@link https://www.npmjs.com/package/http-link-header}
@param {object} response - Axios response object
@returns {object} Link object
*/
2022-03-14 16:06:42 -07:00
export const getLinks = (response: AxiosResponse): LinkHeader => {
return new LinkHeader(response.headers?.link);
2020-03-27 13:59:38 -07:00
};
2022-04-23 20:31:49 -07:00
export const getNextLink = (response: AxiosResponse): string | undefined => {
return getLinks(response).refs.find(link => link.rel === 'next')?.uri;
};
2022-03-14 16:06:42 -07:00
const getToken = (state: RootState, authType: string) => {
2021-03-31 12:47:54 -07:00
return authType === 'app' ? getAppToken(state) : getAccessToken(state);
};
2020-04-29 12:06:26 -07:00
2022-03-14 16:06:42 -07:00
const maybeParseJSON = (data: string) => {
2021-03-31 12:47:54 -07:00
try {
return JSON.parse(data);
} catch (Exception) {
2021-03-31 12:47:54 -07:00
return data;
}
};
2020-04-29 12:06:26 -07:00
2021-08-21 18:41:29 -07:00
const getAuthBaseURL = createSelector([
2022-03-14 16:06:42 -07:00
(state: RootState, me: string | false | null) => state.accounts.getIn([me, 'url']),
(state: RootState, _me: string | false | null) => state.auth.get('me'),
2021-08-21 18:41:29 -07:00
], (accountUrl, authUserUrl) => {
const baseURL = parseBaseURL(accountUrl) || parseBaseURL(authUserUrl);
return baseURL !== window.location.origin ? baseURL : '';
});
/**
* Base client for HTTP requests.
* @param {string} accessToken
* @param {string} baseURL
* @returns {object} Axios instance
*/
2022-03-14 16:06:42 -07:00
export const baseClient = (accessToken: string, baseURL: string = ''): AxiosInstance => {
2020-04-04 13:28:57 -07:00
return axios.create({
// When BACKEND_URL is set, always use it.
2022-03-14 16:06:42 -07:00
baseURL: isURL(BuildConfig.BACKEND_URL) ? BuildConfig.BACKEND_URL : baseURL,
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
} : {}),
2021-03-31 12:47:54 -07:00
transformResponse: [maybeParseJSON],
2020-04-04 13:28:57 -07:00
});
};
2021-03-31 12:47:54 -07:00
/**
* Dumb client for grabbing static files.
2021-09-05 11:21:39 -07:00
* It uses FE_SUBDIRECTORY and parses JSON if possible.
* No authorization is needed.
*/
export const staticClient = axios.create({
2022-03-14 16:06:42 -07:00
baseURL: BuildConfig.FE_SUBDIRECTORY,
transformResponse: [maybeParseJSON],
});
/**
* Stateful API client.
* Uses credentials from the Redux store if available.
* @param {function} getState - Must return the Redux state
* @param {string} authType - Either 'user' or 'app'
* @returns {object} Axios instance
*/
2022-03-14 16:06:42 -07:00
export default (getState: () => RootState, authType: string = 'user'): AxiosInstance => {
2021-08-21 18:41:29 -07:00
const state = getState();
const accessToken = getToken(state, authType);
2022-03-14 16:06:42 -07:00
const me = state.me;
2022-04-04 12:24:42 -07:00
const baseURL = me ? getAuthBaseURL(state, me) : '';
2021-08-21 18:41:29 -07:00
return baseClient(accessToken, baseURL);
2021-03-31 12:47:54 -07:00
};