2022-03-18 00:13:35 +01:00
|
|
|
import each from 'lodash/each';
|
2022-03-17 22:29:10 +01:00
|
|
|
import find from 'lodash/find';
|
2022-03-22 13:48:20 +01:00
|
|
|
import isString from 'lodash/isString';
|
2022-03-19 14:03:16 +01:00
|
|
|
import map from 'lodash/map';
|
2022-03-18 20:35:43 +01:00
|
|
|
import filter from 'lodash/filter';
|
2022-03-22 13:48:20 +01:00
|
|
|
import sortBy from 'lodash/sortBy';
|
2022-03-18 14:08:49 +01:00
|
|
|
import cloneDeep from 'lodash/cloneDeep';
|
2022-03-17 22:29:10 +01:00
|
|
|
|
2022-03-22 13:48:20 +01:00
|
|
|
const replaceTabsWithSpaces = (str, numSpaces = 2) => {
|
|
|
|
if(!str || !str.length || !isString(str)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return str.replaceAll('\t', ' '.repeat(numSpaces));
|
|
|
|
};
|
|
|
|
|
2022-03-18 18:18:05 +01:00
|
|
|
export const addDepth = (items = []) => {
|
|
|
|
const depth = (itms, initialDepth) => {
|
|
|
|
each(itms, (i) => {
|
|
|
|
i.depth = initialDepth;
|
|
|
|
|
|
|
|
if(i.items && i.items.length) {
|
|
|
|
depth(i.items, initialDepth + 1);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
depth(items, 1);
|
|
|
|
};
|
|
|
|
|
2022-03-22 13:48:20 +01:00
|
|
|
export const sortItems = (collection) => {
|
|
|
|
const sort = (obj) => {
|
|
|
|
if(obj.items && obj.items.length) {
|
|
|
|
obj.items = sortBy(obj.items, 'type');
|
|
|
|
}
|
|
|
|
|
|
|
|
each(obj.items, (i) => sort(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
sort(collection);
|
|
|
|
};
|
|
|
|
|
2022-03-18 00:13:35 +01:00
|
|
|
export const flattenItems = (items = []) => {
|
|
|
|
const flattenedItems = [];
|
|
|
|
|
|
|
|
const flatten = (itms, flattened) => {
|
|
|
|
each(itms, (i) => {
|
|
|
|
flattened.push(i);
|
|
|
|
|
|
|
|
if(i.items && i.items.length) {
|
|
|
|
flatten(i.items, flattened);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
flatten(items, flattenedItems);
|
|
|
|
|
|
|
|
return flattenedItems;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const findItem = (items = [], itemUid) => {
|
|
|
|
return find(items, (i) => i.uid === itemUid);
|
|
|
|
};
|
|
|
|
|
2022-03-22 13:48:20 +01:00
|
|
|
|
2022-03-17 22:29:10 +01:00
|
|
|
export const findCollectionByUid = (collections, collectionUid) => {
|
|
|
|
return find(collections, (c) => c.uid === collectionUid);
|
|
|
|
};
|
2022-03-18 00:13:35 +01:00
|
|
|
|
|
|
|
export const findItemInCollection = (collection, itemUid) => {
|
|
|
|
let flattenedItems = flattenItems(collection.items);
|
|
|
|
|
|
|
|
return findItem(flattenedItems, itemUid);
|
2022-03-18 14:08:49 +01:00
|
|
|
}
|
|
|
|
|
2022-09-28 22:32:17 +02:00
|
|
|
export const findParentItemInCollection = (collection, itemUid) => {
|
|
|
|
let flattenedItems = flattenItems(collection.items);
|
|
|
|
|
|
|
|
return find(flattenedItems, (item) => {
|
|
|
|
return item.items && find(item.items, i => i.uid === itemUid);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-22 13:48:20 +01:00
|
|
|
export const recursivelyGetAllItemUids = (items = []) => {
|
|
|
|
let flattenedItems = flattenItems(items);
|
|
|
|
|
|
|
|
return map(flattenedItems, (i) => i.uid);
|
|
|
|
};
|
|
|
|
|
2022-03-18 22:48:15 +01:00
|
|
|
export const transformCollectionToSaveToIdb = (collection, options = {}) => {
|
2022-03-19 14:03:16 +01:00
|
|
|
const copyHeaders = (headers) => {
|
|
|
|
return map(headers, (header) => {
|
|
|
|
return {
|
|
|
|
uid: header.uid,
|
|
|
|
name: header.name,
|
|
|
|
value: header.value,
|
|
|
|
description: header.description,
|
|
|
|
enabled: header.enabled
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-03-18 14:08:49 +01:00
|
|
|
const copyItems = (sourceItems, destItems) => {
|
|
|
|
each(sourceItems, (si) => {
|
|
|
|
const di = {
|
|
|
|
uid: si.uid,
|
|
|
|
type: si.type
|
|
|
|
};
|
|
|
|
|
2022-03-18 22:48:15 +01:00
|
|
|
di.name = si.name;
|
2022-03-18 14:08:49 +01:00
|
|
|
|
2022-03-18 22:48:15 +01:00
|
|
|
// if items is draft, then take data from draft to save
|
|
|
|
if(!options.ignoreDraft && si.draft) {
|
2022-03-18 14:08:49 +01:00
|
|
|
if(si.draft.request) {
|
|
|
|
di.request = {
|
|
|
|
url: si.draft.request.url,
|
|
|
|
method: si.draft.request.method,
|
2022-03-19 14:03:16 +01:00
|
|
|
headers: copyHeaders(si.draft.request.headers),
|
2022-03-22 13:48:20 +01:00
|
|
|
body: {
|
|
|
|
mode: si.draft.request.body.mode,
|
|
|
|
content: replaceTabsWithSpaces(si.draft.request.body.content)
|
|
|
|
}
|
2022-03-18 14:08:49 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(si.request) {
|
|
|
|
di.request = {
|
|
|
|
url: si.request.url,
|
|
|
|
method: si.request.method,
|
2022-03-19 14:03:16 +01:00
|
|
|
headers: copyHeaders(si.request.headers),
|
2022-03-22 13:48:20 +01:00
|
|
|
body: {
|
|
|
|
mode: si.request.body.mode,
|
|
|
|
content: replaceTabsWithSpaces(si.request.body.content)
|
|
|
|
}
|
2022-03-18 14:08:49 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-22 13:48:20 +01:00
|
|
|
if(di.request && di.request.body.mode === 'json') {
|
|
|
|
di.request.body.content = replaceTabsWithSpaces(di.request.body.content);
|
|
|
|
}
|
|
|
|
|
2022-03-18 14:08:49 +01:00
|
|
|
destItems.push(di);
|
|
|
|
|
|
|
|
if(si.items && si.items.length) {
|
|
|
|
di.items = [];
|
|
|
|
copyItems(si.items, di.items);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const collectionToSave = {};
|
|
|
|
collectionToSave.name = collection.name;
|
|
|
|
collectionToSave.uid = collection.uid;
|
|
|
|
collectionToSave.userId = collection.userId;
|
2022-03-22 13:48:20 +01:00
|
|
|
collectionToSave.orgId = collection.orgId;
|
2022-03-18 14:08:49 +01:00
|
|
|
collectionToSave.environments = cloneDeep(collection.environments);
|
|
|
|
collectionToSave.items = [];
|
|
|
|
|
|
|
|
copyItems(collection.items, collectionToSave.items);
|
|
|
|
|
|
|
|
return collectionToSave;
|
2022-03-18 20:35:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// todo: optimize this
|
|
|
|
export const deleteItemInCollection = (itemUid, collection) => {
|
|
|
|
collection.items = filter(collection.items, (i) => i.uid !== itemUid);
|
|
|
|
|
|
|
|
let flattenedItems = flattenItems(collection.items);
|
|
|
|
|
|
|
|
each(flattenedItems, (i) => {
|
|
|
|
if(i.items && i.items.length) {
|
|
|
|
i.items = filter(i.items, (i) => i.uid !== itemUid);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2022-03-19 14:43:16 +01:00
|
|
|
|
|
|
|
export const isItemARequest = (item) => {
|
2022-09-28 22:32:17 +02:00
|
|
|
return item.hasOwnProperty('request')
|
|
|
|
&& ['http-request', 'graphql-request'].includes(item.type)
|
|
|
|
&& !item.items;
|
2022-03-19 14:43:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export const isItemAFolder = (item) => {
|
|
|
|
return !item.hasOwnProperty('request') && item.type === 'folder';
|
|
|
|
};
|