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

View File

@ -1,6 +1,7 @@
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 { cloneDeep } from 'lodash';
const initialState = {
globalEnvironments: [],
@ -72,7 +73,7 @@ export const globalEnvironmentsSlice = createSlice({
state.activeGlobalEnvironmentUid = null;
}
}
},
}
}
});
@ -149,17 +150,12 @@ export const saveGlobalEnvironment = ({ variables, environmentUid }) => (dispatc
.then(() => ipcRenderer.invoke('renderer:save-global-environment', {
environmentUid,
variables
// variables: variables?.map(v => {
// let { uid, ...rest } = v;
// return rest;
// })
}))
.then(
dispatch(_saveGlobalEnvironment({ environmentUid, variables }))
)
.then(resolve)
.catch((error) => {
console.error(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;

View File

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

View File

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

View File

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

View File

@ -33,6 +33,18 @@ const addBruShimToContext = (vm, bru) => {
vm.setProp(bruObject, 'setEnvVar', setEnvVar);
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) {
return marshallToVm(bru.getVar(vm.dump(key)), vm);
});