Skip to content

Commit 2e9f040

Browse files
authored
chore: use initial value in runtime if current value is empty (hoppscotch#5162)
1 parent f655035 commit 2e9f040

File tree

6 files changed

+68
-45
lines changed

6 files changed

+68
-45
lines changed

packages/hoppscotch-common/src/components/environments/my/Details.vue

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -492,21 +492,12 @@ const saveEnvironment = () => {
492492
493493
const variables = pipe(
494494
filteredVariables,
495-
A.map((e) =>
496-
e.secret
497-
? {
498-
key: e.key,
499-
secret: e.secret,
500-
initialValue: e.initialValue,
501-
currentValue: "",
502-
}
503-
: {
504-
key: e.key,
505-
secret: e.secret,
506-
initialValue: e.initialValue,
507-
currentValue: "",
508-
}
509-
)
495+
A.map((e) => ({
496+
key: e.key,
497+
secret: e.secret,
498+
initialValue: e.initialValue || "",
499+
currentValue: "",
500+
}))
510501
)
511502
512503
const environmentUpdated: Environment = {

packages/hoppscotch-common/src/components/environments/teams/Details.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ const saveEnvironment = async () => {
437437
A.map((e) => ({
438438
key: e.key,
439439
secret: e.secret,
440-
initialValue: e.initialValue,
440+
initialValue: e.initialValue || "",
441441
currentValue: "",
442442
}))
443443
)

packages/hoppscotch-common/src/helpers/RequestRunner.ts

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ export const getTestableBody = (
100100
return x
101101
}
102102

103+
/**
104+
* Combines the environment variables from the request and the selected, global, and temporary environments.
105+
* The priority is as follows:
106+
* 1. Request variables
107+
* 2. Temporary variables (if any)
108+
* 3. Selected environment variables
109+
* 4. Global environment variables
110+
* @param variables The environment variables to combine
111+
* @returns The combined environment variables
112+
*/
103113
export const combineEnvVariables = (variables: {
104114
environments: {
105115
selected: Environment["variables"]
@@ -119,8 +129,8 @@ export const executedResponses$ = new Subject<
119129
>()
120130

121131
/**
122-
* Used to update the environment schema with the secret variables
123-
* and store the secret variable values in the secret environment service
132+
* This will update the environment variables in the current environment
133+
* and secret environment service.
124134
* @param envs The environment variables to update
125135
* @param type Whether the environment variables are global or selected
126136
* @returns the updated environment variables
@@ -216,8 +226,24 @@ const getEnvironmentVariableValue = (
216226
)
217227
}
218228

229+
/**
230+
* Set currentValue as initialValue if currentValue is empty
231+
* This is set just for request runtime and it will not be persisted.
232+
* @param env The environment variable to be transformed
233+
* @returns The transformed environment variable with currentValue set to initialValue if empty
234+
*/
235+
const getTransformedEnvs = (
236+
env: Environment["variables"][number]
237+
): Environment["variables"][number] => {
238+
return {
239+
...env,
240+
currentValue: env.currentValue || env.initialValue,
241+
}
242+
}
243+
219244
/**
220245
* Transforms the environment list to a list with unique keys with value
246+
* and set currentValue as initialValue if currentValue is empty.
221247
* @param envs The environment list to be transformed
222248
* @returns The transformed environment list with keys with value
223249
*/
@@ -226,21 +252,21 @@ const filterNonEmptyEnvironmentVariables = (
226252
): Environment["variables"] => {
227253
const envsMap = new Map<string, Environment["variables"][number]>()
228254
envs.forEach((env) => {
229-
if (env.secret) {
230-
envsMap.set(env.key, env)
231-
} else if (envsMap.has(env.key)) {
232-
const existingEnv = envsMap.get(env.key)
255+
const transformedEnv = getTransformedEnvs(env)
256+
257+
if (envsMap.has(transformedEnv.key)) {
258+
const existingEnv = envsMap.get(transformedEnv.key)
233259

234260
if (
235261
existingEnv &&
236262
"currentValue" in existingEnv &&
237263
existingEnv.currentValue === "" &&
238-
env.currentValue !== ""
264+
transformedEnv.currentValue !== ""
239265
) {
240-
envsMap.set(env.key, env)
266+
envsMap.set(transformedEnv.key, transformedEnv)
241267
}
242268
} else {
243-
envsMap.set(env.key, env)
269+
envsMap.set(transformedEnv.key, transformedEnv)
244270
}
245271
})
246272

@@ -502,7 +528,7 @@ function updateEnvsAfterTestScript(runResult: E.Right<SandboxTestResult>) {
502528
v: 2,
503529
variables: globalEnvVariables,
504530
})
505-
updateEnvironments(
531+
const selectedEnvVariables = updateEnvironments(
506532
// @ts-expect-error Typescript can't figure out this inference for some reason
507533
cloneDeep(runResult.right.envs.selected),
508534
"selected"
@@ -516,7 +542,7 @@ function updateEnvsAfterTestScript(runResult: E.Right<SandboxTestResult>) {
516542
name: env.name,
517543
v: 2,
518544
id: "id" in env ? env.id : "",
519-
variables: runResult.right.envs.selected,
545+
variables: selectedEnvVariables,
520546
})
521547
} else if (
522548
environmentsStore.value.selectedEnvironmentIndex.type === "TEAM_ENV"
@@ -526,7 +552,7 @@ function updateEnvsAfterTestScript(runResult: E.Right<SandboxTestResult>) {
526552
})
527553
pipe(
528554
updateTeamEnvironment(
529-
JSON.stringify(runResult.right.envs.selected),
555+
JSON.stringify(selectedEnvVariables),
530556
environmentsStore.value.selectedEnvironmentIndex.teamEnvID,
531557
env.name
532558
)
@@ -565,13 +591,15 @@ export function runTestRunnerRequest(
565591
id: "env-id",
566592
v: 2,
567593
name: "Env",
568-
variables: combineEnvVariables({
569-
environments: {
570-
...preRequestScriptResult.right.envs,
571-
temp: !persistEnv ? getTemporaryVariables() : [],
572-
},
573-
requestVariables: [],
574-
}),
594+
variables: filterNonEmptyEnvironmentVariables(
595+
combineEnvVariables({
596+
environments: {
597+
...preRequestScriptResult.right.envs,
598+
temp: !persistEnv ? getTemporaryVariables() : [],
599+
},
600+
requestVariables: [],
601+
})
602+
),
575603
})
576604

577605
const [stream] = createRESTNetworkRequestStream(effectiveRequest)

packages/hoppscotch-common/src/helpers/editor/extensions/HoppEnvironment.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ const filterNonEmptyEnvironmentVariables = (
5757
if (envsMap.has(env.key)) {
5858
const existingEnv = envsMap.get(env.key)
5959

60-
if (existingEnv?.currentValue === "" && env.currentValue !== "") {
60+
if (
61+
existingEnv?.currentValue === "" &&
62+
existingEnv?.initialValue === "" &&
63+
(env.currentValue || env.initialValue)
64+
) {
6165
envsMap.set(env.key, env)
6266
}
6367
} else {

packages/hoppscotch-common/src/helpers/utils/environments.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ const unWrapEnvironments = (
4040
}
4141
return {
4242
...globalVar,
43-
currentValue:
44-
currentVar?.currentValue ??
45-
globalVar.currentValue ??
46-
globalVar.initialValue,
43+
currentValue: currentVar?.currentValue || globalVar.currentValue || "",
4744
}
4845
})
4946

@@ -66,9 +63,7 @@ const unWrapEnvironments = (
6663
return {
6764
...selectedVar,
6865
currentValue:
69-
currentVar?.currentValue ??
70-
selectedVar.currentValue ??
71-
selectedVar.initialValue,
66+
currentVar?.currentValue || selectedVar.currentValue || "",
7267
}
7368
}
7469
)

packages/hoppscotch-common/src/newstore/environments.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,10 @@ export type AggregateEnvironment = {
427427
* Stream returning all the environment variables accessible in
428428
* the current state (Global + The Selected Environment).
429429
* NOTE: The source environment attribute will be "Global" for Global Env as source.
430+
* The priority of the variables is as follows:
431+
* 1. Pre-defined variables
432+
* 2. Selected Environment Variables
433+
* 3. Global Environment Variables
430434
*/
431435
export const aggregateEnvs$: Observable<AggregateEnvironment[]> = combineLatest(
432436
[currentEnvironment$, globalEnv$]
@@ -492,7 +496,7 @@ export function getAggregateEnvs() {
492496
const currentEnv = getCurrentEnvironment()
493497
return [
494498
...currentEnv.variables.map((x) => {
495-
let currentValue
499+
let currentValue = ""
496500
if (!x.secret) {
497501
currentValue = x.currentValue
498502
}
@@ -506,7 +510,7 @@ export function getAggregateEnvs() {
506510
}
507511
}),
508512
...getGlobalVariables().map((x) => {
509-
let currentValue
513+
let currentValue = ""
510514
if (!x.secret) {
511515
currentValue = x.currentValue
512516
}
@@ -567,6 +571,7 @@ export const aggregateEnvsWithSecrets$: Observable<AggregateEnvironment[]> =
567571
combineLatest([currentEnvironment$, globalEnv$]).pipe(
568572
map(([selectedEnv, globalEnv]) => {
569573
const results: AggregateEnvironment[] = []
574+
570575
selectedEnv?.variables.map((x, index) => {
571576
let currentValue = x.currentValue
572577
if (x.secret) {

0 commit comments

Comments
 (0)