Skip to content

Commit

Permalink
bugfix(usebruno#2431) Refactor URL construction in Postman collection…
Browse files Browse the repository at this point in the history
… processing (usebruno#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.
  • Loading branch information
sanjai0py authored Sep 23, 2024
1 parent 858afdb commit eb33504
Showing 1 changed file with 54 additions and 13 deletions.
67 changes: 54 additions & 13 deletions packages/bruno-app/src/utils/importers/postman-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

const importPostmanV2CollectionItem = (brunoParent, item, parentAuth, options) => {
brunoParent.items = brunoParent.items || [];
const folderMap = {};
const requestMap = {};

each(item, (i) => {
if (isItemAFolder(i)) {
const baseFolderName = i.name;
Expand All @@ -86,28 +127,23 @@ const importPostmanV2CollectionItem = (brunoParent, item, parentAuth, options) =
} else {
if (i.request) {
const baseRequestName = i.name;
let requestName = baseRequestName;
let requestName = baseRequestName;
let count = 1;

while (requestMap[requestName]) {
requestName = `${baseRequestName}_${count}`;
count++;
}

let url = '';
if (typeof i.request.url === 'string') {
url = i.request.url;
} else {
url = get(i, 'request.url.raw') || '';
}

const url = constructUrl(i.request.url);

const brunoRequestItem = {
uid: uuid(),
name: requestName,
type: 'http-request',
request: {
url: url,
method: i.request.method,
method: i?.request?.method?.toUpperCase(),
auth: {
mode: 'none',
basic: null,
Expand Down Expand Up @@ -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({
uid: uuid(),
name: param.key,
value: param.value,
description: param.description,
value: param.value ?? '',
description: param.description ?? '',
type: 'path',
enabled: true
});
Expand Down

0 comments on commit eb33504

Please sign in to comment.