diff --git a/packages/hoppscotch-common/src/components/http/Codegen.vue b/packages/hoppscotch-common/src/components/http/Codegen.vue index 4727f02a..351739cf 100644 --- a/packages/hoppscotch-common/src/components/http/Codegen.vue +++ b/packages/hoppscotch-common/src/components/http/Codegen.vue @@ -131,7 +131,7 @@ import { getEffectiveRESTRequest, resolvesEnvsInBody, } from "~/helpers/utils/EffectiveURL" -import { getAggregateEnvs } from "~/newstore/environments" +import { AggregateEnvironment, getAggregateEnvs } from "~/newstore/environments" import { useService } from "dioc/vue" import cloneDeep from "lodash-es/cloneDeep" @@ -144,10 +144,13 @@ import IconCheck from "~icons/lucide/check" import IconWrapText from "~icons/lucide/wrap-text" import { asyncComputed } from "@vueuse/core" import { getDefaultRESTRequest } from "~/helpers/rest/default" +import { CurrentValueService } from "~/services/current-environment-value.service" +import { getCurrentEnvironment } from "../../newstore/environments" const t = useI18n() const tabs = useService(RESTTabService) +const currentEnvironmentValueService = useService(CurrentValueService) // Get the current active request if the current active tab is a request else get the original request from the response tab const currentActiveRequest = computed(() => { @@ -187,6 +190,18 @@ const emit = defineEmits<{ (e: "request-code", value: string): void }>() +const getCurrentValue = (env: AggregateEnvironment) => { + const currentSelectedEnvironment = getCurrentEnvironment() + + if (env && env.secret) { + return env.currentValue + } + return currentEnvironmentValueService.getEnvironmentByKey( + env?.sourceEnv !== "Global" ? currentSelectedEnvironment.id : "Global", + env?.key ?? "" + )?.currentValue +} + const requestCode = asyncComputed(async () => { // Generate code snippet action only applies to request documents if (currentActiveTabDocument.value.type !== "request") { @@ -200,19 +215,23 @@ const requestCode = asyncComputed(async () => { if (requestVariable.active) return { key: requestVariable.key, - value: requestVariable.value, + currentValue: requestVariable.value, + initialValue: requestVariable.value, secret: false, } return {} } ) const env: Environment = { - v: 1, + v: 2, id: "env", name: "Env", variables: [ ...(requestVariables as Environment["variables"]), - ...aggregateEnvs, + ...aggregateEnvs.map((env) => ({ + ...env, + currentValue: getCurrentValue(env) ?? env.currentValue, + })), ], } diff --git a/packages/hoppscotch-selfhost-desktop/src/platform/environments/environments.platform.ts b/packages/hoppscotch-selfhost-desktop/src/platform/environments/environments.platform.ts index 8d504222..95a741b3 100644 --- a/packages/hoppscotch-selfhost-desktop/src/platform/environments/environments.platform.ts +++ b/packages/hoppscotch-selfhost-desktop/src/platform/environments/environments.platform.ts @@ -25,6 +25,12 @@ import { runUserEnvironmentDeletedSubscription, runUserEnvironmentUpdatedSubscription, } from "@platform/environments/environments.api" +import { entityReference } from "verzod" +import { + Environment, + EnvironmentSchemaVersion, + GlobalEnvironment, +} from "@hoppscotch/data" export function initEnvironmentsSync() { const currentUser$ = platformAuth.getCurrentUserStream() @@ -79,12 +85,27 @@ async function loadUserEnvironments() { if (environments.length > 0) { runDispatchWithOutSyncing(() => { + const formatedEnvironments = environments.map( + (env) => + { + id: env.id, + name: env.name, + variables: JSON.parse(env.variables), + } + ) + replaceEnvironments( - environments.map(({ id, variables, name }) => ({ - id, - name, - variables: JSON.parse(variables), - })) + formatedEnvironments.map((environment) => { + const parsedEnv = + entityReference(Environment).safeParse(environment) + + return parsedEnv.success + ? parsedEnv.data + : { + ...environment, + v: EnvironmentSchemaVersion, + } + }) ) }) } @@ -98,8 +119,16 @@ async function loadGlobalEnvironments() { const globalEnv = res.right.me.globalEnvironments if (globalEnv) { + const globalEnvVariableEntries = JSON.parse(globalEnv.variables) + + const result = entityReference(GlobalEnvironment).safeParse( + globalEnvVariableEntries + ) + runDispatchWithOutSyncing(() => { - setGlobalEnvVariables(JSON.parse(globalEnv.variables)) + setGlobalEnvVariables( + result.success ? result.data : globalEnvVariableEntries + ) setGlobalEnvID(globalEnv.id) }) }