fix: code refactor and handle secret var in req var

This commit is contained in:
nivedin
2024-03-06 20:44:10 +05:30
committed by Andrew Bastin
parent 7776668e40
commit 1e906bbec3
2 changed files with 45 additions and 48 deletions

View File

@@ -79,10 +79,7 @@ import { history, historyKeymap } from "@codemirror/commands"
import { inputTheme } from "~/helpers/editor/themes/baseTheme" import { inputTheme } from "~/helpers/editor/themes/baseTheme"
import { HoppReactiveEnvPlugin } from "~/helpers/editor/extensions/HoppEnvironment" import { HoppReactiveEnvPlugin } from "~/helpers/editor/extensions/HoppEnvironment"
import { useReadonlyStream } from "@composables/stream" import { useReadonlyStream } from "@composables/stream"
import { import { AggregateEnvironment, aggregateEnvs$ } from "~/newstore/environments"
AggregateEnvironment,
aggregateEnvsWithSecrets$,
} from "~/newstore/environments"
import { platform } from "~/platform" import { platform } from "~/platform"
import { onClickOutside, useDebounceFn } from "@vueuse/core" import { onClickOutside, useDebounceFn } from "@vueuse/core"
import { InspectorResult } from "~/services/inspection" import { InspectorResult } from "~/services/inspection"
@@ -356,56 +353,51 @@ watch(
let clipboardEv: ClipboardEvent | null = null let clipboardEv: ClipboardEvent | null = null
let pastedValue: string | null = null let pastedValue: string | null = null
const aggregateEnvs = useReadonlyStream(aggregateEnvsWithSecrets$, []) as Ref< const aggregateEnvs = useReadonlyStream(aggregateEnvs$, []) as Ref<
AggregateEnvironment[] AggregateEnvironment[]
> >
const tabs = useService(RESTTabService) const tabs = useService(RESTTabService)
const envVars = computed(() => { const envVars = computed(() => {
return props.envs if (props.envs) {
? props.envs.map((x) => { return props.envs.map((x) => {
if (x.secret) { const { key, secret } = x
const value = secret ? "********" : x.value
const sourceEnv = "sourceEnv" in x ? x.sourceEnv : null
return { return {
key: x.key, key,
sourceEnv: "sourceEnv" in x ? x.sourceEnv : null, value,
value: "********", sourceEnv,
secret: true, secret,
}
}
return {
key: x.key,
value: x.value,
sourceEnv: "sourceEnv" in x ? x.sourceEnv : null,
secret: false,
} }
}) })
: [ }
return [
...tabs.currentActiveTab.value.document.request.requestVariables.map( ...tabs.currentActiveTab.value.document.request.requestVariables.map(
(x) => ({ active, key, value }) =>
x.active active
? { ? {
key: x.key, key,
value: x.value, value,
sourceEnv: "RequestVariable", sourceEnv: "RequestVariable",
secret: false, secret: false,
} }
: ([] as unknown as AggregateEnvironment) : ({} as AggregateEnvironment)
), ),
...aggregateEnvs.value, ...aggregateEnvs.value,
] ]
}) })
function envAutoCompletion(context: CompletionContext) { function envAutoCompletion(context: CompletionContext) {
const options = envVars.value const options = (envVars.value ?? [])
? envVars.value.map((env) => { .map((env) => ({
return { label: env?.key ? `<<${env.key}>>` : "",
label: `<<${env.key}>>`, info: env?.value ?? "",
info: env.value, apply: env?.key ? `<<${env.key}>>` : "",
apply: `<<${env.key}>>`, }))
} .filter((x) => x)
})
: []
const nodeBefore = syntaxTree(context.state).resolveInner(context.pos, -1) const nodeBefore = syntaxTree(context.state).resolveInner(context.pos, -1)
const textBefore = context.state.sliceDoc(nodeBefore.from, context.pos) const textBefore = context.state.sliceDoc(nodeBefore.from, context.pos)
const tagBefore = /<<\w*$/.exec(textBefore) const tagBefore = /<<\w*$/.exec(textBefore)

View File

@@ -13,7 +13,7 @@ import { StreamSubscriberFunc } from "@composables/stream"
import { import {
AggregateEnvironment, AggregateEnvironment,
aggregateEnvsWithSecrets$, aggregateEnvsWithSecrets$,
getAggregateEnvsWithSecrets, getAggregateEnvs,
getCurrentEnvironment, getCurrentEnvironment,
getSelectedEnvironmentType, getSelectedEnvironmentType,
} from "~/newstore/environments" } from "~/newstore/environments"
@@ -100,7 +100,12 @@ const cursorTooltipField = (aggregateEnvs: AggregateEnvironment[]) =>
const result = parseTemplateStringE(envValue, aggregateEnvs) const result = parseTemplateStringE(envValue, aggregateEnvs)
const finalEnv = E.isLeft(result) ? "error" : result.right let finalEnv = E.isLeft(result) ? "error" : result.right
// If the request variable has an secret variable
// parseTemplateStringE is passed the secret value which has value undefined
// So, we need to check if the result is undefined and then set the finalEnv to ******
if (finalEnv === "undefined") finalEnv = "******"
const selectedEnvType = getSelectedEnvironmentType() const selectedEnvType = getSelectedEnvironmentType()
@@ -224,7 +229,7 @@ export class HoppEnvironmentPlugin {
subscribeToStream: StreamSubscriberFunc, subscribeToStream: StreamSubscriberFunc,
private editorView: Ref<EditorView | undefined> private editorView: Ref<EditorView | undefined>
) { ) {
const aggregateEnvs = getAggregateEnvsWithSecrets() const aggregateEnvs = getAggregateEnvs()
const currentTab = restTabs.currentActiveTab.value const currentTab = restTabs.currentActiveTab.value
watch( watch(