From 680937e50b69ba3a08cfa7c49336e6bd11cb9e3b Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Sat, 25 Sep 2021 21:19:52 +0530 Subject: [PATCH] feat: fix issue with the pre-request envs --- .../components/http/Request.vue | 3 +- .../hoppscotch-app/helpers/RequestRunner.ts | 74 ++++++++++--------- packages/hoppscotch-app/helpers/preRequest.ts | 10 +-- 3 files changed, 46 insertions(+), 41 deletions(-) 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 -> = pipe( - getFinalEnvsFromPreRequest(), - chain((envs) => { - console.log(envs) - const effectiveRequest = getEffectiveRESTRequest(getRESTRequest(), { - name: "Env", - variables: envs, - }) - - 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, - })() - - // TODO: Handle script executation fails (isLeft) - if (isRight(runResult)) { - setRESTTestResults(translateToSandboxTestResults(runResult.right)) - } - - subscription.unsubscribe() - } +> => + pipe( + getFinalEnvsFromPreRequest( + getRESTRequest().preRequestScript, + getCombinedEnvVariables() + ), + chain((envs) => { + const effectiveRequest = getEffectiveRESTRequest(getRESTRequest(), { + name: "Env", + variables: envs, }) - return right(stream) - }) -) + 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, + })() + + // TODO: Handle script executation fails (isLeft) + if (isRight(runResult)) { + setRESTTestResults(translateToSandboxTestResults(runResult.right)) + } + + subscription.unsubscribe() + } + }) + + 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)