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: fix issue with the pre-request envs

Andrew Bastin680937e50b69
diff --git a/packages/hoppscotch-app/components/http/Request.vue b/packages/hoppscotch-app/components/http/Request.vue
index 31c9c6f01..364625146 100644
--- a/packages/hoppscotch-app/components/http/Request.vue
+++ b/packages/hoppscotch-app/components/http/Request.vue
@@ -307,7 +307,8 @@ watch(loading, () => {
const newSendRequest = async () => {
loading.value = true
- const streamResult = await runRESTRequest$()
+ // Double calling is because the function returns a TaskEither than should be executed
+ const streamResult = await runRESTRequest$()()
// TODO: What if stream fetching failed (script execution errors ?) (isLeft)
if (isRight(streamResult)) {
diff --git a/packages/hoppscotch-app/helpers/RequestRunner.ts b/packages/hoppscotch-app/helpers/RequestRunner.ts
index d4450c8ed..62fb0379e 100644
--- a/packages/hoppscotch-app/helpers/RequestRunner.ts
+++ b/packages/hoppscotch-app/helpers/RequestRunner.ts
@@ -4,7 +4,10 @@ import { chain, right, TaskEither } from "fp-ts/lib/TaskEither"
import { pipe } from "fp-ts/lib/function"
import { runTestScript, TestDescriptor } from "@hoppscotch/js-sandbox"
import { isRight } from "fp-ts/lib/Either"
-import { getFinalEnvsFromPreRequest } from "./preRequest"
+import {
+ getCombinedEnvVariables,
+ getFinalEnvsFromPreRequest,
+} from "./preRequest"
import { getEffectiveRESTRequest } from "./utils/EffectiveURL"
import { HoppRESTResponse } from "./types/HoppRESTResponse"
import { createRESTNetworkRequestStream } from "./network"
@@ -25,43 +28,46 @@ const getTestableBody = (res: HoppRESTResponse & { type: "success" }) => {
return JSON.parse(rawBody)
}
-export const runRESTRequest$: TaskEither<
+export const runRESTRequest$ = (): TaskEither<
string,
Observable<HoppRESTResponse>
-> = pipe(
- getFinalEnvsFromPreRequest(),
- chain((envs) => {
- console.log(envs)
- const effectiveRequest = getEffectiveRESTRequest(getRESTRequest(), {
- name: "Env",
- variables: envs,
- })
+> =>
+ pipe(
+ getFinalEnvsFromPreRequest(
+ getRESTRequest().preRequestScript,
+ getCombinedEnvVariables()
+ ),
+ chain((envs) => {
+ const effectiveRequest = getEffectiveRESTRequest(getRESTRequest(), {
+ name: "Env",
+ variables: envs,
+ })
- const stream = createRESTNetworkRequestStream(effectiveRequest)
+ const stream = createRESTNetworkRequestStream(effectiveRequest)
- // Run Test Script when request ran successfully
- const subscription = stream
- .pipe(filter((res) => res.type === "success"))
- .subscribe(async (res) => {
- if (res.type === "success") {
- const runResult = await runTestScript(res.req.testScript, {
- status: res.statusCode,
- body: getTestableBody(res),
- headers: res.headers,
- })()
+ // Run Test Script when request ran successfully
+ const subscription = stream
+ .pipe(filter((res) => res.type === "success"))
+ .subscribe(async (res) => {
+ if (res.type === "success") {
+ const runResult = await runTestScript(res.req.testScript, {
+ status: res.statusCode,
+ body: getTestableBody(res),
+ headers: res.headers,
+ })()
- // TODO: Handle script executation fails (isLeft)
- if (isRight(runResult)) {
- setRESTTestResults(translateToSandboxTestResults(runResult.right))
- }
+ // TODO: Handle script executation fails (isLeft)
+ if (isRight(runResult)) {
+ setRESTTestResults(translateToSandboxTestResults(runResult.right))
+ }
- subscription.unsubscribe()
- }
- })
+ subscription.unsubscribe()
+ }
+ })
- return right(stream)
- })
-)
+ return right(stream)
+ })
+ )
function translateToSandboxTestResults(
testDesc: TestDescriptor
diff --git a/packages/hoppscotch-app/helpers/preRequest.ts b/packages/hoppscotch-app/helpers/preRequest.ts
index b5e53731b..984230795 100644
--- a/packages/hoppscotch-app/helpers/preRequest.ts
+++ b/packages/hoppscotch-app/helpers/preRequest.ts
@@ -3,7 +3,6 @@ import {
getCurrentEnvironment,
getGlobalVariables,
} from "~/newstore/environments"
-import { getRESTRequest } from "~/newstore/RESTSession"
export const getCombinedEnvVariables = () => {
const variables: { key: string; value: string }[] = [...getGlobalVariables()]
@@ -24,8 +23,7 @@ export const getCombinedEnvVariables = () => {
return variables
}
-export const getFinalEnvsFromPreRequest = () =>
- runPreRequestScript(
- getRESTRequest().preRequestScript,
- getCombinedEnvVariables()
- )
+export const getFinalEnvsFromPreRequest = (
+ script: string,
+ envs: { key: string; value: string }[]
+) => runPreRequestScript(script, envs)