From b968756be22c9d6d7c10479c7e9f4da9376ee131 Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Tue, 17 Aug 2021 14:18:00 +0530 Subject: [PATCH] feat: global env variable migration and local persistence --- newstore/localpersistence.ts | 43 ++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) 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() }