From eb33504f1962e30236c34ed842c34570874b2f55 Mon Sep 17 00:00:00 2001 From: Sanjai Kumar <84461672+sanjai0py@users.noreply.github.com> Date: Mon, 23 Sep 2024 12:22:03 +0530 Subject: [PATCH] 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. --- .../src/utils/importers/postman-collection.js | 67 +++++++++++++++---- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/packages/bruno-app/src/utils/importers/postman-collection.js b/packages/bruno-app/src/utils/importers/postman-collection.js index 4486e0efc2..053a557230 100644 --- a/packages/bruno-app/src/utils/importers/postman-collection.js +++ b/packages/bruno-app/src/utils/importers/postman-collection.js @@ -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; @@ -86,20 +127,15 @@ 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(), @@ -107,7 +143,7 @@ const importPostmanV2CollectionItem = (brunoParent, item, parentAuth, options) = type: 'http-request', request: { url: url, - method: i.request.method, + method: i?.request?.method?.toUpperCase(), auth: { mode: 'none', 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({ uid: uuid(), name: param.key, - value: param.value, - description: param.description, + value: param.value ?? '', + description: param.description ?? '', type: 'path', enabled: true });