Skip to content

Commit f3cb0d4

Browse files
authored
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
1 parent 7b18388 commit f3cb0d4

File tree

5 files changed

+35
-53
lines changed

5 files changed

+35
-53
lines changed

packages/bruno-app/src/components/ResponsePane/ResponseBookmark/index.js

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { uuid } from 'utils/common';
88
import toast from 'react-hot-toast';
99
import CreateExampleModal from 'components/ResponseExample/CreateExampleModal';
1010
import { getBodyType } from 'utils/responseBodyProcessor';
11+
import { getInitialExampleName } from 'utils/collections/index';
1112
import classnames from 'classnames';
1213
import StyledWrapper from './StyledWrapper';
1314

@@ -23,33 +24,6 @@ const ResponseBookmark = ({ item, collection, responseSize }) => {
2324
return null;
2425
}
2526

26-
// Generate initial name for the example
27-
const getInitialExampleName = () => {
28-
const baseName = 'example';
29-
const existingExamples = item.draft?.examples || item.examples || [];
30-
31-
// Check if any existing example has the same base name
32-
const hasSameBaseName = existingExamples.some((example) => {
33-
const exampleName = example.name || '';
34-
return exampleName === baseName || exampleName.startsWith(baseName);
35-
});
36-
37-
if (!hasSameBaseName) {
38-
return baseName;
39-
}
40-
41-
// Find the highest existing counter
42-
let maxCounter = 0;
43-
existingExamples.forEach((example) => {
44-
const exampleName = example.name || '';
45-
if (exampleName.startsWith(baseName)) {
46-
maxCounter++;
47-
}
48-
});
49-
50-
return `${baseName} (${maxCounter})`;
51-
};
52-
5327
const handleSaveClick = () => {
5428
if (!response || response.error) {
5529
toast.error('No valid response to save as example');
@@ -147,7 +121,7 @@ const ResponseBookmark = ({ item, collection, responseSize }) => {
147121
onClose={() => setShowSaveResponseExampleModal(false)}
148122
onSave={saveAsExample}
149123
title="Save Response as Example"
150-
initialName={getInitialExampleName()}
124+
initialName={getInitialExampleName(item)}
151125
/>
152126
</>
153127
);

packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/index.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import ExampleItem from './ExampleItem';
3333
import { scrollToTheActiveTab } from 'utils/tabs';
3434
import { isTabForItemActive as isTabForItemActiveSelector, isTabForItemPresent as isTabForItemPresentSelector } from 'src/selectors/tab';
3535
import { isEqual } from 'lodash';
36-
import { calculateDraggedItemNewPathname } from 'utils/collections/index';
36+
import { calculateDraggedItemNewPathname, getInitialExampleName } from 'utils/collections/index';
3737
import { sortByNameThenSequence } from 'utils/common/index';
3838
import CreateExampleModal from 'components/ResponseExample/CreateExampleModal';
3939

@@ -346,19 +346,6 @@ const CollectionItem = ({ item, collectionUid, collectionPathname, searchText })
346346
setCreateExampleModalOpen(false);
347347
};
348348

349-
const getInitialExampleName = () => {
350-
const baseName = 'example';
351-
const existingExamples = item.draft?.examples || item.examples || [];
352-
let maxCounter = 0;
353-
existingExamples.forEach((example) => {
354-
const exampleName = example.name || '';
355-
if (exampleName.startsWith(baseName)) {
356-
maxCounter++;
357-
}
358-
});
359-
return `${baseName} (${maxCounter})`;
360-
};
361-
362349
const folderItems = sortByNameThenSequence(filter(item.items, (i) => isItemAFolder(i)));
363350
const requestItems = sortItemsBySequence(filter(item.items, (i) => isItemARequest(i)));
364351

@@ -439,7 +426,7 @@ const CollectionItem = ({ item, collectionUid, collectionPathname, searchText })
439426
onClose={() => setCreateExampleModalOpen(false)}
440427
onSave={handleCreateExample}
441428
title="Create Response Example"
442-
initialName={getInitialExampleName()}
429+
initialName={getInitialExampleName(item)}
443430
/>
444431
<div
445432
className={itemRowClassName}

packages/bruno-app/src/utils/collections/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,3 +1393,27 @@ export const transformExampleToDraft = (example, newExample) => {
13931393

13941394
return exampleToDraft;
13951395
};
1396+
1397+
/**
1398+
* Generate an initial name for a new response example
1399+
* @param {Object} item - The request item that will contain the example
1400+
* @returns {string} - The suggested name for the new example
1401+
*/
1402+
export const getInitialExampleName = (item) => {
1403+
const baseName = 'example';
1404+
const existingExamples = item.draft?.examples || item.examples || [];
1405+
const existingNames = new Set(existingExamples.map((example) => example.name || '').filter(Boolean));
1406+
1407+
if (!existingNames.has(baseName)) {
1408+
return baseName;
1409+
}
1410+
1411+
let counter = 1;
1412+
while (true) {
1413+
const candidateName = `${baseName} (${counter})`;
1414+
if (!existingNames.has(candidateName)) {
1415+
return candidateName;
1416+
}
1417+
counter++;
1418+
}
1419+
};

packages/bruno-lang/v2/src/example/jsonToBru.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,9 @@ const jsonToExampleBru = (json) => {
4444
// Request block
4545
bru += '\nrequest: {\n';
4646

47-
if (url) {
48-
bru += ` url: ${url}\n`;
49-
}
47+
bru += ` url: ${url}\n`;
5048

51-
// Add method field right after url
52-
if (method) {
53-
bru += ` method: ${method}\n`;
54-
}
49+
bru += ` method: ${method}\n`;
5550

5651
// Add mode field inside request block, right after method
5752
if (request && request.body && request.body.mode) {

packages/bruno-lang/v2/tests/examples/examples.spec.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,16 @@ get {
204204
type: 'http'
205205
},
206206
http: {
207-
method: 'get',
208-
url: 'https://api.example.com/test'
207+
url: 'https://api.example.com/test',
208+
method: 'get'
209209
},
210210
examples: [
211211
{
212212
name: 'Example Request',
213213
description: 'A simple example',
214214
request: {
215-
url: 'https://api.example.com/example'
215+
url: 'https://api.example.com/example',
216+
method: 'get'
216217
}
217218
}
218219
]
@@ -233,6 +234,7 @@ example {
233234
234235
request: {
235236
url: https://api.example.com/example
237+
method: get
236238
}
237239
}
238240
`;

0 commit comments

Comments
 (0)