diff --git a/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/GenerateCodeItem/index.js b/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/GenerateCodeItem/index.js index 660d7ea91..c9943f9df 100644 --- a/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/GenerateCodeItem/index.js +++ b/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/GenerateCodeItem/index.js @@ -95,7 +95,8 @@ const GenerateCodeItem = ({ collectionUid, item, onClose, isExample = false, exa // interpolate the path params const finalUrl = interpolateUrlPathParams( interpolatedUrl, - requestData.params + requestData.params, + variables ); // Get the full language object based on current preferences diff --git a/packages/bruno-app/src/utils/url/index.js b/packages/bruno-app/src/utils/url/index.js index 268170a4b..46076a5eb 100644 --- a/packages/bruno-app/src/utils/url/index.js +++ b/packages/bruno-app/src/utils/url/index.js @@ -98,9 +98,9 @@ export const interpolateUrl = ({ url, variables }) => { return interpolate(url, variables); }; -export const interpolateUrlPathParams = (url, params) => { +export const interpolateUrlPathParams = (url, params, variables = {}) => { const getInterpolatedBasePath = (pathname, params) => { - return pathname + let replacedPathname = pathname .split('/') .map((segment) => { // traditional path parameters @@ -137,6 +137,8 @@ export const interpolateUrlPathParams = (url, params) => { return result; }) .join('/'); + + return interpolate(replacedPathname, variables); }; let uri; diff --git a/packages/bruno-app/src/utils/url/index.spec.js b/packages/bruno-app/src/utils/url/index.spec.js index db6af815a..dfd18c96c 100644 --- a/packages/bruno-app/src/utils/url/index.spec.js +++ b/packages/bruno-app/src/utils/url/index.spec.js @@ -319,6 +319,29 @@ describe('Url Utils - interpolateUrl, interpolateUrlPathParams', () => { expect(result).toEqual(expectedUrl); }); + it('should interpolate path params correctly', () => { + const url = 'https://example.com/api/:id/path/:user'; + const params = [{ name: 'id', type: 'path', enabled: true, value: '123' }, { name: 'user', type: 'path', enabled: true, value: '{{user}}' }]; + const variables = { user: 'John' }; + const expectedUrl = 'https://example.com/api/123/path/John'; + + const result = interpolateUrlPathParams(url, params, variables); + + expect(result).toEqual(expectedUrl); + }); + + it('should interpolate url and path params correctly', () => { + const url = '{{host}}/api/:id/path/:user?foo={{foo}}&bar={{bar}}&baz={{process.env.baz}}'; + const params = [{ name: 'id', type: 'path', enabled: true, value: '123' }, { name: 'user', type: 'path', enabled: true, value: '{{user}}' }]; + const variables = { 'host': 'https://example.com', 'foo': 'foo_value', 'bar': 'bar_value', 'process.env.baz': 'baz_value', 'user': 'John' }; + const expectedUrl = 'https://example.com/api/123/path/John?foo=foo_value&bar=bar_value&baz=baz_value'; + + const intermediateResult = interpolateUrl({ url, variables }); + const result = interpolateUrlPathParams(intermediateResult, params, variables); + + expect(result).toEqual(expectedUrl); + }); + it('should handle empty params', () => { const url = 'https://example.com/api'; const params = [];