bugfix(#2431) Refactor URL construction in Postman collection processing (#2445)

* Refactor URL construction in Postman collection processing

* Updated the constructUrl function and made it more loose. Also now when there is a param with its key as undefined we discard it.

* Handled the case when the url is an object and dosen't have a raw value.

* Added missing return.

* Removed the URL fragments

* Removed unused destructures.

* Minor changes.
This commit is contained in:
Sanjai Kumar 2024-09-23 12:22:03 +05:30 committed by GitHub
parent 858afdbf03
commit eb33504f19
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -54,13 +54,54 @@ const convertV21Auth = (array) => {
}, {}); }, {});
}; };
const constructUrlFromParts = (url) => {
const { protocol = 'http', host, path, port, query, hash } = url || {};
const hostStr = Array.isArray(host) ? host.filter(Boolean).join('.') : host || '';
const pathStr = Array.isArray(path) ? path.filter(Boolean).join('/') : path || '';
const portStr = port ? `:${port}` : '';
const queryStr =
query && Array.isArray(query) && query.length > 0
? `?${query
.filter((q) => q.key)
.map((q) => `${q.key}=${q.value || ''}`)
.join('&')}`
: '';
const urlStr = `${protocol}://${hostStr}${portStr}${pathStr ? `/${pathStr}` : ''}${queryStr}`;
return urlStr;
};
const constructUrl = (url) => {
if (!url) return '';
if (typeof url === 'string') {
return url;
}
if (typeof url === 'object') {
const { raw } = url;
if (raw && typeof raw === 'string') {
// If the raw URL contains url-fragments remove it
if (raw.includes('#')) {
return raw.split('#')[0]; // Returns the part of raw URL without the url-fragment part.
}
return raw;
}
// If no raw value exists, construct the URL from parts
return constructUrlFromParts(url);
}
return '';
};
let translationLog = {}; let translationLog = {};
const importPostmanV2CollectionItem = (brunoParent, item, parentAuth, options) => { const importPostmanV2CollectionItem = (brunoParent, item, parentAuth, options) => {
brunoParent.items = brunoParent.items || []; brunoParent.items = brunoParent.items || [];
const folderMap = {}; const folderMap = {};
const requestMap = {}; const requestMap = {};
each(item, (i) => { each(item, (i) => {
if (isItemAFolder(i)) { if (isItemAFolder(i)) {
const baseFolderName = i.name; const baseFolderName = i.name;
@ -86,20 +127,15 @@ const importPostmanV2CollectionItem = (brunoParent, item, parentAuth, options) =
} else { } else {
if (i.request) { if (i.request) {
const baseRequestName = i.name; const baseRequestName = i.name;
let requestName = baseRequestName; let requestName = baseRequestName;
let count = 1; let count = 1;
while (requestMap[requestName]) { while (requestMap[requestName]) {
requestName = `${baseRequestName}_${count}`; requestName = `${baseRequestName}_${count}`;
count++; count++;
} }
let url = ''; const url = constructUrl(i.request.url);
if (typeof i.request.url === 'string') {
url = i.request.url;
} else {
url = get(i, 'request.url.raw') || '';
}
const brunoRequestItem = { const brunoRequestItem = {
uid: uuid(), uid: uuid(),
@ -107,7 +143,7 @@ const importPostmanV2CollectionItem = (brunoParent, item, parentAuth, options) =
type: 'http-request', type: 'http-request',
request: { request: {
url: url, url: url,
method: i.request.method, method: i?.request?.method?.toUpperCase(),
auth: { auth: {
mode: 'none', mode: 'none',
basic: null, basic: null,
@ -313,12 +349,17 @@ const importPostmanV2CollectionItem = (brunoParent, item, parentAuth, options) =
}); });
}); });
each(get(i, 'request.url.variable'), (param) => { each(get(i, 'request.url.variable', []), (param) => {
if (!param.key) {
// If no key, skip this iteration and discard the param
return;
}
brunoRequestItem.request.params.push({ brunoRequestItem.request.params.push({
uid: uuid(), uid: uuid(),
name: param.key, name: param.key,
value: param.value, value: param.value ?? '',
description: param.description, description: param.description ?? '',
type: 'path', type: 'path',
enabled: true enabled: true
}); });