From eea2f38f8c5a099a296b026df001c4b3f944de30 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 26 Aug 2021 20:39:47 -0700 Subject: [PATCH] Build config: allow hardcoding BACKEND_URL into the build --- app/soapbox/api.js | 5 ++++- app/soapbox/build_config.js | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 app/soapbox/build_config.js 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), +});