diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/global-environments.js b/packages/bruno-app/src/providers/ReduxStore/slices/global-environments.js index 6fe856ce5..8e8effd1e 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/global-environments.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/global-environments.js @@ -90,10 +90,11 @@ export const { export const addGlobalEnvironment = ({ name, variables = [] }) => (dispatch) => { return new Promise((resolve, reject) => { const uid = uuid(); - + let environment = { name, uid, variables }; const { ipcRenderer } = window; - ipcRenderer - .invoke('renderer:create-global-environment', { name, uid, variables }) + environmentSchema + .validate(environment) + .then(() => ipcRenderer.invoke('renderer:create-global-environment', { name, uid, variables })) .then((result) => { const finalName = result?.name || name; dispatch(_addGlobalEnvironment({ name: finalName, uid, variables })); @@ -110,11 +111,12 @@ export const copyGlobalEnvironment = ({ name, environmentUid: baseEnvUid }) => ( const globalEnvironments = state.globalEnvironments.globalEnvironments; const baseEnv = globalEnvironments?.find(env => env?.uid == baseEnvUid) const uid = uuid(); + let environment = { uid, name, variables: baseEnv.variables }; const { ipcRenderer } = window; - ipcRenderer - .invoke('renderer:create-global-environment', { uid, name, variables: baseEnv.variables }) + environmentSchema + .validate(environment) + .then(() => ipcRenderer.invoke('renderer:create-global-environment', { uid, name, variables: baseEnv.variables })) .then((result) => { - // Use the unique name returned by the IPC handler const finalName = result?.name || name; dispatch(_copyGlobalEnvironment({ name: finalName, uid, variables: baseEnv.variables })); }) @@ -151,9 +153,11 @@ export const saveGlobalEnvironment = ({ variables, environmentUid }) => (dispatc return reject(new Error('Environment not found')); } + let environmentToSave = { ...environment, variables }; + const { ipcRenderer } = window; environmentSchema - .validate(environment) + .validate(environmentToSave) .then(() => ipcRenderer.invoke('renderer:save-global-environment', { environmentUid, variables @@ -228,8 +232,10 @@ export const globalEnvironmentsUpdateEvent = ({ globalEnvironmentVariables }) => } }); + let environmentToSave = { ...environment, variables }; + environmentSchema - .validate(environment) + .validate(environmentToSave) .then(() => ipcRenderer.invoke('renderer:save-global-environment', { environmentUid, variables diff --git a/packages/bruno-app/src/utils/environments.js b/packages/bruno-app/src/utils/environments.js index f7e8948cb..8ce488aa4 100644 --- a/packages/bruno-app/src/utils/environments.js +++ b/packages/bruno-app/src/utils/environments.js @@ -1,3 +1,5 @@ +import { uuid } from './common/index'; + const isPersistableEnvVarForMerge = (persistedNames) => (v) => { return !v?.ephemeral || v?.persistedValue !== undefined || (v?.name && persistedNames.has(v.name)); }; @@ -30,12 +32,21 @@ export const buildPersistedEnvVariables = (variables, { mode, persistedNames } = return src.map(toPersistedEnvVarForSave); }; -export const buildEnvVariable = (obj) => { - return { +export const buildEnvVariable = ({ envVariable: obj, withUuid = false }) => { + let envVariable = { name: obj.name ?? '', value: !!obj.secret ? '' : (obj.value ?? ''), type: 'text', enabled: obj.enabled !== false, secret: !!obj.secret }; + + if (!withUuid) { + return envVariable; + } + + return { + uid: uuid(), + ...envVariable + }; }; diff --git a/packages/bruno-app/src/utils/exporters/bruno-environment.js b/packages/bruno-app/src/utils/exporters/bruno-environment.js index e22bc8dcd..dc3d0a0f8 100644 --- a/packages/bruno-app/src/utils/exporters/bruno-environment.js +++ b/packages/bruno-app/src/utils/exporters/bruno-environment.js @@ -6,7 +6,7 @@ export const exportBrunoEnvironment = async ({ environments, environmentType, fi let cleanEnvironments = environments.map((environment) => ({ name: environment.name, - variables: (environment.variables || []).map(buildEnvVariable) + variables: (environment.variables || []).map((envVariable) => buildEnvVariable({ envVariable })) })); await ipcRenderer.invoke('renderer:export-environment', { diff --git a/packages/bruno-app/src/utils/importers/bruno-environment.js b/packages/bruno-app/src/utils/importers/bruno-environment.js index 13a37380e..7cf2f1e27 100644 --- a/packages/bruno-app/src/utils/importers/bruno-environment.js +++ b/packages/bruno-app/src/utils/importers/bruno-environment.js @@ -22,7 +22,7 @@ const validateBrunoEnvironment = (env) => { return { name: env.name || 'Imported Environment', - variables: env.variables.map(buildEnvVariable) + variables: env.variables.map((envVariable) => buildEnvVariable({ envVariable, withUuid: true })) }; }; diff --git a/packages/bruno-electron/src/ipc/collection.js b/packages/bruno-electron/src/ipc/collection.js index 6bfb109c2..b12b76f5a 100644 --- a/packages/bruno-electron/src/ipc/collection.js +++ b/packages/bruno-electron/src/ipc/collection.js @@ -412,7 +412,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection fs.mkdirSync(exportPath, { recursive: true }); for (const environment of environments) { - const baseFileName = `${environment.name.replace(/[^a-zA-Z0-9-_]/g, '_')}`; + const baseFileName = environment.name ? `${environment.name.replace(/[^a-zA-Z0-9-_]/g, '_')}` : 'environment'; const uniqueFileName = generateUniqueName(baseFileName, (name) => fs.existsSync(path.join(exportPath, `${name}.json`))); const fullPath = path.join(exportPath, `${uniqueFileName}.json`); @@ -444,7 +444,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection } const environment = environments[0]; - const baseFileName = `${environment.name.replace(/[^a-zA-Z0-9-_]/g, '_')}`; + const baseFileName = environment.name ? `${environment.name.replace(/[^a-zA-Z0-9-_]/g, '_')}` : 'environment'; const uniqueFileName = generateUniqueName(baseFileName, (name) => fs.existsSync(path.join(filePath, `${name}.json`))); const fullPath = path.join(filePath, `${uniqueFileName}.json`); const jsonContent = JSON.stringify(environmentWithInfo(environment), null, 2);