mirror of
https://github.com/usebruno/bruno.git
synced 2025-06-21 20:41:41 +02:00
added file saving + added export of collection var
This commit is contained in:
parent
d145275172
commit
542735e220
@ -1,5 +1,5 @@
|
|||||||
import { BrunoError } from 'utils/common/error';
|
|
||||||
import map from 'lodash/map';
|
import map from 'lodash/map';
|
||||||
|
import * as FileSaver from 'file-saver';
|
||||||
import { deleteSecretsInEnvs, deleteUidsInEnvs, deleteUidsInItems } from 'utils/collections/export';
|
import { deleteSecretsInEnvs, deleteUidsInEnvs, deleteUidsInItems } from 'utils/collections/export';
|
||||||
|
|
||||||
export const exportCollection = (collection) => {
|
export const exportCollection = (collection) => {
|
||||||
@ -15,6 +15,37 @@ export const exportCollection = (collection) => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const generateCollectionVars = (collection) => {
|
||||||
|
const pattern = /{{[^{}]+}}/g;
|
||||||
|
let listOfVars = [];
|
||||||
|
|
||||||
|
const findOccurrences = (obj, results) => {
|
||||||
|
if (typeof obj === 'object') {
|
||||||
|
if (Array.isArray(obj)) {
|
||||||
|
obj.forEach((item) => findOccurrences(item, results));
|
||||||
|
} else {
|
||||||
|
for (const key in obj) {
|
||||||
|
findOccurrences(obj[key], results);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (typeof obj === 'string') {
|
||||||
|
obj.replace(pattern, (match) => {
|
||||||
|
results.push(match.replace(/{{|}}/g, ''));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
findOccurrences(collection, listOfVars);
|
||||||
|
|
||||||
|
const finalArrayOfVars = [...new Set(listOfVars)];
|
||||||
|
|
||||||
|
return finalArrayOfVars.map((variable) => ({
|
||||||
|
key: variable,
|
||||||
|
value: '',
|
||||||
|
type: 'default'
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
const generateEventSection = (item) => {
|
const generateEventSection = (item) => {
|
||||||
const eventArray = [];
|
const eventArray = [];
|
||||||
if (item.request.tests.length) {
|
if (item.request.tests.length) {
|
||||||
@ -108,16 +139,43 @@ export const exportCollection = (collection) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const generateAuth = (itemAuth) => {
|
||||||
|
switch (itemAuth) {
|
||||||
|
case 'bearer':
|
||||||
|
return {
|
||||||
|
type: 'bearer',
|
||||||
|
bearer: {
|
||||||
|
key: 'token',
|
||||||
|
value: itemAuth.bearer.token,
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
case 'basic': {
|
||||||
|
return {
|
||||||
|
type: 'basic',
|
||||||
|
basic: [
|
||||||
|
{
|
||||||
|
key: 'password',
|
||||||
|
value: itemAuth.basic.password,
|
||||||
|
type: 'string'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'username',
|
||||||
|
value: itemAuth.basic.username,
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const generateRequestSection = (itemRequest) => {
|
const generateRequestSection = (itemRequest) => {
|
||||||
const requestObject = {
|
const requestObject = {
|
||||||
method: itemRequest.method,
|
method: itemRequest.method,
|
||||||
header: generateHeaders(itemRequest.headers),
|
header: generateHeaders(itemRequest.headers),
|
||||||
url: {
|
url: itemRequest.url,
|
||||||
raw: itemRequest.url,
|
auth: generateAuth(itemRequest.auth)
|
||||||
protocol: itemRequest.url.split('://')[0]
|
|
||||||
}
|
|
||||||
// host: TODO
|
|
||||||
// path: TODO
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (itemRequest.body.mode != 'none') {
|
if (itemRequest.body.mode != 'none') {
|
||||||
@ -145,7 +203,13 @@ export const exportCollection = (collection) => {
|
|||||||
const collectionToExport = {};
|
const collectionToExport = {};
|
||||||
collectionToExport.info = generateInfoSection();
|
collectionToExport.info = generateInfoSection();
|
||||||
collectionToExport.item = generateItemSection(collection.items);
|
collectionToExport.item = generateItemSection(collection.items);
|
||||||
console.log(collectionToExport);
|
collectionToExport.variable = generateCollectionVars(collection);
|
||||||
|
|
||||||
|
const fileName = `${collection.name}.json`;
|
||||||
|
const fileBlob = new Blob([JSON.stringify(collection, null, 2)], { type: 'application/json' });
|
||||||
|
|
||||||
|
FileSaver.saveAs(fileBlob, fileName);
|
||||||
|
// console.log(collectionToExport);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default exportCollection;
|
export default exportCollection;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user