feat: hardening seq number functionality

This commit is contained in:
Anoop M D 2023-01-21 23:22:45 +05:30
parent e513694912
commit fff540010e
6 changed files with 34 additions and 6 deletions

View File

@ -516,9 +516,13 @@ export const newHttpRequest = (params) => (dispatch, getState) => {
} }
}; };
// itemUid is null when we are creating a new request at the root level
const filename = resolveRequestFilename(requestName); const filename = resolveRequestFilename(requestName);
if (!itemUid) { if (!itemUid) {
const reqWithSameNameExists = find(collection.items, (i) => i.type !== 'folder' && trim(i.filename) === trim(filename)); const reqWithSameNameExists = find(collection.items, (i) => i.type !== 'folder' && trim(i.filename) === trim(filename));
item.seq = collectionCopy.items ? collectionCopy.items.length : 1;
item.seq = item.seq + 1;
if (!reqWithSameNameExists) { if (!reqWithSameNameExists) {
const fullName = `${collection.pathname}${PATH_SEPARATOR}${filename}`; const fullName = `${collection.pathname}${PATH_SEPARATOR}${filename}`;
const { ipcRenderer } = window; const { ipcRenderer } = window;
@ -531,6 +535,8 @@ export const newHttpRequest = (params) => (dispatch, getState) => {
const currentItem = findItemInCollection(collection, itemUid); const currentItem = findItemInCollection(collection, itemUid);
if (currentItem) { if (currentItem) {
const reqWithSameNameExists = find(currentItem.items, (i) => i.type !== 'folder' && trim(i.filename) === trim(filename)); const reqWithSameNameExists = find(currentItem.items, (i) => i.type !== 'folder' && trim(i.filename) === trim(filename));
item.seq = currentItem.items ? currentItem.items.length : 1;
item.seq = item.seq + 1;
if (!reqWithSameNameExists) { if (!reqWithSameNameExists) {
const fullName = `${currentItem.pathname}${PATH_SEPARATOR}${filename}`; const fullName = `${currentItem.pathname}${PATH_SEPARATOR}${filename}`;
const { ipcRenderer } = window; const { ipcRenderer } = window;

View File

@ -256,11 +256,11 @@ export const transformCollectionToSaveToIdb = (collection, options = {}) => {
each(sourceItems, (si) => { each(sourceItems, (si) => {
const di = { const di = {
uid: si.uid, uid: si.uid,
type: si.type type: si.type,
name: si.name,
seq: si.seq
}; };
di.name = si.name;
// if items is draft, then take data from draft to save // if items is draft, then take data from draft to save
// The condition "!options.ignoreDraft" may appear confusing // The condition "!options.ignoreDraft" may appear confusing
// When saving a collection, this option allows the caller to specify to ignore any draft changes while still saving rest of the collection. // When saving a collection, this option allows the caller to specify to ignore any draft changes while still saving rest of the collection.
@ -337,6 +337,7 @@ export const transformRequestToSaveToFilesystem = (item) => {
uid: _item.uid, uid: _item.uid,
type: _item.type, type: _item.type,
name: _item.name, name: _item.name,
seq: _item.seq,
request: { request: {
method: _item.request.method, method: _item.request.method,
url: _item.request.url, url: _item.request.url,

View File

@ -1,6 +1,6 @@
import fileDialog from 'file-dialog'; import fileDialog from 'file-dialog';
import { BrunoError } from 'utils/common/error'; import { BrunoError } from 'utils/common/error';
import { validateSchema, updateUidsInCollection } from './common'; import { validateSchema, updateUidsInCollection, hydrateSeqInCollection } from './common';
const readFile = (files) => { const readFile = (files) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -28,6 +28,7 @@ const importCollection = () => {
fileDialog({ accept: 'application/json' }) fileDialog({ accept: 'application/json' })
.then(readFile) .then(readFile)
.then(parseJsonCollection) .then(parseJsonCollection)
.then(hydrateSeqInCollection)
.then(updateUidsInCollection) .then(updateUidsInCollection)
.then(validateSchema) .then(validateSchema)
.then((collection) => resolve(collection)) .then((collection) => resolve(collection))

View File

@ -4,6 +4,7 @@ import get from 'lodash/get';
import cloneDeep from 'lodash/cloneDeep'; import cloneDeep from 'lodash/cloneDeep';
import { uuid } from 'utils/common'; import { uuid } from 'utils/common';
import { isItemARequest } from 'utils/collections';
import { collectionSchema } from '@usebruno/schema'; import { collectionSchema } from '@usebruno/schema';
import { BrunoError } from 'utils/common/error'; import { BrunoError } from 'utils/common/error';
@ -51,3 +52,21 @@ export const updateUidsInCollection = (_collection) => {
return collection; return collection;
}; };
export const hydrateSeqInCollection = (collection) => {
const hydrateSeq = (items = []) => {
let index = 1;
each(items, (item) => {
if(isItemARequest(item) && !item.seq) {
item.seq = index;
index++;
}
if (item.items && item.items.length) {
hydrateSeq(item.items);
}
});
};
hydrateSeq(collection.items);
return collection;
}

View File

@ -3,7 +3,7 @@ import get from 'lodash/get';
import fileDialog from 'file-dialog'; import fileDialog from 'file-dialog';
import { uuid } from 'utils/common'; import { uuid } from 'utils/common';
import { BrunoError } from 'utils/common/error'; import { BrunoError } from 'utils/common/error';
import { validateSchema, updateUidsInCollection } from './common'; import { validateSchema, hydrateSeqInCollection } from './common';
const readFile = (files) => { const readFile = (files) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -178,6 +178,7 @@ const importCollection = () => {
fileDialog({ accept: 'application/json' }) fileDialog({ accept: 'application/json' })
.then(readFile) .then(readFile)
.then(parsePostmanCollection) .then(parsePostmanCollection)
.then(hydrateSeqInCollection)
.then(validateSchema) .then(validateSchema)
.then((collection) => resolve(collection)) .then((collection) => resolve(collection))
.catch((err) => { .catch((err) => {

View File

@ -70,7 +70,7 @@ const itemSchema = Yup.object({
items: Yup.lazy(() => Yup.array().of(itemSchema)), items: Yup.lazy(() => Yup.array().of(itemSchema)),
filename: Yup.string().max(1024, 'filename cannot be more than 1024 characters').nullable(), filename: Yup.string().max(1024, 'filename cannot be more than 1024 characters').nullable(),
pathname: Yup.string().max(1024, 'pathname cannot be more than 1024 characters').nullable() pathname: Yup.string().max(1024, 'pathname cannot be more than 1024 characters').nullable()
}).noUnknown(true); }).noUnknown(true).strict();
const collectionSchema = Yup.object({ const collectionSchema = Yup.object({
version: Yup.string().oneOf(['1']).required('version is required'), version: Yup.string().oneOf(['1']).required('version is required'),