feat: updates

This commit is contained in:
lohxt1 2024-10-03 11:52:00 +05:30
parent f088cdb504
commit 5afafb5944
7 changed files with 105 additions and 13 deletions

View File

@ -23,7 +23,7 @@ import { collectionAddEnvFileEvent, openCollectionEvent } from 'providers/ReduxS
import toast from 'react-hot-toast'; import toast from 'react-hot-toast';
import { useDispatch } from 'react-redux'; import { useDispatch } from 'react-redux';
import { isElectron } from 'utils/common/platform'; import { isElectron } from 'utils/common/platform';
import { updateGlobalEnvironments } from 'providers/ReduxStore/slices/globalEnvironments'; import { globalEnvironmentsUpdateEvent, updateGlobalEnvironments } from 'providers/ReduxStore/slices/globalEnvironments';
const useIpcEvents = () => { const useIpcEvents = () => {
const dispatch = useDispatch(); const dispatch = useDispatch();
@ -110,6 +110,10 @@ const useIpcEvents = () => {
dispatch(scriptEnvironmentUpdateEvent(val)); dispatch(scriptEnvironmentUpdateEvent(val));
}); });
const removeGlobalEnvironmentVariablesUpdateListener = ipcRenderer.on('main:global-environment-variables-update', (val) => {
dispatch(globalEnvironmentsUpdateEvent(val));
});
const removeCollectionRenamedListener = ipcRenderer.on('main:collection-renamed', (val) => { const removeCollectionRenamedListener = ipcRenderer.on('main:collection-renamed', (val) => {
dispatch(collectionRenamedEvent(val)); dispatch(collectionRenamedEvent(val));
}); });
@ -160,6 +164,7 @@ const useIpcEvents = () => {
removeCollectionAlreadyOpenedListener(); removeCollectionAlreadyOpenedListener();
removeDisplayErrorListener(); removeDisplayErrorListener();
removeScriptEnvUpdateListener(); removeScriptEnvUpdateListener();
removeGlobalEnvironmentVariablesUpdateListener();
removeCollectionRenamedListener(); removeCollectionRenamedListener();
removeRunFolderEventListener(); removeRunFolderEventListener();
removeRunRequestEventListener(); removeRunRequestEventListener();

View File

@ -1,6 +1,7 @@
import { createSlice } from '@reduxjs/toolkit'; import { createSlice } from '@reduxjs/toolkit';
import { generateUidBasedOnHash } from 'utils/common/index'; import { generateUidBasedOnHash, stringifyIfNot, uuid } from 'utils/common/index';
import { environmentSchema } from '@usebruno/schema'; import { environmentSchema } from '@usebruno/schema';
import { cloneDeep } from 'lodash';
const initialState = { const initialState = {
globalEnvironments: [], globalEnvironments: [],
@ -72,7 +73,7 @@ export const globalEnvironmentsSlice = createSlice({
state.activeGlobalEnvironmentUid = null; state.activeGlobalEnvironmentUid = null;
} }
} }
}, }
} }
}); });
@ -149,17 +150,12 @@ export const saveGlobalEnvironment = ({ variables, environmentUid }) => (dispatc
.then(() => ipcRenderer.invoke('renderer:save-global-environment', { .then(() => ipcRenderer.invoke('renderer:save-global-environment', {
environmentUid, environmentUid,
variables variables
// variables: variables?.map(v => {
// let { uid, ...rest } = v;
// return rest;
// })
})) }))
.then( .then(
dispatch(_saveGlobalEnvironment({ environmentUid, variables })) dispatch(_saveGlobalEnvironment({ environmentUid, variables }))
) )
.then(resolve) .then(resolve)
.catch((error) => { .catch((error) => {
console.error(error);
reject(error); reject(error);
}); });
}); });
@ -189,5 +185,57 @@ export const deleteGlobalEnvironment = ({ environmentUid }) => (dispatch, getSta
}); });
}; };
export const globalEnvironmentsUpdateEvent = ({ globalEnvironmentVariables }) => (dispatch, getState) => {
return new Promise((resolve, reject) => {
if (!globalEnvironmentVariables) resolve();
const state = getState();
const globalEnvironments = state?.globalEnvironments?.globalEnvironments || [];
const environmentUid = state?.globalEnvironments?.activeGlobalEnvironmentUid;
const environment = globalEnvironments?.find(env => env?.uid == environmentUid);
if (!environment || !environmentUid) {
return reject(new Error('Environment not found'));
}
let variables = cloneDeep(environment?.variables);
// update existing values
variables = variables?.map?.(variable => ({
...variable,
value: stringifyIfNot(globalEnvironmentVariables?.[variable?.name])
}));
// add new env values
Object.entries(globalEnvironmentVariables)?.forEach?.(([key, value]) => {
let isAnExistingVariable = variables?.find(v => v?.name == key)
if (!isAnExistingVariable) {
variables.push({
uid: uuid(),
name: key,
value: stringifyIfNot(value),
type: 'text',
secret: false,
enabled: true
});
}
});
environmentSchema
.validate(environment)
.then(() => ipcRenderer.invoke('renderer:save-global-environment', {
environmentUid,
variables
}))
.then(
dispatch(_saveGlobalEnvironment({ environmentUid, variables }))
)
.then(resolve)
.catch((error) => {
reject(error);
});
});
}
export default globalEnvironmentsSlice.reducer; export default globalEnvironmentsSlice.reducer;

View File

@ -164,3 +164,5 @@ export const generateUidBasedOnHash = (str) => {
return `${hash}`.padEnd(21, '0'); return `${hash}`.padEnd(21, '0');
}; };
export const stringifyIfNot = v => typeof v === 'string' ? v : String(v);

View File

