From ecf1ca69a9dafa90ae88499f064c668b6941dd87 Mon Sep 17 00:00:00 2001 From: Hassan_Wari <85742599+hassan254-prog@users.noreply.github.com> Date: Fri, 9 Aug 2024 10:54:11 +0300 Subject: [PATCH] feat(proxy): add fallback to proxy base_url (#1873) ## Describe your changes Continuing from this, https://github.com/NangoHQ/nango/pull/1770#discussion_r1526495126. We need to have a fallback incase `api_base_url_for_customer` field is not populated in the connection configuration. I have also added a unit test for this.. --------- Co-authored-by: Khaliq --- packages/shared/lib/services/proxy.service.ts | 15 ++++++- .../lib/services/proxy.service.unit.test.ts | 40 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/packages/shared/lib/services/proxy.service.ts b/packages/shared/lib/services/proxy.service.ts index 33bb83393f..cdb3f1c5c4 100644 --- a/packages/shared/lib/services/proxy.service.ts +++ b/packages/shared/lib/services/proxy.service.ts @@ -386,7 +386,20 @@ class ProxyService { const { connection } = config; const { template: { proxy: { base_url: templateApiBase } = {} } = {}, endpoint: apiEndpoint } = config; - const apiBase = config.baseUrlOverride || templateApiBase; + let apiBase = config.baseUrlOverride || templateApiBase; + + if (apiBase?.includes('${') && apiBase?.includes('||')) { + const connectionConfig = connection.connection_config; + const splitApiBase = apiBase.split(/\s*\|\|\s*/); + + if (!connectionConfig) { + apiBase = splitApiBase[1]; + } else { + const keyMatch = apiBase.match(/connectionConfig\.(\w+)/); + const index = keyMatch && keyMatch[1] && connectionConfig[keyMatch[1]] ? 0 : 1; + apiBase = splitApiBase[index]?.trim(); + } + } const base = apiBase?.substr(-1) === '/' ? apiBase.slice(0, -1) : apiBase; let endpoint = apiEndpoint.charAt(0) === '/' ? apiEndpoint.slice(1) : apiEndpoint; diff --git a/packages/shared/lib/services/proxy.service.unit.test.ts b/packages/shared/lib/services/proxy.service.unit.test.ts index 2ce3931fcd..1543e0ca57 100644 --- a/packages/shared/lib/services/proxy.service.unit.test.ts +++ b/packages/shared/lib/services/proxy.service.unit.test.ts @@ -425,6 +425,46 @@ describe('Proxy service Construct URL Tests', () => { expect(url).toBe('https://myinstanceurl.com/api/test'); }); + it('Should handle Proxy base URL interpolation where connection configuration param is present', () => { + const config = { + template: { + auth_mode: 'OAUTH2', + proxy: { + base_url: '${connectionConfig.api_base_url_for_customer} || https://api.gong.io' + } + }, + token: { apiKey: 'sweet-secret-token' }, + endpoint: '/api/test', + connection: { + connection_config: { api_base_url_for_customer: 'https://company-17.api.gong.io' } + } + }; + + const url = proxyService.constructUrl(config as unknown as ApplicationConstructedProxyConfiguration); + + expect(url).toBe('https://company-17.api.gong.io/api/test'); + }); + + it('Should handle Proxy base URL interpolation where connection configuration param is absent', () => { + const config = { + template: { + auth_mode: 'OAUTH2', + proxy: { + base_url: '${connectionConfig.api_base_url_for_customer}||https://api.gong.io' + } + }, + token: { apiKey: 'sweet-secret-token' }, + endpoint: '/api/test', + connection: { + environment_id: 1 + } + }; + + const url = proxyService.constructUrl(config as unknown as ApplicationConstructedProxyConfiguration); + + expect(url).toBe('https://api.gong.io/api/test'); + }); + it('Should strip away the auth token from the headers', () => { const headers = { Accept: 'application/json',