Skip to content

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

Inject globals env into pre-request scripts

Andrew Bastinddbc9ca61bc8
diff --git a/helpers/preRequest.ts b/helpers/preRequest.ts
index d8dd5466d..d8b10e9e4 100644
--- a/helpers/preRequest.ts
+++ b/helpers/preRequest.ts
@@ -1,4 +1,7 @@
-import { getCurrentEnvironment } from "~/newstore/environments"
+import {
+ getCurrentEnvironment,
+ getGlobalEnvironment,
+} from "~/newstore/environments"
export default function getEnvironmentVariablesFromScript(script: any) {
const _variables: Record<string, string> = {}
@@ -9,6 +12,14 @@ export default function getEnvironmentVariablesFromScript(script: any) {
_variables[variable.key] = variable.value
}
+ const globalEnv = getGlobalEnvironment()
+
+ if (globalEnv) {
+ for (const variable of globalEnv.variables) {
+ _variables[variable.key] = variable.value
+ }
+ }
+
try {
// the pw object is the proxy by which pre-request scripts can pass variables to the request.
// for security and control purposes, this is the only way a pre-request script should modify variables.
diff --git a/newstore/environments.ts b/newstore/environments.ts
index 4010a9be2..8702bf496 100644
--- a/newstore/environments.ts
+++ b/newstore/environments.ts
@@ -216,6 +216,16 @@ export function setCurrentEnvironment(newEnvIndex: number) {
})
}
+export function getGlobalEnvironment(): Environment | null {
+ const envs = environmentsStore.value.environments
+
+ const el = envs.find(
+ (env) => env.name === "globals" || env.name === "Globals"
+ )
+
+ return el ?? null
+}
+
export function replaceEnvironments(newEnvironments: any[]) {
environmentsStore.dispatch({
dispatcher: "replaceEnvironments",