From 3fabc09dbe71aae93a4fab7a6d90b438174b36df Mon Sep 17 00:00:00 2001 From: kvmw Date: Mon, 4 Aug 2025 15:10:28 +0200 Subject: [PATCH] refactor: deduplicate http calls Signed-off-by: kvmw --- src/config-server.js | 71 ++++++++++++++------------------------------ 1 file changed, 22 insertions(+), 49 deletions(-) diff --git a/src/config-server.js b/src/config-server.js index 64a1e15..fc49e55 100644 --- a/src/config-server.js +++ b/src/config-server.js @@ -4,89 +4,62 @@ import application from './vcap-application.js'; import oauth2 from './oauth2.js'; const DEFAULT_PROFILE = 'default'; + const NAME = env.name || application.getName() || 'application'; +const PROFILES = env.profiles || DEFAULT_PROFILE; +const LABEL = env.label || 'main'; -function configServerUri() { - return services.getUri('p.config-server'); +function buildConfigUri() { + return `${services.getUri('p.config-server')}/${NAME}/${PROFILES}/${LABEL}`; } -// Loads properties from the config-server for the given profiles. -async function loadProperties(profiles) { - const url = `${configServerUri()}/${NAME}/${profiles}/${env.label}`.replace( - /\/$/, - '', - ); - - console.log(`Loading properties from ${url}`); - +async function makeHttpRequest(url, accept) { const token = await oauth2.getAccessToken(); const response = await fetch(`${url}`, { headers: { Authorization: `Bearer ${token}`, - Accept: 'application/json', + Accept: accept, }, }); if (!response.ok) { - throw new Error(`HTTP error! Status: ${response.status}`); + throw new Error(`HTTP error! status: ${response.status}, url: ${url}`); } - - // Merge the property sources into a single properties object. - return (await response.json()).propertySources - .map((it) => it.source) - .reduce((acc, source) => ({ ...source, ...acc }), {}); + return response; } // Loads properties from the config-server. const load = async () => { - // If given profile is not the default profile, load the default properties first. - let defaultProperties = {}; - if (env.profiles !== DEFAULT_PROFILE) { - defaultProperties = await loadProperties(DEFAULT_PROFILE); - } + const url = `${buildConfigUri()}`; + + console.log(`Loading properties from ${url}`); - // Merge the default properties with the profile-specific properties. - return { ...defaultProperties, ...(await loadProperties(env.profiles)) }; + const response = await makeHttpRequest(`${url}`, 'application/json'); + + // Merge the property sources into a single properties object. + return (await response.json()).propertySources + .map((it) => it.source) + .reduce((acc, source) => ({ ...source, ...acc }), {}); }; // Loads a text resource from the config-server. const loadTextResource = async (name) => { - const url = `${configServerUri()}/${NAME}/${env.profiles}/${env.label || 'main'}/${name}`; + const url = `${buildConfigUri()}/${name}`; console.log(`Loading text resource from ${url}`); - const token = await oauth2.getAccessToken(); - const response = await fetch(`${url}`, { - headers: { - Authorization: `Bearer ${token}`, - Accept: 'plain/text', - }, - }); - - if (!response.ok) { - throw new Error(`HTTP error! Status: ${response.status}`); - } + const response = await makeHttpRequest(`${url}`, 'text/plain'); return await response.text(); }; // Loads a binary resource from the config-server. const loadBinaryResource = async (name) => { - const url = `${configServerUri()}/${NAME}/${env.profiles}/${env.label || 'main'}/${name}`; + const url = `${buildConfigUri()}/${name}`; console.log(`Loading binary resource from ${url}`); - const token = await oauth2.getAccessToken(); - const response = await fetch(`${url}`, { - headers: { - Authorization: `Bearer ${token}`, - Accept: 'application/octet-stream', - }, - }); - - if (!response.ok) { - throw new Error(`HTTP error! Status: ${response.status}`); - } + const response = await makeHttpRequest(`${url}`, 'application/octet-stream'); return response.blob(); };