feat: request variables (#3825)
Co-authored-by: jamesgeorge007 <jamesgeorge998001@gmail.com>
This commit is contained in:
@@ -10,6 +10,7 @@ import { Ref, markRaw } from "vue"
|
||||
import IconPlusCircle from "~icons/lucide/plus-circle"
|
||||
import { HoppRESTRequest } from "@hoppscotch/data"
|
||||
import {
|
||||
AggregateEnvironment,
|
||||
aggregateEnvsWithSecrets$,
|
||||
getCurrentEnvironment,
|
||||
getSelectedEnvironmentType,
|
||||
@@ -18,6 +19,7 @@ import { invokeAction } from "~/helpers/actions"
|
||||
import { computed } from "vue"
|
||||
import { useStreamStatic } from "~/composables/stream"
|
||||
import { SecretEnvironmentService } from "~/services/secret-environment.service"
|
||||
import { RESTTabService } from "~/services/tab/rest"
|
||||
|
||||
const HOPP_ENVIRONMENT_REGEX = /(<<[a-zA-Z0-9-_]+>>)/g
|
||||
|
||||
@@ -41,6 +43,7 @@ export class EnvironmentInspectorService extends Service implements Inspector {
|
||||
|
||||
private readonly inspection = this.bind(InspectionService)
|
||||
private readonly secretEnvs = this.bind(SecretEnvironmentService)
|
||||
private readonly restTabs = this.bind(RESTTabService)
|
||||
|
||||
private aggregateEnvsWithSecrets = useStreamStatic(
|
||||
aggregateEnvsWithSecrets$,
|
||||
@@ -68,7 +71,14 @@ export class EnvironmentInspectorService extends Service implements Inspector {
|
||||
) => {
|
||||
const newErrors: InspectorResult[] = []
|
||||
|
||||
const envKeys = this.aggregateEnvsWithSecrets.value.map((e) => e.key)
|
||||
const currentTab = this.restTabs.currentActiveTab.value
|
||||
|
||||
const environmentVariables = [
|
||||
...currentTab.document.request.requestVariables,
|
||||
...this.aggregateEnvsWithSecrets.value,
|
||||
]
|
||||
|
||||
const envKeys = environmentVariables.map((e) => e.key)
|
||||
|
||||
target.forEach((element, index) => {
|
||||
if (isENVInString(element)) {
|
||||
@@ -124,6 +134,31 @@ export class EnvironmentInspectorService extends Service implements Inspector {
|
||||
return newErrors
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms the environment list to a list with unique keys with value
|
||||
* @param envs The environment list to be transformed
|
||||
* @returns The transformed environment list with keys with value
|
||||
*/
|
||||
private filterNonEmptyEnvironmentVariables = (
|
||||
envs: AggregateEnvironment[]
|
||||
): AggregateEnvironment[] => {
|
||||
const envsMap = new Map<string, AggregateEnvironment>()
|
||||
|
||||
envs.forEach((env) => {
|
||||
if (envsMap.has(env.key)) {
|
||||
const existingEnv = envsMap.get(env.key)
|
||||
|
||||
if (existingEnv?.value === "" && env.value !== "") {
|
||||
envsMap.set(env.key, env)
|
||||
}
|
||||
} else {
|
||||
envsMap.set(env.key, env)
|
||||
}
|
||||
})
|
||||
|
||||
return Array.from(envsMap.values())
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the environment variables in the target array are empty
|
||||
* @param target The target array to validate
|
||||
@@ -145,7 +180,19 @@ export class EnvironmentInspectorService extends Service implements Inspector {
|
||||
const formattedExEnv = exEnv.slice(2, -2)
|
||||
const currentSelectedEnvironment = getCurrentEnvironment()
|
||||
|
||||
this.aggregateEnvsWithSecrets.value.forEach((env) => {
|
||||
const currentTab = this.restTabs.currentActiveTab.value
|
||||
|
||||
const environmentVariables =
|
||||
this.filterNonEmptyEnvironmentVariables([
|
||||
...currentTab.document.request.requestVariables.map((env) => ({
|
||||
...env,
|
||||
secret: false,
|
||||
sourceEnv: "RequestVariable",
|
||||
})),
|
||||
...this.aggregateEnvsWithSecrets.value,
|
||||
])
|
||||
|
||||
environmentVariables.forEach((env) => {
|
||||
const hasSecretEnv = this.secretEnvs.hasSecretValue(
|
||||
env.sourceEnv !== "Global"
|
||||
? currentSelectedEnvironment.id
|
||||
@@ -199,14 +246,19 @@ export class EnvironmentInspectorService extends Service implements Inspector {
|
||||
"inspections.environment.add_environment_value"
|
||||
),
|
||||
apply: () => {
|
||||
invokeAction(invokeActionType, {
|
||||
envName:
|
||||
env.sourceEnv !== "Global"
|
||||
? currentSelectedEnvironment.name
|
||||
: "Global",
|
||||
variableName: formattedExEnv,
|
||||
isSecret: env.secret,
|
||||
})
|
||||
if (env.sourceEnv === "RequestVariable") {
|
||||
currentTab.document.optionTabPreference =
|
||||
"requestVariables"
|
||||
} else {
|
||||
invokeAction(invokeActionType, {
|
||||
envName:
|
||||
env.sourceEnv === "Global"
|
||||
? "Global"
|
||||
: currentSelectedEnvironment.name,
|
||||
variableName: formattedExEnv,
|
||||
isSecret: env.secret,
|
||||
})
|
||||
}
|
||||
},
|
||||
},
|
||||
severity: 2,
|
||||
|
||||
Reference in New Issue
Block a user