Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

feat: global env variable migration and local persistence

Andrew Bastinb968756be22c
diff --git a/newstore/localpersistence.ts b/newstore/localpersistence.ts
index f425378ca..e59ecead4 100644
--- a/newstore/localpersistence.ts
+++ b/newstore/localpersistence.ts
@@ -23,7 +23,14 @@ import {
setGraphqlCollections,
setRESTCollections,
} from "./collections"
-import { replaceEnvironments, environments$ } from "./environments"
+import {
+ replaceEnvironments,
+ environments$,
+ Environment,
+ addGlobalEnvVariable,
+ setGlobalEnvVariables,
+ globalEnv$,
+} from "./environments"
function checkAndMigrateOldSettings() {
const vuexData = JSON.parse(window.localStorage.getItem("vuex") || "{}")
@@ -142,10 +149,29 @@ function setupCollectionsPersistence() {
}
function setupEnvironmentsPersistence() {
- const environmentsData = JSON.parse(
+ const environmentsData: Environment[] = JSON.parse(
window.localStorage.getItem("environments") || "[]"
)
+ // Check if a global env is defined and if so move that to globals
+ const globalIndex = environmentsData.findIndex(
+ (x) => x.name.toLowerCase() === "globals"
+ )
+
+ if (globalIndex !== -1) {
+ const globalEnv = environmentsData[globalIndex]
+ globalEnv.variables.forEach((variable) => addGlobalEnvVariable(variable))
+
+ // Remove global from environments
+ environmentsData.splice(globalIndex, 1)
+
+ // Just sync the changes manually
+ window.localStorage.setItem(
+ "environments",
+ JSON.stringify(environmentsData)
+ )
+ }
+
replaceEnvironments(environmentsData)
environments$.subscribe((envs) => {
@@ -153,12 +179,25 @@ function setupEnvironmentsPersistence() {
})
}
+function setupGlobalEnvsPersistence() {
+ const globals: Environment["variables"] = JSON.parse(
+ window.localStorage.getItem("globalEnv") || "[]"
+ )
+
+ setGlobalEnvVariables(globals)
+
+ globalEnv$.subscribe((vars) => {
+ window.localStorage.setItem("globalEnvs", JSON.stringify(vars))
+ })
+}
+
export function setupLocalPersistence() {
checkAndMigrateOldSettings()
setupSettingsPersistence()
setupHistoryPersistence()
setupCollectionsPersistence()
+ setupGlobalEnvsPersistence()
setupEnvironmentsPersistence()
}