mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-07 08:34:15 +01:00
* 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:
parent
858afdbf03
commit
eb33504f19
@ -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
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user