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
refactor: custom space for global variables
Andrew Bastined5b4b6dc527
diff --git a/helpers/preRequest.ts b/helpers/preRequest.tsindex c96d766a7..b0ce2a1f4 100644--- a/helpers/preRequest.ts+++ b/helpers/preRequest.ts@@ -1,6 +1,6 @@import {getCurrentEnvironment,- getGlobalEnvironment,+ getLegacyGlobalEnvironment,} from "~/newstore/environments"export default function getEnvironmentVariablesFromScript(script: string) {@@ -12,7 +12,7 @@ export default function getEnvironmentVariablesFromScript(script: string) {_variables[variable.key] = variable.value}- const globalEnv = getGlobalEnvironment()+ const globalEnv = getLegacyGlobalEnvironment()if (globalEnv) {for (const variable of globalEnv.variables) {diff --git a/newstore/environments.ts b/newstore/environments.tsindex b09c76abd..26770307b 100644--- a/newstore/environments.ts+++ b/newstore/environments.ts@@ -20,6 +20,8 @@ const defaultEnvironmentsState = {},] as Environment[],+ globals: [] as Environment["variables"],+// Current environment index specifies the index// -1 means no environments are selectedcurrentEnvironmentIndex: -1,@@ -188,6 +190,40 @@ const dispatchers = defineDispatchers({),}},+ setGlobalVariables(_, { entries }: { entries: Environment["variables"] }) {+ return {+ globals: entries,+ }+ },+ clearGlobalVariables() {+ return {+ globals: [],+ }+ },+ addGlobalVariable(+ { globals },+ { entry }: { entry: Environment["variables"][number] }+ ) {+ return {+ globals: [...globals, entry],+ }+ },+ removeGlobalVariable({ globals }, { envIndex }: { envIndex: number }) {+ return {+ globals: globals.filter((_, i) => i !== envIndex),+ }+ },+ updateGlobalVariable(+ { globals },+ {+ envIndex,+ updatedEntry,+ }: { envIndex: number; updatedEntry: Environment["variables"][number] }+ ) {+ return {+ globals: globals.map((x, i) => (i !== envIndex ? x : updatedEntry)),+ }+ },})export const environmentsStore = new DispatchingStore(@@ -199,6 +235,8 @@ export const environments$ = environmentsStore.subject$.pipe(pluck("environments"))+export const globalEnv$ = environmentsStore.subject$.pipe(pluck("globalEnv"))+export const selectedEnvIndex$ = environmentsStore.subject$.pipe(pluck("currentEnvironmentIndex"))@@ -243,7 +281,7 @@ export function setCurrentEnvironment(newEnvIndex: number) {})}-export function getGlobalEnvironment(): Environment | null {+export function getLegacyGlobalEnvironment(): Environment | null {const envs = environmentsStore.value.environmentsconst el = envs.find(@@ -253,6 +291,57 @@ export function getGlobalEnvironment(): Environment | null {return el ?? null}+export function getGlobalVariables(): Environment["variables"] {+ return environmentsStore.value.globals+}++export function addGlobalEnvVariable(entry: Environment["variables"][number]) {+ environmentsStore.dispatch({+ dispatcher: "addGlobalVariable",+ payload: {+ entry,+ },+ })+}++export function setGlobalEnvVariables(entries: Environment["variables"]) {+ environmentsStore.dispatch({+ dispatcher: "setGlobalVariables",+ payload: {+ entries,+ },+ })+}++export function clearGlobalEnvVariables() {+ environmentsStore.dispatch({+ dispatcher: "clearGlobalVariables",+ payload: {},+ })+}++export function removeGlobalEnvVariable(envIndex: number) {+ environmentsStore.dispatch({+ dispatcher: "removeGlobalVariable",+ payload: {+ envIndex,+ },+ })+}++export function updateGlobalEnvVariable(+ envIndex: number,+ updatedEntry: Environment["variables"][number]+) {+ environmentsStore.dispatch({+ dispatcher: "updateGlobalVariable",+ payload: {+ envIndex,+ updatedEntry,+ },+ })+}+export function replaceEnvironments(newEnvironments: any[]) {environmentsStore.dispatch({dispatcher: "replaceEnvironments",