diff --git a/packages/hoppscotch-common/src/helpers/RequestRunner.ts b/packages/hoppscotch-common/src/helpers/RequestRunner.ts index 0ca299051..2b57fb3d7 100644 --- a/packages/hoppscotch-common/src/helpers/RequestRunner.ts +++ b/packages/hoppscotch-common/src/helpers/RequestRunner.ts @@ -1,4 +1,8 @@ -import { Environment } from "@hoppscotch/data" +import { + Environment, + HoppRESTHeaders, + HoppRESTRequestVariable, +} from "@hoppscotch/data" import { SandboxTestResult, TestDescriptor } from "@hoppscotch/js-sandbox" import { runTestScript } from "@hoppscotch/js-sandbox/web" import * as A from "fp-ts/Array" @@ -65,10 +69,17 @@ const getTestableBody = ( return x } -const combineEnvVariables = (envs: { - global: Environment["variables"] - selected: Environment["variables"] -}) => [...envs.selected, ...envs.global] +const combineEnvVariables = (variables: { + environments: { + global: Environment["variables"] + selected: Environment["variables"] + } + requestVariables: Environment["variables"] +}) => [ + ...variables.requestVariables, + ...variables.environments.global, + ...variables.environments.selected, +] export const executedResponses$ = new Subject< HoppRESTResponse & { type: "success" | "fail " } @@ -175,15 +186,36 @@ export function runRESTRequest$( requestHeaders = [...tab.value.document.request.headers] } + const finalRequestVariables = + tab.value.document.request.requestVariables.map( + (v: HoppRESTRequestVariable) => { + if (v.active) { + return { + key: v.key, + value: v.value, + secret: false, + } + } + return [] + } + ) + const finalRequest = { ...tab.value.document.request, auth: requestAuth ?? { authType: "none", authActive: false }, - headers: requestHeaders, + headers: requestHeaders as HoppRESTHeaders, + } + + const finalEnvs = { + environments: envs.right, + requestVariables: finalRequestVariables as Environment["variables"], } const effectiveRequest = getEffectiveRESTRequest(finalRequest, { + id: "env-id", + v: 1, name: "Env", - variables: combineEnvVariables(envs.right), + variables: combineEnvVariables(finalEnvs), }) const [stream, cancelRun] = createRESTNetworkRequestStream(effectiveRequest) diff --git a/packages/hoppscotch-common/src/helpers/utils/EffectiveURL.ts b/packages/hoppscotch-common/src/helpers/utils/EffectiveURL.ts index d287976ee..f4adbcfff 100644 --- a/packages/hoppscotch-common/src/helpers/utils/EffectiveURL.ts +++ b/packages/hoppscotch-common/src/helpers/utils/EffectiveURL.ts @@ -22,7 +22,6 @@ import { import { arrayFlatMap, arraySort } from "../functional/array" import { toFormData } from "../functional/formData" import { tupleWithSameKeysToRecord } from "../functional/record" -import { getGlobalVariables } from "~/newstore/environments" export interface EffectiveHoppRESTRequest extends HoppRESTRequest { /** @@ -34,6 +33,7 @@ export interface EffectiveHoppRESTRequest extends HoppRESTRequest { effectiveFinalHeaders: { key: string; value: string }[] effectiveFinalParams: { key: string; value: string }[] effectiveFinalBody: FormData | string | null + effectiveFinalRequestVariables: { key: string; value: string }[] } /** @@ -313,38 +313,53 @@ export function getEffectiveRESTRequest( request: HoppRESTRequest, environment: Environment ): EffectiveHoppRESTRequest { - const envVariables = [...environment.variables, ...getGlobalVariables()] - const effectiveFinalHeaders = pipe( - getComputedHeaders(request, envVariables).map((h) => h.header), + getComputedHeaders(request, environment.variables).map((h) => h.header), A.concat(request.headers), A.filter((x) => x.active && x.key !== ""), A.map((x) => ({ active: true, - key: parseTemplateString(x.key, envVariables), - value: parseTemplateString(x.value, envVariables), + key: parseTemplateString(x.key, environment.variables), + value: parseTemplateString(x.value, environment.variables), })) ) const effectiveFinalParams = pipe( - getComputedParams(request, envVariables).map((p) => p.param), + getComputedParams(request, environment.variables).map((p) => p.param), A.concat(request.params), A.filter((x) => x.active && x.key !== ""), A.map((x) => ({ active: true, - key: parseTemplateString(x.key, envVariables), - value: parseTemplateString(x.value, envVariables), + key: parseTemplateString(x.key, environment.variables), + value: parseTemplateString(x.value, environment.variables), })) ) - const effectiveFinalBody = getFinalBodyFromRequest(request, envVariables) + const effectiveFinalRequestVariables = pipe( + request.requestVariables, + A.filter((x) => x.active && x.key !== ""), + A.map((x) => ({ + active: true, + key: parseTemplateString(x.key, environment.variables), + value: parseTemplateString(x.value, environment.variables), + })) + ) + + const effectiveFinalBody = getFinalBodyFromRequest( + request, + environment.variables + ) return { ...request, - effectiveFinalURL: parseTemplateString(request.endpoint, envVariables), + effectiveFinalURL: parseTemplateString( + request.endpoint, + environment.variables + ), effectiveFinalHeaders, effectiveFinalParams, effectiveFinalBody, + effectiveFinalRequestVariables, } }