feat: fix issue with the pre-request envs
This commit is contained in:
@@ -307,7 +307,8 @@ watch(loading, () => {
|
|||||||
const newSendRequest = async () => {
|
const newSendRequest = async () => {
|
||||||
loading.value = true
|
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)
|
// TODO: What if stream fetching failed (script execution errors ?) (isLeft)
|
||||||
if (isRight(streamResult)) {
|
if (isRight(streamResult)) {
|
||||||
|
|||||||
@@ -4,7 +4,10 @@ import { chain, right, TaskEither } from "fp-ts/lib/TaskEither"
|
|||||||
import { pipe } from "fp-ts/lib/function"
|
import { pipe } from "fp-ts/lib/function"
|
||||||
import { runTestScript, TestDescriptor } from "@hoppscotch/js-sandbox"
|
import { runTestScript, TestDescriptor } from "@hoppscotch/js-sandbox"
|
||||||
import { isRight } from "fp-ts/lib/Either"
|
import { isRight } from "fp-ts/lib/Either"
|
||||||
import { getFinalEnvsFromPreRequest } from "./preRequest"
|
import {
|
||||||
|
getCombinedEnvVariables,
|
||||||
|
getFinalEnvsFromPreRequest,
|
||||||
|
} from "./preRequest"
|
||||||
import { getEffectiveRESTRequest } from "./utils/EffectiveURL"
|
import { getEffectiveRESTRequest } from "./utils/EffectiveURL"
|
||||||
import { HoppRESTResponse } from "./types/HoppRESTResponse"
|
import { HoppRESTResponse } from "./types/HoppRESTResponse"
|
||||||
import { createRESTNetworkRequestStream } from "./network"
|
import { createRESTNetworkRequestStream } from "./network"
|
||||||
@@ -25,43 +28,46 @@ const getTestableBody = (res: HoppRESTResponse & { type: "success" }) => {
|
|||||||
return JSON.parse(rawBody)
|
return JSON.parse(rawBody)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const runRESTRequest$: TaskEither<
|
export const runRESTRequest$ = (): TaskEither<
|
||||||
string,
|
string,
|
||||||
Observable<HoppRESTResponse>
|
Observable<HoppRESTResponse>
|
||||||
> = pipe(
|
> =>
|
||||||
getFinalEnvsFromPreRequest(),
|
pipe(
|
||||||
chain((envs) => {
|
getFinalEnvsFromPreRequest(
|
||||||
console.log(envs)
|
getRESTRequest().preRequestScript,
|
||||||
const effectiveRequest = getEffectiveRESTRequest(getRESTRequest(), {
|
getCombinedEnvVariables()
|
||||||
name: "Env",
|
),
|
||||||
variables: envs,
|
chain((envs) => {
|
||||||
})
|
const effectiveRequest = getEffectiveRESTRequest(getRESTRequest(), {
|
||||||
|
name: "Env",
|
||||||
const stream = createRESTNetworkRequestStream(effectiveRequest)
|
variables: envs,
|
||||||
|
|
||||||
// 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)
|
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(
|
function translateToSandboxTestResults(
|
||||||
testDesc: TestDescriptor
|
testDesc: TestDescriptor
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import {
|
|||||||
getCurrentEnvironment,
|
getCurrentEnvironment,
|
||||||
getGlobalVariables,
|
getGlobalVariables,
|
||||||
} from "~/newstore/environments"
|
} from "~/newstore/environments"
|
||||||
import { getRESTRequest } from "~/newstore/RESTSession"
|
|
||||||
|
|
||||||
export const getCombinedEnvVariables = () => {
|
export const getCombinedEnvVariables = () => {
|
||||||
const variables: { key: string; value: string }[] = [...getGlobalVariables()]
|
const variables: { key: string; value: string }[] = [...getGlobalVariables()]
|
||||||
@@ -24,8 +23,7 @@ export const getCombinedEnvVariables = () => {
|
|||||||
return variables
|
return variables
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getFinalEnvsFromPreRequest = () =>
|
export const getFinalEnvsFromPreRequest = (
|
||||||
runPreRequestScript(
|
script: string,
|
||||||
getRESTRequest().preRequestScript,
|
envs: { key: string; value: string }[]
|
||||||
getCombinedEnvVariables()
|
) => runPreRequestScript(script, envs)
|
||||||
)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user