Skip to content

Commit f8cd8bf

Browse files
authored
fix: desktop platform level env migration and codegen bug (hoppscotch#5110)
1 parent a325f29 commit f8cd8bf

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

packages/hoppscotch-common/src/components/http/Codegen.vue

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ import {
131131
getEffectiveRESTRequest,
132132
resolvesEnvsInBody,
133133
} from "~/helpers/utils/EffectiveURL"
134-
import { getAggregateEnvs } from "~/newstore/environments"
134+
import { AggregateEnvironment, getAggregateEnvs } from "~/newstore/environments"
135135
136136
import { useService } from "dioc/vue"
137137
import cloneDeep from "lodash-es/cloneDeep"
@@ -144,10 +144,13 @@ import IconCheck from "~icons/lucide/check"
144144
import IconWrapText from "~icons/lucide/wrap-text"
145145
import { asyncComputed } from "@vueuse/core"
146146
import { getDefaultRESTRequest } from "~/helpers/rest/default"
147+
import { CurrentValueService } from "~/services/current-environment-value.service"
148+
import { getCurrentEnvironment } from "../../newstore/environments"
147149
148150
const t = useI18n()
149151
150152
const tabs = useService(RESTTabService)
153+
const currentEnvironmentValueService = useService(CurrentValueService)
151154
152155
// Get the current active request if the current active tab is a request else get the original request from the response tab
153156
const currentActiveRequest = computed(() => {
@@ -187,6 +190,18 @@ const emit = defineEmits<{
187190
(e: "request-code", value: string): void
188191
}>()
189192
193+
const getCurrentValue = (env: AggregateEnvironment) => {
194+
const currentSelectedEnvironment = getCurrentEnvironment()
195+
196+
if (env && env.secret) {
197+
return env.currentValue
198+
}
199+
return currentEnvironmentValueService.getEnvironmentByKey(
200+
env?.sourceEnv !== "Global" ? currentSelectedEnvironment.id : "Global",
201+
env?.key ?? ""
202+
)?.currentValue
203+
}
204+
190205
const requestCode = asyncComputed(async () => {
191206
// Generate code snippet action only applies to request documents
192207
if (currentActiveTabDocument.value.type !== "request") {
@@ -200,19 +215,23 @@ const requestCode = asyncComputed(async () => {
200215
if (requestVariable.active)
201216
return {
202217
key: requestVariable.key,
203-
value: requestVariable.value,
218+
currentValue: requestVariable.value,
219+
initialValue: requestVariable.value,
204220
secret: false,
205221
}
206222
return {}
207223
}
208224
)
209225
const env: Environment = {
210-
v: 1,
226+
v: 2,
211227
id: "env",
212228
name: "Env",
213229
variables: [
214230
...(requestVariables as Environment["variables"]),
215-
...aggregateEnvs,
231+
...aggregateEnvs.map((env) => ({
232+
...env,
233+
currentValue: getCurrentValue(env) ?? env.currentValue,
234+
})),
216235
],
217236
}
218237

packages/hoppscotch-selfhost-desktop/src/platform/environments/environments.platform.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ import {
2525
runUserEnvironmentDeletedSubscription,
2626
runUserEnvironmentUpdatedSubscription,
2727
} from "@platform/environments/environments.api"
28+
import { entityReference } from "verzod"
29+
import {
30+
Environment,
31+
EnvironmentSchemaVersion,
32+
GlobalEnvironment,
33+
} from "@hoppscotch/data"
2834

2935
export function initEnvironmentsSync() {
3036
const currentUser$ = platformAuth.getCurrentUserStream()
@@ -79,12 +85,27 @@ async function loadUserEnvironments() {
7985

8086
if (environments.length > 0) {
8187
runDispatchWithOutSyncing(() => {
88+
const formatedEnvironments = environments.map(
89+
(env) =>
90+
<Environment>{
91+
id: env.id,
92+
name: env.name,
93+
variables: JSON.parse(env.variables),
94+
}
95+
)
96+
8297
replaceEnvironments(
83-
environments.map(({ id, variables, name }) => ({
84-
id,
85-
name,
86-
variables: JSON.parse(variables),
87-
}))
98+
formatedEnvironments.map((environment) => {
99+
const parsedEnv =
100+
entityReference(Environment).safeParse(environment)
101+
102+
return parsedEnv.success
103+
? parsedEnv.data
104+
: {
105+
...environment,
106+
v: EnvironmentSchemaVersion,
107+
}
108+
})
88109
)
89110
})
90111
}
@@ -98,8 +119,16 @@ async function loadGlobalEnvironments() {
98119
const globalEnv = res.right.me.globalEnvironments
99120

100121
if (globalEnv) {
122+
const globalEnvVariableEntries = JSON.parse(globalEnv.variables)
123+
124+
const result = entityReference(GlobalEnvironment).safeParse(
125+
globalEnvVariableEntries
126+
)
127+
101128
runDispatchWithOutSyncing(() => {
102-
setGlobalEnvVariables(JSON.parse(globalEnv.variables))
129+
setGlobalEnvVariables(
130+
result.success ? result.data : globalEnvVariableEntries
131+
)
103132
setGlobalEnvID(globalEnv.id)
104133
})
105134
}

0 commit comments

Comments
 (0)