diff --git a/app/soapbox/api.js b/app/soapbox/api.js index 945ffbd25..fd8ec2577 100644 --- a/app/soapbox/api.js +++ b/app/soapbox/api.js @@ -4,6 +4,8 @@ import axios from 'axios'; import LinkHeader from 'http-link-header'; import { getAccessToken, getAppToken, parseBaseURL } from 'soapbox/utils/auth'; import { createSelector } from 'reselect'; +import { BACKEND_URL } from 'soapbox/build_config'; +import { isURL } from 'soapbox/utils/auth'; export const getLinks = response => { const value = response.headers.link; @@ -33,7 +35,8 @@ const getAuthBaseURL = createSelector([ export const baseClient = (accessToken, baseURL = '') => { return axios.create({ - baseURL, + // When BACKEND_URL is set, always use it. + baseURL: isURL(BACKEND_URL) ? BACKEND_URL : baseURL, headers: Object.assign(accessToken ? { 'Authorization': `Bearer ${accessToken}`, } : {}), diff --git a/app/soapbox/build_config.js b/app/soapbox/build_config.js new file mode 100644 index 000000000..7dc8cec99 --- /dev/null +++ b/app/soapbox/build_config.js @@ -0,0 +1,23 @@ +// @preval +/** + * Build config: configuration set at build time. + * @module soapbox/build_config + */ + +const { BACKEND_URL } = process.env; + +const sanitizeURL = url => { + try { + return new URL(url).toString(); + } catch { + return ''; + } +}; + +// JSON.parse/stringify is to emulate what @preval is doing and avoid any +// inconsistent behavior in dev mode +const sanitize = obj => JSON.parse(JSON.stringify(obj)); + +module.exports = sanitize({ + BACKEND_URL: sanitizeURL(BACKEND_URL), +});