feat: request variables (#3825)

Co-authored-by: jamesgeorge007 <jamesgeorge998001@gmail.com>
This commit is contained in:
Nivedin
2024-03-07 12:50:44 +05:30
committed by GitHub
parent 3611cac241
commit 7ec8659381
54 changed files with 1273 additions and 506 deletions

View File

@@ -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,

View File

@@ -25,7 +25,7 @@ export const REST_COLLECTIONS_MOCK: HoppCollection[] = [
folders: [],
requests: [
{
v: "1",
v: "2",
endpoint: "https://echo.hoppscotch.io",
name: "Echo test",
params: [],
@@ -35,6 +35,7 @@ export const REST_COLLECTIONS_MOCK: HoppCollection[] = [
preRequestScript: "",
testScript: "",
body: { contentType: null, body: null },
requestVariables: [],
},
],
auth: { authType: "none", authActive: true },
@@ -136,7 +137,8 @@ export const REST_HISTORY_MOCK: RESTHistoryEntry[] = [
params: [],
preRequestScript: "",
testScript: "",
v: "1",
requestVariables: [],
v: "2",
},
responseMeta: { duration: 807, statusCode: 200 },
star: false,
@@ -192,7 +194,7 @@ export const REST_TAB_STATE_MOCK: PersistableTabState<HoppRESTDocument> = {
tabID: "e6e8d800-caa8-44a2-a6a6-b4765a3167aa",
doc: {
request: {
v: "1",
v: "2",
endpoint: "https://echo.hoppscotch.io",
name: "Echo test",
params: [],
@@ -202,6 +204,7 @@ export const REST_TAB_STATE_MOCK: PersistableTabState<HoppRESTDocument> = {
preRequestScript: "",
testScript: "",
body: { contentType: null, body: null },
requestVariables: [],
},
isDirty: false,
saveContext: {

View File

@@ -48,21 +48,22 @@ const SettingsDefSchema = z.object({
WRAP_LINES: z.optional(
z.object({
httpRequestBody: z.boolean(),
httpResponseBody: z.boolean(),
httpHeaders: z.boolean(),
httpParams: z.boolean(),
httpUrlEncoded: z.boolean(),
httpPreRequest: z.boolean(),
httpTest: z.boolean(),
graphqlQuery: z.boolean(),
graphqlResponseBody: z.boolean(),
graphqlHeaders: z.boolean(),
graphqlVariables: z.boolean(),
graphqlSchema: z.boolean(),
importCurl: z.boolean(),
codeGen: z.boolean(),
cookie: z.boolean(),
httpRequestBody: z.boolean().catch(true),
httpResponseBody: z.boolean().catch(true),
httpHeaders: z.boolean().catch(true),
httpParams: z.boolean().catch(true),
httpUrlEncoded: z.boolean().catch(true),
httpPreRequest: z.boolean().catch(true),
httpTest: z.boolean().catch(true),
httpRequestVariables: z.boolean().catch(true),
graphqlQuery: z.boolean().catch(true),
graphqlResponseBody: z.boolean().catch(true),
graphqlHeaders: z.boolean().catch(false),
graphqlVariables: z.boolean().catch(false),
graphqlSchema: z.boolean().catch(true),
importCurl: z.boolean().catch(true),
codeGen: z.boolean().catch(true),
cookie: z.boolean().catch(true),
})
),
})
@@ -514,6 +515,7 @@ const validRestOperations = [
"authorization",
"preRequestScript",
"tests",
"requestVariables",
] as const
export const REST_TAB_STATE_SCHEMA = z