Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia
TypeScript apiapi-clientapi-restapi-testingdeveloper-toolsgraphqlhttphttp-clientpwarestrest-apispatestingtesting-toolstoolsvuevuejswebsocket
chore: use initial value in runtime if current value is empty (#5162)
Nivedin2e9f0400f93d
diff --git a/packages/hoppscotch-common/src/components/environments/my/Details.vue b/packages/hoppscotch-common/src/components/environments/my/Details.vueindex 47d7b2e48..44b7a9474 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.vueindex 3759da777..1d696fb42 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.tsindex eebd69b11..a9a45fb3d 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<string, Environment["variables"][number]>()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<SandboxTestResult>) {v: 2,variables: globalEnvVariables,})- updateEnvironments(+ const selectedEnvVariables = updateEnvironments(// @ts-expect-error Typescript can't figure out this inference for some reasoncloneDeep(runResult.right.envs.selected),"selected"@@ -516,7 +542,7 @@ function updateEnvsAfterTestScript(runResult: E.Right<SandboxTestResult>) {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<SandboxTestResult>) {})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.tsindex 1c817f627..a335fc740 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.tsindex 2d952fc03..d197c3840 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.tsindex 80f7d1d6d..f8b43ce17 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<AggregateEnvironment[]> = 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<AggregateEnvironment[]> =combineLatest([currentEnvironment$, globalEnv$]).pipe(map(([selectedEnv, globalEnv]) => {const results: AggregateEnvironment[] = []+selectedEnv?.variables.map((x, index) => {let currentValue = x.currentValueif (x.secret) {