diff --git a/packages/hoppscotch-common/src/components/environments/my/Details.vue b/packages/hoppscotch-common/src/components/environments/my/Details.vue index 47d7b2e4..44b7a947 100644 --- a/packages/hoppscotch-common/src/components/environments/my/Details.vue +++ b/packages/hoppscotch-common/src/components/environments/my/Details.vue @@ -492,21 +492,12 @@ const saveEnvironment = () => { const variables = pipe( filteredVariables, - A.map((e) => - e.secret - ? { - key: e.key, - secret: e.secret, - initialValue: e.initialValue, - currentValue: "", - } - : { - key: e.key, - secret: e.secret, - initialValue: e.initialValue, - currentValue: "", - } - ) + A.map((e) => ({ + key: e.key, + secret: e.secret, + initialValue: e.initialValue || "", + currentValue: "", + })) ) const environmentUpdated: Environment = { diff --git a/packages/hoppscotch-common/src/components/environments/teams/Details.vue b/packages/hoppscotch-common/src/components/environments/teams/Details.vue index 3759da77..1d696fb4 100644 --- a/packages/hoppscotch-common/src/components/environments/teams/Details.vue +++ b/packages/hoppscotch-common/src/components/environments/teams/Details.vue @@ -437,7 +437,7 @@ const saveEnvironment = async () => { A.map((e) => ({ key: e.key, secret: e.secret, - initialValue: e.initialValue, + initialValue: e.initialValue || "", currentValue: "", })) ) diff --git a/packages/hoppscotch-common/src/helpers/RequestRunner.ts b/packages/hoppscotch-common/src/helpers/RequestRunner.ts index eebd69b1..a9a45fb3 100644 --- a/packages/hoppscotch-common/src/helpers/RequestRunner.ts +++ b/packages/hoppscotch-common/src/helpers/RequestRunner.ts @@ -100,6 +100,16 @@ export const getTestableBody = ( return x } +/** + * Combines the environment variables from the request and the selected, global, and temporary environments. + * The priority is as follows: + * 1. Request variables + * 2. Temporary variables (if any) + * 3. Selected environment variables + * 4. Global environment variables + * @param variables The environment variables to combine + * @returns The combined environment variables + */ export const combineEnvVariables = (variables: { environments: { selected: Environment["variables"] @@ -119,8 +129,8 @@ export const executedResponses$ = new Subject< >() /** - * Used to update the environment schema with the secret variables - * and store the secret variable values in the secret environment service + * This will update the environment variables in the current environment + * and secret environment service. * @param envs The environment variables to update * @param type Whether the environment variables are global or selected * @returns the updated environment variables @@ -216,8 +226,24 @@ const getEnvironmentVariableValue = ( ) } +/** + * Set currentValue as initialValue if currentValue is empty + * This is set just for request runtime and it will not be persisted. + * @param env The environment variable to be transformed + * @returns The transformed environment variable with currentValue set to initialValue if empty + */ +const getTransformedEnvs = ( + env: Environment["variables"][number] +): Environment["variables"][number] => { + return { + ...env, + currentValue: env.currentValue || env.initialValue, + } +} + /** * Transforms the environment list to a list with unique keys with value + * and set currentValue as initialValue if currentValue is empty. * @param envs The environment list to be transformed * @returns The transformed environment list with keys with value */ @@ -226,21 +252,21 @@ const filterNonEmptyEnvironmentVariables = ( ): Environment["variables"] => { const envsMap = new Map() envs.forEach((env) => { - if (env.secret) { - envsMap.set(env.key, env) - } else if (envsMap.has(env.key)) { - const existingEnv = envsMap.get(env.key) + const transformedEnv = getTransformedEnvs(env) + + if (envsMap.has(transformedEnv.key)) { + const existingEnv = envsMap.get(transformedEnv.key) if ( existingEnv && "currentValue" in existingEnv && existingEnv.currentValue === "" && - env.currentValue !== "" + transformedEnv.currentValue !== "" ) { - envsMap.set(env.key, env) + envsMap.set(transformedEnv.key, transformedEnv) } } else { - envsMap.set(env.key, env) + envsMap.set(transformedEnv.key, transformedEnv) } }) @@ -502,7 +528,7 @@ function updateEnvsAfterTestScript(runResult: E.Right) { v: 2, variables: globalEnvVariables, }) - updateEnvironments( + const selectedEnvVariables = updateEnvironments( // @ts-expect-error Typescript can't figure out this inference for some reason cloneDeep(runResult.right.envs.selected), "selected" @@ -516,7 +542,7 @@ function updateEnvsAfterTestScript(runResult: E.Right) { name: env.name, v: 2, id: "id" in env ? env.id : "", - variables: runResult.right.envs.selected, + variables: selectedEnvVariables, }) } else if ( environmentsStore.value.selectedEnvironmentIndex.type === "TEAM_ENV" @@ -526,7 +552,7 @@ function updateEnvsAfterTestScript(runResult: E.Right) { }) pipe( updateTeamEnvironment( - JSON.stringify(runResult.right.envs.selected), + JSON.stringify(selectedEnvVariables), environmentsStore.value.selectedEnvironmentIndex.teamEnvID, env.name ) @@ -565,13 +591,15 @@ export function runTestRunnerRequest( id: "env-id", v: 2, name: "Env", - variables: combineEnvVariables({ - environments: { - ...preRequestScriptResult.right.envs, - temp: !persistEnv ? getTemporaryVariables() : [], - }, - requestVariables: [], - }), + variables: filterNonEmptyEnvironmentVariables( + combineEnvVariables({ + environments: { + ...preRequestScriptResult.right.envs, + temp: !persistEnv ? getTemporaryVariables() : [], + }, + requestVariables: [], + }) + ), }) const [stream] = createRESTNetworkRequestStream(effectiveRequest) diff --git a/packages/hoppscotch-common/src/helpers/editor/extensions/HoppEnvironment.ts b/packages/hoppscotch-common/src/helpers/editor/extensions/HoppEnvironment.ts index 1c817f62..a335fc74 100644 --- a/packages/hoppscotch-common/src/helpers/editor/extensions/HoppEnvironment.ts +++ b/packages/hoppscotch-common/src/helpers/editor/extensions/HoppEnvironment.ts @@ -57,7 +57,11 @@ const filterNonEmptyEnvironmentVariables = ( if (envsMap.has(env.key)) { const existingEnv = envsMap.get(env.key) - if (existingEnv?.currentValue === "" && env.currentValue !== "") { + if ( + existingEnv?.currentValue === "" && + existingEnv?.initialValue === "" && + (env.currentValue || env.initialValue) + ) { envsMap.set(env.key, env) } } else { diff --git a/packages/hoppscotch-common/src/helpers/utils/environments.ts b/packages/hoppscotch-common/src/helpers/utils/environments.ts index 2d952fc0..d197c384 100644 --- a/packages/hoppscotch-common/src/helpers/utils/environments.ts +++ b/packages/hoppscotch-common/src/helpers/utils/environments.ts @@ -40,10 +40,7 @@ const unWrapEnvironments = ( } return { ...globalVar, - currentValue: - currentVar?.currentValue ?? - globalVar.currentValue ?? - globalVar.initialValue, + currentValue: currentVar?.currentValue || globalVar.currentValue || "", } }) @@ -66,9 +63,7 @@ const unWrapEnvironments = ( return { ...selectedVar, currentValue: - currentVar?.currentValue ?? - selectedVar.currentValue ?? - selectedVar.initialValue, + currentVar?.currentValue || selectedVar.currentValue || "", } } ) diff --git a/packages/hoppscotch-common/src/newstore/environments.ts b/packages/hoppscotch-common/src/newstore/environments.ts index 80f7d1d6..f8b43ce1 100644 --- a/packages/hoppscotch-common/src/newstore/environments.ts +++ b/packages/hoppscotch-common/src/newstore/environments.ts @@ -427,6 +427,10 @@ export type AggregateEnvironment = { * Stream returning all the environment variables accessible in * the current state (Global + The Selected Environment). * NOTE: The source environment attribute will be "Global" for Global Env as source. + * The priority of the variables is as follows: + * 1. Pre-defined variables + * 2. Selected Environment Variables + * 3. Global Environment Variables */ export const aggregateEnvs$: Observable = combineLatest( [currentEnvironment$, globalEnv$] @@ -492,7 +496,7 @@ export function getAggregateEnvs() { const currentEnv = getCurrentEnvironment() return [ ...currentEnv.variables.map((x) => { - let currentValue + let currentValue = "" if (!x.secret) { currentValue = x.currentValue } @@ -506,7 +510,7 @@ export function getAggregateEnvs() { } }), ...getGlobalVariables().map((x) => { - let currentValue + let currentValue = "" if (!x.secret) { currentValue = x.currentValue } @@ -567,6 +571,7 @@ export const aggregateEnvsWithSecrets$: Observable = combineLatest([currentEnvironment$, globalEnv$]).pipe( map(([selectedEnv, globalEnv]) => { const results: AggregateEnvironment[] = [] + selectedEnv?.variables.map((x, index) => { let currentValue = x.currentValue if (x.secret) {