mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-23 12:45:38 +00:00
fix#6247: Interpolate dynamic variables in path param (#6251)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 = [];
|
||||
|
||||
Reference in New Issue
Block a user