Feat/improved path params (#2357)

* feat: path parameters (#484)

* add path parameters on bruno-app

* add path parameters on bruno-cli

* fix bruno-schema testing

* fix generate request code not replace path parameter value

---------

Co-authored-by: game5413 <febryanph10@gmail.com>
Co-authored-by: Anoop M D <anoop.md1421@gmail.com>

* feat: Refactor request parameter handling

- Update prepare-request.js to filter and rename 'paths' to 'params' with type 'path'
- Remove 'paths' from export.js and interpolate-vars.js
- Update bru.js to use 'params' instead of 'path'
- Update requestSchema in index.js to use 'keyValueWithTypeSchema' for 'params'

Co-authored-by: game5413 <febryanph10@gmail.com>
Co-authored-by: Anoop M D <anoop.md1421@gmail.com>

* feat: Refactor request parameter handling

* refactor: changes form the review

* refactor: Refactor transformItemsInCollection handling

* refactor: Refactor improved export/import functionalities

* refactor: Remove console.log statement in bruToJson.js

---------

Co-authored-by: game5413 <37659721+game5413@users.noreply.github.com>
Co-authored-by: game5413 <febryanph10@gmail.com>
Co-authored-by: Anoop M D <anoop.md1421@gmail.com>
This commit is contained in:
Sanjai Kumar
2024-05-30 15:49:14 +05:30
committed by GitHub
parent 77b1e6d738
commit abfd14a306
31 changed files with 691 additions and 163 deletions

View File

@@ -1,5 +1,5 @@
const { interpolate } = require('@usebruno/common');
const { each, forOwn, cloneDeep } = require('lodash');
const { each, forOwn, cloneDeep, find } = require('lodash');
const getContentType = (headers = {}) => {
let contentType = '';
@@ -86,6 +86,36 @@ const interpolateVars = (request, envVars = {}, collectionVariables = {}, proces
param.value = _interpolate(param.value);
});
if (request.params.length) {
let url = request.url;
if (!url.startsWith('http://') && !url.startsWith('https://')) {
url = `http://${url}`;
}
try {
url = new URL(url);
} catch (e) {
throw { message: 'Invalid URL format', originalError: e.message };
}
const urlPaths = url.pathname
.split('/')
.filter((path) => path !== '')
.map((path) => {
if (path[0] !== ':') {
return '/' + path;
} else {
const name = path.slice(1);
const existingPathParam = request.params.find((param) => param.type === 'path' && param.name === name);
return existingPathParam ? '/' + existingPathParam.value : '';
}
})
.join('');
request.url = url.origin + urlPaths + url.search;
}
if (request.proxy) {
request.proxy.protocol = _interpolate(request.proxy.protocol);
request.proxy.hostname = _interpolate(request.proxy.hostname);

View File

@@ -29,7 +29,8 @@ const prepareRequest = (request, collectionRoot) => {
let axiosRequest = {
method: request.method,
url: request.url,
headers: headers
headers: headers,
paths: request.paths
};
const collectionAuth = get(collectionRoot, 'request.auth');

View File

@@ -13,7 +13,7 @@ const collectionBruToJson = (bru) => {
const transformedJson = {
request: {
params: _.get(json, 'query', []),
params: _.get(json, 'params', []),
headers: _.get(json, 'headers', []),
auth: _.get(json, 'auth', {}),
script: _.get(json, 'script', {}),
@@ -60,7 +60,7 @@ const bruToJson = (bru) => {
method: _.upperCase(_.get(json, 'http.method')),
url: _.get(json, 'http.url'),
auth: _.get(json, 'auth', {}),
params: _.get(json, 'query', []),
params: _.get(json, 'params', []),
headers: _.get(json, 'headers', []),
body: _.get(json, 'body', {}),
vars: _.get(json, 'vars', []),