Skip to content

Commit

Permalink
feat(proxy): add fallback to proxy base_url (NangoHQ#1873)
Browse files Browse the repository at this point in the history
## Describe your changes
Continuing from this,
NangoHQ#1770 (comment). 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 <[email protected]>
  • Loading branch information
hassan254-prog and khaliqgant authored Aug 9, 2024
1 parent f0e05be commit ecf1ca6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/shared/lib/services/proxy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 40 additions & 0 deletions packages/shared/lib/services/proxy.service.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit ecf1ca6

Please sign in to comment.