chore: code refactors

This commit is contained in:
nivedin
2024-03-06 21:24:27 +05:30
committed by Andrew Bastin
parent 1e906bbec3
commit e9f1dc7ba1
8 changed files with 110 additions and 118 deletions

View File

@@ -123,9 +123,9 @@ const sharedRequestURL = computed(() => {
}) })
const tabRequestVariables = computed(() => { const tabRequestVariables = computed(() => {
return tab.value.document.request.requestVariables.map((v) => ({ return tab.value.document.request.requestVariables.map(({ key, value }) => ({
key: v.key, key,
value: v.value, value,
secret: false, secret: false,
sourceEnv: "RequestVariable", sourceEnv: "RequestVariable",
})) }))

View File

@@ -196,7 +196,7 @@ const requestCode = computed(() => {
value: requestVariable.value, value: requestVariable.value,
secret: false, secret: false,
} }
return [] return {}
} }
) )
const env: Environment = { const env: Environment = {

View File

@@ -18,7 +18,7 @@
:auto-complete-env="true" :auto-complete-env="true"
:styles="hasOIDCURL ? 'pointer-events-none opacity-70' : ''" :styles="hasOIDCURL ? 'pointer-events-none opacity-70' : ''"
:envs="envs" :envs="envs"
></SmartEnvInput> />
</div> </div>
<div class="flex flex-1 border-b border-dividerLight"> <div class="flex flex-1 border-b border-dividerLight">
<SmartEnvInput <SmartEnvInput

View File

@@ -5,15 +5,15 @@
render-inactive-tabs render-inactive-tabs
> >
<HoppSmartTab <HoppSmartTab
v-if="properties ? properties.includes('params') : true" v-if="properties?.includes('params') ?? true"
:id="'params'" :id="'params'"
:label="`${t('tab.parameters')}`" :label="`${t('tab.parameters')}`"
:info="`${newActiveParamsCount$}`" :info="`${newActiveParamsCount}`"
> >
<HttpParameters v-model="request.params" :envs="envs" /> <HttpParameters v-model="request.params" :envs="envs" />
</HoppSmartTab> </HoppSmartTab>
<HoppSmartTab <HoppSmartTab
v-if="properties ? properties.includes('bodyParams') : true" v-if="properties?.includes('bodyParams') ?? true"
:id="'bodyParams'" :id="'bodyParams'"
:label="`${t('tab.body')}`" :label="`${t('tab.body')}`"
> >
@@ -25,10 +25,10 @@
/> />
</HoppSmartTab> </HoppSmartTab>
<HoppSmartTab <HoppSmartTab
v-if="properties ? properties.includes('headers') : true" v-if="properties?.includes('headers') ?? true"
:id="'headers'" :id="'headers'"
:label="`${t('tab.headers')}`" :label="`${t('tab.headers')}`"
:info="`${newActiveHeadersCount$}`" :info="`${newActiveHeadersCount}`"
> >
<HttpHeaders <HttpHeaders
v-model="request" v-model="request"
@@ -38,7 +38,7 @@
/> />
</HoppSmartTab> </HoppSmartTab>
<HoppSmartTab <HoppSmartTab
v-if="properties ? properties.includes('authorization') : true" v-if="properties?.includes('authorization') ?? true"
:id="'authorization'" :id="'authorization'"
:label="`${t('tab.authorization')}`" :label="`${t('tab.authorization')}`"
> >
@@ -49,7 +49,7 @@
/> />
</HoppSmartTab> </HoppSmartTab>
<HoppSmartTab <HoppSmartTab
v-if="properties ? properties.includes('preRequestScript') : true" v-if="properties?.includes('preRequestScript') ?? true"
:id="'preRequestScript'" :id="'preRequestScript'"
:label="`${t('tab.pre_request_script')}`" :label="`${t('tab.pre_request_script')}`"
:indicator=" :indicator="
@@ -61,7 +61,7 @@
<HttpPreRequestScript v-model="request.preRequestScript" /> <HttpPreRequestScript v-model="request.preRequestScript" />
</HoppSmartTab> </HoppSmartTab>
<HoppSmartTab <HoppSmartTab
v-if="properties ? properties.includes('tests') : true" v-if="properties?.includes('tests') ?? true"
:id="'tests'" :id="'tests'"
:label="`${t('tab.tests')}`" :label="`${t('tab.tests')}`"
:indicator=" :indicator="
@@ -71,10 +71,10 @@
<HttpTests v-model="request.testScript" /> <HttpTests v-model="request.testScript" />
</HoppSmartTab> </HoppSmartTab>
<HoppSmartTab <HoppSmartTab
v-if="properties ? properties.includes('requestVariables') : true" v-if="properties?.includes('requestVariables') ?? true"
:id="'requestVariables'" :id="'requestVariables'"
:label="`${t('tab.variables')}`" :label="`${t('tab.variables')}`"
:info="`${newActiveRequestVariablesCount$}`" :info="`${newActiveRequestVariablesCount}`"
:align-last="true" :align-last="true"
> >
<HttpRequestVariables v-model="request.requestVariables" /> <HttpRequestVariables v-model="request.requestVariables" />
@@ -131,31 +131,27 @@ const changeOptionTab = (e: RESTOptionTabs) => {
selectedOptionTab.value = e selectedOptionTab.value = e
} }
const newActiveParamsCount$ = computed(() => { const newActiveParamsCount = computed(() => {
const e = request.value.params.filter( const count = request.value.params.filter(
(x) => x.active && (x.key !== "" || x.value !== "") (x) => x.active && (x.key || x.value)
).length ).length
if (e === 0) return null return count ? count : null
return `${e}`
}) })
const newActiveHeadersCount$ = computed(() => { const newActiveHeadersCount = computed(() => {
const e = request.value.headers.filter( const count = request.value.headers.filter(
(x) => x.active && (x.key !== "" || x.value !== "") (x) => x.active && (x.key || x.value)
).length ).length
if (e === 0) return null return count ? count : null
return `${e}`
}) })
const newActiveRequestVariablesCount$ = computed(() => { const newActiveRequestVariablesCount = computed(() => {
const e = request.value.requestVariables.filter( const count = request.value.requestVariables.filter(
(x) => x.active && (x.key !== "" || x.value !== "") (x) => x.active && (x.key || x.value)
).length ).length
return count ? count : null
if (e === 0) return null
return `${e}`
}) })
defineActionHandler("request.open-tab", ({ tab }) => { defineActionHandler("request.open-tab", ({ tab }) => {

View File

@@ -49,7 +49,7 @@
</div> </div>
<div v-else> <div v-else>
<draggable <draggable
v-model="workingRequestVaraiables" v-model="workingRequestVariables"
item-key="id" item-key="id"
animation="250" animation="250"
handle=".draggable-handle" handle=".draggable-handle"
@@ -62,13 +62,12 @@
<div <div
class="draggable-content group flex divide-x divide-dividerLight border-b border-dividerLight" class="draggable-content group flex divide-x divide-dividerLight border-b border-dividerLight"
> >
<span>
<HoppButtonSecondary <HoppButtonSecondary
v-tippy="{ v-tippy="{
theme: 'tooltip', theme: 'tooltip',
delay: [500, 20], delay: [500, 20],
content: content:
index !== workingRequestVaraiables?.length - 1 index !== workingRequestVariables?.length - 1
? t('action.drag_to_reorder') ? t('action.drag_to_reorder')
: null, : null,
}" }"
@@ -76,11 +75,10 @@
class="opacity-0" class="opacity-0"
:class="{ :class="{
'draggable-handle cursor-grab group-hover:opacity-100': 'draggable-handle cursor-grab group-hover:opacity-100':
index !== workingRequestVaraiables?.length - 1, index !== workingRequestVariables?.length - 1,
}" }"
tabindex="-1" tabindex="-1"
/> />
</span>
<SmartEnvInput <SmartEnvInput
v-model="variable.key" v-model="variable.key"
:placeholder="`${t('count.variable', { count: index + 1 })}`" :placeholder="`${t('count.variable', { count: index + 1 })}`"
@@ -105,7 +103,6 @@
}) })
" "
/> />
<span>
<HoppButtonSecondary <HoppButtonSecondary
v-tippy="{ theme: 'tooltip' }" v-tippy="{ theme: 'tooltip' }"
:title=" :title="
@@ -134,8 +131,6 @@
}) })
" "
/> />
</span>
<span>
<HoppButtonSecondary <HoppButtonSecondary
v-tippy="{ theme: 'tooltip' }" v-tippy="{ theme: 'tooltip' }"
:title="t('action.remove')" :title="t('action.remove')"
@@ -143,12 +138,11 @@
color="red" color="red"
@click="deleteVariable(index)" @click="deleteVariable(index)"
/> />
</span>
</div> </div>
</template> </template>
</draggable> </draggable>
<HoppSmartPlaceholder <HoppSmartPlaceholder
v-if="workingRequestVaraiables.length === 0" v-if="workingRequestVariables.length === 0"
:src="`/images/states/${colorMode.value}/add_files.svg`" :src="`/images/states/${colorMode.value}/add_files.svg`"
:alt="`${t('empty.request_variables')}`" :alt="`${t('empty.request_variables')}`"
:text="t('empty.request_variables')" :text="t('empty.request_variables')"
@@ -238,7 +232,7 @@ useCodemirror(
const idTicker = ref(0) const idTicker = ref(0)
const workingRequestVaraiables = ref< const workingRequestVariables = ref<
Array<HoppRESTRequestVariable & { id: number }> Array<HoppRESTRequestVariable & { id: number }>
>([ >([
{ {
@@ -255,7 +249,7 @@ watch(
(newRequestVariableList) => { (newRequestVariableList) => {
// Sync should overwrite working params // Sync should overwrite working params
const filteredWorkingRequestVariables: HoppRESTRequestVariable[] = pipe( const filteredWorkingRequestVariables: HoppRESTRequestVariable[] = pipe(
workingRequestVaraiables.value, workingRequestVariables.value,
A.filterMap( A.filterMap(
flow( flow(
O.fromPredicate((e) => e.key !== ""), O.fromPredicate((e) => e.key !== ""),
@@ -276,7 +270,7 @@ watch(
) )
if (!isEqual(newRequestVariableList, filteredWorkingRequestVariables)) { if (!isEqual(newRequestVariableList, filteredWorkingRequestVariables)) {
workingRequestVaraiables.value = pipe( workingRequestVariables.value = pipe(
newRequestVariableList, newRequestVariableList,
A.map((x) => ({ id: idTicker.value++, ...x })) A.map((x) => ({ id: idTicker.value++, ...x }))
) )
@@ -289,7 +283,7 @@ watch(
{ immediate: true } { immediate: true }
) )
watch(workingRequestVaraiables, (newWorkingRequestVariables) => { watch(workingRequestVariables, (newWorkingRequestVariables) => {
const fixedRequestVariables = pipe( const fixedRequestVariables = pipe(
newWorkingRequestVariables, newWorkingRequestVariables,
A.filterMap( A.filterMap(
@@ -323,12 +317,12 @@ watch(bulkVariables, (newBulkParams) => {
}) })
// Rule: Working Request variable always have last element is always an empty param // Rule: Working Request variable always have last element is always an empty param
watch(workingRequestVaraiables, (variableList) => { watch(workingRequestVariables, (variableList) => {
if ( if (
variableList.length > 0 && variableList.length > 0 &&
variableList[variableList.length - 1].key !== "" variableList[variableList.length - 1].key !== ""
) { ) {
workingRequestVaraiables.value.push({ workingRequestVariables.value.push({
id: idTicker.value++, id: idTicker.value++,
key: "", key: "",
value: "", value: "",
@@ -338,7 +332,7 @@ watch(workingRequestVaraiables, (variableList) => {
}) })
const addVariable = () => { const addVariable = () => {
workingRequestVaraiables.value.push({ workingRequestVariables.value.push({
id: idTicker.value++, id: idTicker.value++,
key: "", key: "",
value: "", value: "",
@@ -347,14 +341,14 @@ const addVariable = () => {
} }
const updateVariable = (index: number, variable: any & { id: number }) => { const updateVariable = (index: number, variable: any & { id: number }) => {
workingRequestVaraiables.value = workingRequestVaraiables.value.map((h, i) => workingRequestVariables.value = workingRequestVariables.value.map((h, i) =>
i === index ? variable : h i === index ? variable : h
) )
} }
const deleteVariable = (index: number) => { const deleteVariable = (index: number) => {
const requestVariablesBeforeDeletion = cloneDeep( const requestVariablesBeforeDeletion = cloneDeep(
workingRequestVaraiables.value workingRequestVariables.value
) )
if ( if (
@@ -373,7 +367,7 @@ const deleteVariable = (index: number) => {
{ {
text: `${t("action.undo")}`, text: `${t("action.undo")}`,
onClick: (_, toastObject) => { onClick: (_, toastObject) => {
workingRequestVaraiables.value = requestVariablesBeforeDeletion workingRequestVariables.value = requestVariablesBeforeDeletion
toastObject.goAway(0) toastObject.goAway(0)
deletionToast.value = null deletionToast.value = null
}, },
@@ -386,8 +380,8 @@ const deleteVariable = (index: number) => {
}) })
} }
workingRequestVaraiables.value = pipe( workingRequestVariables.value = pipe(
workingRequestVaraiables.value, workingRequestVariables.value,
A.deleteAt(index), A.deleteAt(index),
O.getOrElseW(() => O.getOrElseW(() =>
throwError("Working Request Variable Deletion Out of Bounds") throwError("Working Request Variable Deletion Out of Bounds")
@@ -397,7 +391,7 @@ const deleteVariable = (index: number) => {
const clearContent = () => { const clearContent = () => {
// set params list to the initial state // set params list to the initial state
workingRequestVaraiables.value = [ workingRequestVariables.value = [
{ {
id: idTicker.value++, id: idTicker.value++,
key: "", key: "",

View File

@@ -243,7 +243,7 @@ export function useCodemirror(
if (from === to) return if (from === to) return
const text = view.value?.state.doc.sliceString(from, to) const text = view.value?.state.doc.sliceString(from, to)
const { top, left } = view.value?.coordsAtPos(from) const { top, left } = view.value?.coordsAtPos(from)
if (text && text.trim()) { if (text?.trim()) {
invokeAction("contextmenu.open", { invokeAction("contextmenu.open", {
position: { position: {
top, top,

View File

@@ -138,7 +138,7 @@ const cursorTooltipField = (aggregateEnvs: AggregateEnvironment[]) =>
"requestVariables" "requestVariables"
} else { } else {
invokeAction(invokeActionType, { invokeAction(invokeActionType, {
envName: tooltipEnv?.sourceEnv !== "Global" ? envName : "Global", envName: tooltipEnv?.sourceEnv === "Global" ? "Global" : envName,
variableName: parsedEnvKey, variableName: parsedEnvKey,
isSecret: tooltipEnv?.secret, isSecret: tooltipEnv?.secret,
}) })
@@ -236,9 +236,9 @@ export class HoppEnvironmentPlugin {
currentTab.document.request, currentTab.document.request,
(reqVariables) => { (reqVariables) => {
this.envs = [ this.envs = [
...reqVariables.requestVariables.map((variable) => ({ ...reqVariables.requestVariables.map(({ key, value }) => ({
key: variable.key, key,
value: variable.value, value,
sourceEnv: "RequestVariable", sourceEnv: "RequestVariable",
secret: false, secret: false,
})), })),
@@ -257,12 +257,14 @@ export class HoppEnvironmentPlugin {
subscribeToStream(aggregateEnvsWithSecrets$, (envs) => { subscribeToStream(aggregateEnvsWithSecrets$, (envs) => {
this.envs = [ this.envs = [
...currentTab.document.request.requestVariables.map((variable) => ({ ...currentTab.document.request.requestVariables.map(
key: variable.key, ({ key, value }) => ({
value: variable.value, key,
value,
sourceEnv: "RequestVariable", sourceEnv: "RequestVariable",
secret: false, secret: false,
})), })
),
...envs, ...envs,
] ]

View File

@@ -225,9 +225,9 @@ export class EnvironmentInspectorService extends Service implements Inspector {
} else { } else {
invokeAction(invokeActionType, { invokeAction(invokeActionType, {
envName: envName:
env.sourceEnv !== "Global" env.sourceEnv === "Global"
? currentSelectedEnvironment.name ? "Global"
: "Global", : currentSelectedEnvironment.name,
variableName: formattedExEnv, variableName: formattedExEnv,
isSecret: env.secret, isSecret: env.secret,
}) })