feat: updates

This commit is contained in:
lohxt1
2024-12-18 18:57:23 +05:30
parent f871bc0fa2
commit f72d643e02
9 changed files with 254 additions and 25 deletions

View File

@@ -39,6 +39,7 @@ const Oauth2Store = require('../../store/oauth2');
const iconv = require('iconv-lite');
const FormData = require('form-data');
const { createFormData } = require('../../utils/form-data');
const { findItemInCollectionByPathname } = require('../../utils/collection');
const safeStringifyJSON = (data) => {
try {
@@ -393,7 +394,8 @@ const registerNetworkIpc = (mainWindow) => {
collectionUid,
runtimeVariables,
processEnvVars,
scriptingConfig
scriptingConfig,
runRequestByItemPathname
) => {
// run pre-request script
let scriptResult;
@@ -408,7 +410,8 @@ const registerNetworkIpc = (mainWindow) => {
collectionPath,
onConsoleLog,
processEnvVars,
scriptingConfig
scriptingConfig,
runRequestByItemPathname
);
mainWindow.webContents.send('main:script-environment-update', {
@@ -458,7 +461,8 @@ const registerNetworkIpc = (mainWindow) => {
collectionUid,
runtimeVariables,
processEnvVars,
scriptingConfig
scriptingConfig,
runRequestByItemPathname
) => {
// run post-response vars
const postResponseVars = get(request, 'vars.res', []);
@@ -506,7 +510,8 @@ const registerNetworkIpc = (mainWindow) => {
collectionPath,
onConsoleLog,
processEnvVars,
scriptingConfig
scriptingConfig,
runRequestByItemPathname
);
mainWindow.webContents.send('main:script-environment-update', {
@@ -523,14 +528,24 @@ const registerNetworkIpc = (mainWindow) => {
return scriptResult;
};
// handler for sending http request
ipcMain.handle('send-http-request', async (event, item, collection, environment, runtimeVariables) => {
const runRequest = async ({ item, collection, environment, runtimeVariables, runInBackground = false }) => {
const collectionUid = collection.uid;
const collectionPath = collection.pathname;
const cancelTokenUid = uuid();
const requestUid = uuid();
mainWindow.webContents.send('main:run-request-event', {
const runRequestByItemPathname = async ({ itemPathname }) => {
return new Promise(async (resolve, reject) => {
const _item = findItemInCollectionByPathname(collection, itemPathname);
if(_item) {
const res = await runRequest({ item: _item, collection, environment, runtimeVariables, runInBackground: true });
resolve(res);
}
reject(`bru.runRequest: invalid request path - ${itemPathname}`);
});
}
!runInBackground && mainWindow.webContents.send('main:run-request-event', {
type: 'request-queued',
requestUid,
collectionUid,
@@ -561,7 +576,8 @@ const registerNetworkIpc = (mainWindow) => {
collectionUid,
runtimeVariables,
processEnvVars,
scriptingConfig
scriptingConfig,
runRequestByItemPathname
);
const axiosInstance = await configureRequest(
@@ -573,7 +589,7 @@ const registerNetworkIpc = (mainWindow) => {
collectionPath
);
mainWindow.webContents.send('main:run-request-event', {
!runInBackground && mainWindow.webContents.send('main:run-request-event', {
type: 'request-sent',
requestSent: {
url: request.url,
@@ -645,7 +661,8 @@ const registerNetworkIpc = (mainWindow) => {
collectionUid,
runtimeVariables,
processEnvVars,
scriptingConfig
scriptingConfig,
runRequestByItemPathname
);
// run assertions
@@ -661,7 +678,7 @@ const registerNetworkIpc = (mainWindow) => {
processEnvVars
);
mainWindow.webContents.send('main:run-request-event', {
!runInBackground && mainWindow.webContents.send('main:run-request-event', {
type: 'assertion-results',
results: results,
itemUid: item.uid,
@@ -682,10 +699,11 @@ const registerNetworkIpc = (mainWindow) => {
collectionPath,
onConsoleLog,
processEnvVars,
scriptingConfig
scriptingConfig,
runRequestByItemPathname
);
mainWindow.webContents.send('main:run-request-event', {
!runInBackground && mainWindow.webContents.send('main:run-request-event', {
type: 'test-results',
results: testResults.results,
itemUid: item.uid,
@@ -719,6 +737,11 @@ const registerNetworkIpc = (mainWindow) => {
return Promise.reject(error);
}
}
// handler for sending http request
ipcMain.handle('send-http-request', async (event, item, collection, environment, runtimeVariables) => {
return await runRequest({ item, collection, environment, runtimeVariables });
});
ipcMain.handle('send-collection-oauth2-request', async (event, collection, environment, runtimeVariables) => {
@@ -996,6 +1019,41 @@ const registerNetworkIpc = (mainWindow) => {
nextRequestName = preRequestScriptResult.nextRequestName;
}
if (preRequestScriptResult?.stopExecution) {
deleteCancelToken(cancelTokenUid);
mainWindow.webContents.send('main:run-folder-event', {
type: 'response-received',
error: 'Request has been stopped from pre-request script',
responseReceived: {
status: 'terminated',
statusText: 'Request execution stopped!',
data: null
},
...eventData
});
mainWindow.webContents.send('main:run-folder-event', {
type: 'testrun-ended',
collectionUid,
folderUid
});
break;
}
if (preRequestScriptResult?.skipRequest) {
mainWindow.webContents.send('main:run-folder-event', {
type: 'request-skipped',
error: 'Request has been skipped from pre-request script',
responseReceived: {
status: 'skipped',
statusText: 'request skipped via pre-request script',
data: null
},
...eventData
});
currentRequestIndex++;
continue;
}
// todo:
// i have no clue why electron can't send the request object
// without safeParseJSON(safeStringifyJSON(request.data))
@@ -1114,6 +1172,41 @@ const registerNetworkIpc = (mainWindow) => {
nextRequestName = postRequestScriptResult.nextRequestName;
}
if (postRequestScriptResult?.stopExecution) {
deleteCancelToken(cancelTokenUid);
mainWindow.webContents.send('main:run-folder-event', {
type: 'response-received',
error: 'Request has been stopped from post-response script',
responseReceived: {
status: 'terminated',
statusText: 'Request execution stopped!',
data: null
},
...eventData
});
mainWindow.webContents.send('main:run-folder-event', {
type: 'testrun-ended',
collectionUid,
folderUid
});
break;
}
if (postRequestScriptResult?.skipRequest) {
mainWindow.webContents.send('main:run-folder-event', {
type: 'request-skipped',
error: 'Request has been skipped from post-response script',
responseReceived: {
status: 'skipped',
statusText: 'request skipped via post-response script',
data: null
},
...eventData
});
currentRequestIndex++;
continue;
}
// run assertions
const assertions = get(item, 'request.assertions');
if (assertions) {

View File

@@ -203,9 +203,31 @@ const getTreePathFromCollectionToItem = (collection, _item) => {
return path;
};
const slash = (path) => {
const isExtendedLengthPath = /^\\\\\?\\/.test(path);
if (isExtendedLengthPath) {
return path;
}
return path.replace(/\\/g, '/');
};
const findItemByPathname = (items = [], pathname) => {
return find(items, (i) => slash(i.pathname) === slash(pathname));
};
const findItemInCollectionByPathname = (collection, pathname) => {
let flattenedItems = flattenItems(collection.items);
return findItemByPathname(flattenedItems, pathname);
};
module.exports = {
mergeHeaders,
mergeVars,
mergeScripts,
getTreePathFromCollectionToItem
getTreePathFromCollectionToItem,
slash,
findItemByPathname,
findItemInCollectionByPathname
}