mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-15 20:01:28 +00:00
Fix/example naming (#6002)
* fix: example naming * fix: request not being saved when initialized with empty url * remove check for method * fix: improve the logic for get initial name * fix test
This commit is contained in:
@@ -8,6 +8,7 @@ import { uuid } from 'utils/common';
|
||||
import toast from 'react-hot-toast';
|
||||
import CreateExampleModal from 'components/ResponseExample/CreateExampleModal';
|
||||
import { getBodyType } from 'utils/responseBodyProcessor';
|
||||
import { getInitialExampleName } from 'utils/collections/index';
|
||||
import classnames from 'classnames';
|
||||
import StyledWrapper from './StyledWrapper';
|
||||
|
||||
@@ -23,33 +24,6 @@ const ResponseBookmark = ({ item, collection, responseSize }) => {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Generate initial name for the example
|
||||
const getInitialExampleName = () => {
|
||||
const baseName = 'example';
|
||||
const existingExamples = item.draft?.examples || item.examples || [];
|
||||
|
||||
// Check if any existing example has the same base name
|
||||
const hasSameBaseName = existingExamples.some((example) => {
|
||||
const exampleName = example.name || '';
|
||||
return exampleName === baseName || exampleName.startsWith(baseName);
|
||||
});
|
||||
|
||||
if (!hasSameBaseName) {
|
||||
return baseName;
|
||||
}
|
||||
|
||||
// Find the highest existing counter
|
||||
let maxCounter = 0;
|
||||
existingExamples.forEach((example) => {
|
||||
const exampleName = example.name || '';
|
||||
if (exampleName.startsWith(baseName)) {
|
||||
maxCounter++;
|
||||
}
|
||||
});
|
||||
|
||||
return `${baseName} (${maxCounter})`;
|
||||
};
|
||||
|
||||
const handleSaveClick = () => {
|
||||
if (!response || response.error) {
|
||||
toast.error('No valid response to save as example');
|
||||
@@ -147,7 +121,7 @@ const ResponseBookmark = ({ item, collection, responseSize }) => {
|
||||
onClose={() => setShowSaveResponseExampleModal(false)}
|
||||
onSave={saveAsExample}
|
||||
title="Save Response as Example"
|
||||
initialName={getInitialExampleName()}
|
||||
initialName={getInitialExampleName(item)}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
|
||||
@@ -33,7 +33,7 @@ import ExampleItem from './ExampleItem';
|
||||
import { scrollToTheActiveTab } from 'utils/tabs';
|
||||
import { isTabForItemActive as isTabForItemActiveSelector, isTabForItemPresent as isTabForItemPresentSelector } from 'src/selectors/tab';
|
||||
import { isEqual } from 'lodash';
|
||||
import { calculateDraggedItemNewPathname } from 'utils/collections/index';
|
||||
import { calculateDraggedItemNewPathname, getInitialExampleName } from 'utils/collections/index';
|
||||
import { sortByNameThenSequence } from 'utils/common/index';
|
||||
import CreateExampleModal from 'components/ResponseExample/CreateExampleModal';
|
||||
|
||||
@@ -346,19 +346,6 @@ const CollectionItem = ({ item, collectionUid, collectionPathname, searchText })
|
||||
setCreateExampleModalOpen(false);
|
||||
};
|
||||
|
||||
const getInitialExampleName = () => {
|
||||
const baseName = 'example';
|
||||
const existingExamples = item.draft?.examples || item.examples || [];
|
||||
let maxCounter = 0;
|
||||
existingExamples.forEach((example) => {
|
||||
const exampleName = example.name || '';
|
||||
if (exampleName.startsWith(baseName)) {
|
||||
maxCounter++;
|
||||
}
|
||||
});
|
||||
return `${baseName} (${maxCounter})`;
|
||||
};
|
||||
|
||||
const folderItems = sortByNameThenSequence(filter(item.items, (i) => isItemAFolder(i)));
|
||||
const requestItems = sortItemsBySequence(filter(item.items, (i) => isItemARequest(i)));
|
||||
|
||||
@@ -439,7 +426,7 @@ const CollectionItem = ({ item, collectionUid, collectionPathname, searchText })
|
||||
onClose={() => setCreateExampleModalOpen(false)}
|
||||
onSave={handleCreateExample}
|
||||
title="Create Response Example"
|
||||
initialName={getInitialExampleName()}
|
||||
initialName={getInitialExampleName(item)}
|
||||
/>
|
||||
<div
|
||||
className={itemRowClassName}
|
||||
|
||||
@@ -1393,3 +1393,27 @@ export const transformExampleToDraft = (example, newExample) => {
|
||||
|
||||
return exampleToDraft;
|
||||
};
|
||||
|
||||
/**
|
||||
* Generate an initial name for a new response example
|
||||
* @param {Object} item - The request item that will contain the example
|
||||
* @returns {string} - The suggested name for the new example
|
||||
*/
|
||||
export const getInitialExampleName = (item) => {
|
||||
const baseName = 'example';
|
||||
const existingExamples = item.draft?.examples || item.examples || [];
|
||||
const existingNames = new Set(existingExamples.map((example) => example.name || '').filter(Boolean));
|
||||
|
||||
if (!existingNames.has(baseName)) {
|
||||
return baseName;
|
||||
}
|
||||
|
||||
let counter = 1;
|
||||
while (true) {
|
||||
const candidateName = `${baseName} (${counter})`;
|
||||
if (!existingNames.has(candidateName)) {
|
||||
return candidateName;
|
||||
}
|
||||
counter++;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -44,14 +44,9 @@ const jsonToExampleBru = (json) => {
|
||||
// Request block
|
||||
bru += '\nrequest: {\n';
|
||||
|
||||
if (url) {
|
||||
bru += ` url: ${url}\n`;
|
||||
}
|
||||
bru += ` url: ${url}\n`;
|
||||
|
||||
// Add method field right after url
|
||||
if (method) {
|
||||
bru += ` method: ${method}\n`;
|
||||
}
|
||||
bru += ` method: ${method}\n`;
|
||||
|
||||
// Add mode field inside request block, right after method
|
||||
if (request && request.body && request.body.mode) {
|
||||
|
||||
@@ -204,15 +204,16 @@ get {
|
||||
type: 'http'
|
||||
},
|
||||
http: {
|
||||
method: 'get',
|
||||
url: 'https://api.example.com/test'
|
||||
url: 'https://api.example.com/test',
|
||||
method: 'get'
|
||||
},
|
||||
examples: [
|
||||
{
|
||||
name: 'Example Request',
|
||||
description: 'A simple example',
|
||||
request: {
|
||||
url: 'https://api.example.com/example'
|
||||
url: 'https://api.example.com/example',
|
||||
method: 'get'
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -233,6 +234,7 @@ example {
|
||||
|
||||
request: {
|
||||
url: https://api.example.com/example
|
||||
method: get
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
Reference in New Issue
Block a user