add: filetype for import collection (#6533)

This commit is contained in:
naman-bruno
2025-12-29 16:53:43 +05:30
committed by GitHub
parent c714e9b5d6
commit 646f63dbeb
4 changed files with 42 additions and 5 deletions

View File

@@ -92,6 +92,7 @@ const ImportCollectionLocation = ({ onClose, handleSubmit, rawData, format }) =>
const inputRef = useRef();
const dispatch = useDispatch();
const [groupingType, setGroupingType] = useState('tags');
const [collectionFormat, setCollectionFormat] = useState('bru');
const dropdownTippyRef = useRef();
const isOpenApi = format === 'openapi';
@@ -119,7 +120,7 @@ const ImportCollectionLocation = ({ onClose, handleSubmit, rawData, format }) =>
}),
onSubmit: async (values) => {
const convertedCollection = await convertCollection(format, rawData, groupingType);
handleSubmit(convertedCollection, values.collectionLocation);
handleSubmit(convertedCollection, values.collectionLocation, { format: collectionFormat });
}
});
@@ -209,6 +210,31 @@ const ImportCollectionLocation = ({ onClose, handleSubmit, rawData, format }) =>
Browse
</span>
</div>
<div className="mt-4">
<label htmlFor="format" className="flex items-center font-medium">
File Format
<Help width="300">
<p>Choose the file format for storing requests in this collection.</p>
<p className="mt-2">
<strong>OpenCollection (YAML):</strong> Industry-standard YAML format (.yml files)
</p>
<p className="mt-1">
<strong>BRU:</strong> Bruno's native file format (.bru files)
</p>
</Help>
</label>
<select
id="format"
name="format"
className="block textbox mt-2 w-full"
value={collectionFormat}
onChange={(e) => setCollectionFormat(e.target.value)}
>
<option value="yml">OpenCollection (YAML)</option>
<option value="bru">BRU Format (.bru)</option>
</select>
</div>
</div>
{isOpenApi && (

View File

@@ -58,8 +58,8 @@ const CollectionsSection = () => {
setImportCollectionLocationModalOpen(true);
};
const handleImportCollectionLocation = (convertedCollection, collectionLocation) => {
dispatch(importCollection(convertedCollection, collectionLocation))
const handleImportCollectionLocation = (convertedCollection, collectionLocation, options = {}) => {
dispatch(importCollection(convertedCollection, collectionLocation, options))
.then(() => {
setImportCollectionLocationModalOpen(false);
setImportData(null);

View File

@@ -2389,7 +2389,7 @@ export const importCollection = (collection, collectionLocation, options = {}) =
const state = getState();
const activeWorkspace = state.workspaces.workspaces.find((w) => w.uid === state.workspaces.activeWorkspaceUid);
const collectionPath = await ipcRenderer.invoke('renderer:import-collection', collection, collectionLocation);
const collectionPath = await ipcRenderer.invoke('renderer:import-collection', collection, collectionLocation, options.format || 'bru');
if (activeWorkspace && activeWorkspace.pathname && activeWorkspace.type !== 'default') {
const workspaceCollection = {

View File

@@ -851,11 +851,22 @@ const registerRendererEventHandlers = (mainWindow, watcher) => {
throw new Error(`collection: ${collectionPath} already exists`);
}
const getFilenameWithFormat = (item, format) => {
if (item?.filename) {
const ext = path.extname(item.filename);
if (ext === '.bru' || ext === '.yml') {
return item.filename.replace(ext, `.${format}`);
}
return item.filename;
}
return `${item.name}.${format}`;
};
// Recursive function to parse the collection items and create files/folders
const parseCollectionItems = async (items = [], currentPath) => {
await Promise.all(items.map(async (item) => {
if (['http-request', 'graphql-request', 'grpc-request', 'ws-request'].includes(item.type)) {
let sanitizedFilename = sanitizeName(item?.filename || `${item.name}.${format}`);
let sanitizedFilename = sanitizeName(getFilenameWithFormat(item, format));
const content = await stringifyRequestViaWorker(item, { format });
const filePath = path.join(currentPath, sanitizedFilename);
safeWriteFileSync(filePath, content);