feat: fix issue with the pre-request envs
This commit is contained in:
@@ -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)) {
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
|
||||
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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user