@ -409,6 +409,10 @@ const registerNetworkIpc = (mainWindow) => {
requestUid, requestUid,
collectionUid collectionUid
}); });
mainWindow.webContents.send('main:global-environment-variables-update', {
globalEnvironmentVariables: scriptResult.globalEnvironmentVariables
});
} }
// interpolate variables inside request // interpolate variables inside request
@ -469,6 +473,10 @@ const registerNetworkIpc = (mainWindow) => {
requestUid, requestUid,
collectionUid collectionUid
}); });
mainWindow.webContents.send('main:global-environment-variables-update', {
globalEnvironmentVariables: result.globalEnvironmentVariables
});
} }
if (result?.error) { if (result?.error) {
@ -504,6 +512,10 @@ const registerNetworkIpc = (mainWindow) => {
requestUid, requestUid,
collectionUid collectionUid
}); });
mainWindow.webContents.send('main:global-environment-variables-update', {
globalEnvironmentVariables: scriptResult.globalEnvironmentVariables
});
} }
return scriptResult; return scriptResult;
}; };
@ -691,6 +703,10 @@ const registerNetworkIpc = (mainWindow) => {
requestUid, requestUid,
collectionUid collectionUid
}); });
mainWindow.webContents.send('main:global-environment-variables-update', {
globalEnvironmentVariables: testResults.globalEnvironmentVariables
});
} }
return { return {
@ -1160,6 +1176,10 @@ const registerNetworkIpc = (mainWindow) => {
runtimeVariables: testResults.runtimeVariables, runtimeVariables: testResults.runtimeVariables,
collectionUid collectionUid
}); });
mainWindow.webContents.send('main:global-environment-variables-update', {
globalEnvironmentVariables: testResults.globalEnvironmentVariables
});
} }
} catch (error) { } catch (error) {
mainWindow.webContents.send('main:run-folder-event', { mainWindow.webContents.send('main:run-folder-event', {

View File

@ -103,6 +103,7 @@ class ScriptRuntime {
request, request,
envVariables: cleanJson(envVariables), envVariables: cleanJson(envVariables),
runtimeVariables: cleanJson(runtimeVariables), runtimeVariables: cleanJson(runtimeVariables),
globalEnvironmentVariables: cleanJson(globalEnvironmentVariables),
nextRequestName: bru.nextRequest nextRequestName: bru.nextRequest
}; };
} }
@ -150,6 +151,7 @@ class ScriptRuntime {
request, request,
envVariables: cleanJson(envVariables), envVariables: cleanJson(envVariables),
runtimeVariables: cleanJson(runtimeVariables), runtimeVariables: cleanJson(runtimeVariables),
globalEnvironmentVariables: cleanJson(globalEnvironmentVariables),
nextRequestName: bru.nextRequest nextRequestName: bru.nextRequest
}; };
} }
@ -218,6 +220,7 @@ class ScriptRuntime {
response, response,
envVariables: cleanJson(envVariables), envVariables: cleanJson(envVariables),
runtimeVariables: cleanJson(runtimeVariables), runtimeVariables: cleanJson(runtimeVariables),
globalEnvironmentVariables: cleanJson(globalEnvironmentVariables),
nextRequestName: bru.nextRequest nextRequestName: bru.nextRequest
}; };
} }
@ -265,6 +268,7 @@ class ScriptRuntime {
response, response,
envVariables: cleanJson(envVariables), envVariables: cleanJson(envVariables),
runtimeVariables: cleanJson(runtimeVariables), runtimeVariables: cleanJson(runtimeVariables),
globalEnvironmentVariables: cleanJson(globalEnvironmentVariables),
nextRequestName: bru.nextRequest nextRequestName: bru.nextRequest
}; };
} }

View File

@ -82,12 +82,12 @@ class TestRuntime {
request, request,
envVariables, envVariables,
runtimeVariables, runtimeVariables,
globalEnvironmentVariables,
results: __brunoTestResults.getResults(), results: __brunoTestResults.getResults(),
nextRequestName: bru.nextRequest nextRequestName: bru.nextRequest
}; };
} }
const context = { const context = {
test, test,
bru, bru,
@ -163,6 +163,7 @@ class TestRuntime {
request, request,
envVariables: cleanJson(envVariables), envVariables: cleanJson(envVariables),
runtimeVariables: cleanJson(runtimeVariables), runtimeVariables: cleanJson(runtimeVariables),
globalEnvironmentVariables: cleanJson(globalEnvironmentVariables),
results: cleanJson(__brunoTestResults.getResults()), results: cleanJson(__brunoTestResults.getResults()),
nextRequestName: bru.nextRequest nextRequestName: bru.nextRequest
}; };

View File

@ -33,6 +33,18 @@ const addBruShimToContext = (vm, bru) => {
vm.setProp(bruObject, 'setEnvVar', setEnvVar); vm.setProp(bruObject, 'setEnvVar', setEnvVar);
setEnvVar.dispose(); setEnvVar.dispose();
let getGlobalEnvVar = vm.newFunction('getGlobalEnvVar', function (key) {
return marshallToVm(bru.getGlobalEnvVar(vm.dump(key)), vm);
});
vm.setProp(bruObject, 'getGlobalEnvVar', getGlobalEnvVar);
getGlobalEnvVar.dispose();
let setGlobalEnvVar = vm.newFunction('setGlobalEnvVar', function (key, value) {
bru.setGlobalEnvVar(vm.dump(key), vm.dump(value));
});
vm.setProp(bruObject, 'setGlobalEnvVar', setGlobalEnvVar);
setGlobalEnvVar.dispose();
let getVar = vm.newFunction('getVar', function (key) { let getVar = vm.newFunction('getVar', function (key) {
return marshallToVm(bru.getVar(vm.dump(key)), vm); return marshallToVm(bru.getVar(vm.dump(key)), vm);
}); });