Skip to content

Commit

Permalink
refactor: return created request file path and update sanitzation regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-treason committed Oct 14, 2023
1 parent 992fcf2 commit 307c5d0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ export const moveItemToRootOfCollection = (collectionUid, draggedItemUid) => (di
export const newHttpRequest = (params) => (dispatch, getState) => {
const { requestName, requestType, requestUrl, requestMethod, collectionUid, itemUid } = params;

return new Promise((resolve, reject) => {
return new Promise(async (resolve, reject) => {
const state = getState();
const collection = findCollectionByUid(state.collections.collections, collectionUid);
if (!collection) {
Expand Down Expand Up @@ -620,20 +620,22 @@ export const newHttpRequest = (params) => (dispatch, getState) => {
if (!reqWithSameNameExists) {
const { ipcRenderer } = window;

ipcRenderer.invoke('renderer:new-request', collection.pathname, item).then(resolve).catch(reject);
const newPath = await ipcRenderer.invoke('renderer:new-request', collection.pathname, item);
// the useCollectionNextAction() will track this and open the new request in a new tab
// once the request is created
dispatch(
updateNextAction({
nextAction: {
type: 'OPEN_REQUEST',
payload: {
pathname: collection.pathname + PATH_SEPARATOR + sanitizeFilenme(item.name) + '.bru'
pathname: newPath
}
},
collectionUid
})
);

resolve();
} else {
return reject(new Error('Duplicate request names are not allowed under the same folder'));
}
Expand All @@ -649,7 +651,7 @@ export const newHttpRequest = (params) => (dispatch, getState) => {
if (!reqWithSameNameExists) {
const { ipcRenderer } = window;

ipcRenderer.invoke('renderer:new-request', fullName, item).then(resolve).catch(reject);
const newPath = await ipcRenderer.invoke('renderer:new-request', fullName, item);

// the useCollectionNextAction() will track this and open the new request in a new tab
// once the request is created
Expand All @@ -658,12 +660,14 @@ export const newHttpRequest = (params) => (dispatch, getState) => {
nextAction: {
type: 'OPEN_REQUEST',
payload: {
pathname: collection.pathname + PATH_SEPARATOR + sanitizeFilenme(item.name) + '.bru'
pathname: newPath
}
},
collectionUid
})
);

resolve();
} else {
return reject(new Error('Duplicate request names are not allowed under the same folder'));
}
Expand Down
2 changes: 2 additions & 0 deletions packages/bruno-electron/src/ipc/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection

const content = jsonToBru(request);
await writeFile(sanitizedPathname, content);

return sanitizedPathname;
} catch (error) {
return Promise.reject(error);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/bruno-electron/src/utils/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const sanitizeDirectoryName = (name) => {
};

const sanitizeFilenme = (name) => {
return name.replace(/[^\w-_.]/g, '_');
return name.replace(/[<>:"/\\|?*\x00-\x1F]/g, '_');
};

module.exports = {
Expand Down

0 comments on commit 307c5d0

Please sign in to comment.