fix: resolve removing body parameters in requests (#2390) (#2428)

Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
This commit is contained in:
Vaugen Wake
2022-06-16 15:17:46 +01:00
committed by GitHub
parent 185dc3f2c9
commit 82b6ad935a

View File

@@ -38,8 +38,8 @@
drag-class="cursor-grabbing" drag-class="cursor-grabbing"
> >
<div <div
v-for="(param, index) in workingParams" v-for="({ id, entry }, index) in workingParams"
:key="`param-${index}`" :key="`param=${id}-${index}`"
class="flex border-b divide-x divide-dividerLight border-dividerLight draggable-content group" class="flex border-b divide-x divide-dividerLight border-dividerLight draggable-content group"
> >
<span> <span>
@@ -54,21 +54,21 @@
/> />
</span> </span>
<SmartEnvInput <SmartEnvInput
v-model="param.key" v-model="entry.key"
:placeholder="`${$t('count.parameter', { count: index + 1 })}`" :placeholder="`${$t('count.parameter', { count: index + 1 })}`"
@change=" @change="
updateBodyParam(index, { updateBodyParam(index, {
key: $event, key: $event,
value: param.value, value: entry.value,
active: param.active, active: entry.active,
isFile: param.isFile, isFile: entry.isFile,
}) })
" "
/> />
<div v-if="param.isFile" class="file-chips-container hide-scrollbar"> <div v-if="entry.isFile" class="file-chips-container hide-scrollbar">
<div class="space-x-2 file-chips-wrapper"> <div class="space-x-2 file-chips-wrapper">
<SmartFileChip <SmartFileChip
v-for="(file, fileIndex) in param.value" v-for="(file, fileIndex) in entry.value"
:key="`param-${index}-file-${fileIndex}`" :key="`param-${index}-file-${fileIndex}`"
>{{ file.name }}</SmartFileChip >{{ file.name }}</SmartFileChip
> >
@@ -76,14 +76,14 @@
</div> </div>
<span v-else class="flex flex-1"> <span v-else class="flex flex-1">
<SmartEnvInput <SmartEnvInput
v-model="param.value" v-model="entry.value"
:placeholder="`${$t('count.value', { count: index + 1 })}`" :placeholder="`${$t('count.value', { count: index + 1 })}`"
@change=" @change="
updateBodyParam(index, { updateBodyParam(index, {
key: param.key, key: entry.key,
value: $event, value: $event,
active: param.active, active: entry.active,
isFile: param.isFile, isFile: entry.isFile,
}) })
" "
/> />
@@ -97,7 +97,7 @@
type="file" type="file"
multiple multiple
class="p-1 cursor-pointer transition file:transition file:cursor-pointer text-secondaryLight hover:text-secondaryDark file:mr-2 file:py-1 file:px-4 file:rounded file:border-0 file:text-tiny text-tiny file:text-secondary hover:file:text-secondaryDark file:bg-primaryLight hover:file:bg-primaryDark" class="p-1 cursor-pointer transition file:transition file:cursor-pointer text-secondaryLight hover:text-secondaryDark file:mr-2 file:py-1 file:px-4 file:rounded file:border-0 file:text-tiny text-tiny file:text-secondary hover:file:text-secondaryDark file:bg-primaryLight hover:file:bg-primaryDark"
@change="setRequestAttachment(index, param, $event)" @change="setRequestAttachment(index, entry, $event)"
/> />
</label> </label>
</span> </span>
@@ -105,15 +105,15 @@
<ButtonSecondary <ButtonSecondary
v-tippy="{ theme: 'tooltip' }" v-tippy="{ theme: 'tooltip' }"
:title=" :title="
param.hasOwnProperty('active') entry.hasOwnProperty('active')
? param.active ? entry.active
? $t('action.turn_off') ? $t('action.turn_off')
: $t('action.turn_on') : $t('action.turn_on')
: $t('action.turn_off') : $t('action.turn_off')
" "
:svg=" :svg="
param.hasOwnProperty('active') entry.hasOwnProperty('active')
? param.active ? entry.active
? 'check-circle' ? 'check-circle'
: 'circle' : 'circle'
: 'check-circle' : 'check-circle'
@@ -121,10 +121,10 @@
color="green" color="green"
@click.native=" @click.native="
updateBodyParam(index, { updateBodyParam(index, {
key: param.key, key: entry.key,
value: param.value, value: entry.value,
active: param.hasOwnProperty('active') ? !param.active : false, active: entry.hasOwnProperty('active') ? !entry.active : false,
isFile: param.isFile, isFile: entry.isFile,
}) })
" "
/> />
@@ -164,6 +164,9 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, Ref, watch } from "@nuxtjs/composition-api" import { ref, Ref, watch } from "@nuxtjs/composition-api"
import { flow, pipe } from "fp-ts/function"
import * as O from "fp-ts/Option"
import * as A from "fp-ts/Array"
import { FormDataKeyValue } from "@hoppscotch/data" import { FormDataKeyValue } from "@hoppscotch/data"
import isEqual from "lodash/isEqual" import isEqual from "lodash/isEqual"
import { clone } from "lodash" import { clone } from "lodash"
@@ -171,10 +174,14 @@ import draggable from "vuedraggable"
import { pluckRef, useI18n, useToast } from "~/helpers/utils/composables" import { pluckRef, useI18n, useToast } from "~/helpers/utils/composables"
import { useRESTRequestBody } from "~/newstore/RESTSession" import { useRESTRequestBody } from "~/newstore/RESTSession"
type WorkingFormDataKeyValue = { id: number; entry: FormDataKeyValue }
const t = useI18n() const t = useI18n()
const toast = useToast() const toast = useToast()
const idTicker = ref(0)
const deletionToast = ref<{ goAway: (delay: number) => void } | null>(null) const deletionToast = ref<{ goAway: (delay: number) => void } | null>(null)
const bodyParams = pluckRef<any, any>(useRESTRequestBody(), "body") as Ref< const bodyParams = pluckRef<any, any>(useRESTRequestBody(), "body") as Ref<
@@ -182,23 +189,32 @@ const bodyParams = pluckRef<any, any>(useRESTRequestBody(), "body") as Ref<
> >
// The UI representation of the parameters list (has the empty end param) // The UI representation of the parameters list (has the empty end param)
const workingParams = ref<FormDataKeyValue[]>([ const workingParams = ref<WorkingFormDataKeyValue[]>([
{ {
id: idTicker.value++,
entry: {
key: "", key: "",
value: "", value: "",
active: true, active: true,
isFile: false, isFile: false,
}, },
},
]) ])
// Rule: Working Params always have last element is always an empty param // Rule: Working Params always have last element is always an empty param
watch(workingParams, (paramsList) => { watch(workingParams, (paramsList) => {
if (paramsList.length > 0 && paramsList[paramsList.length - 1].key !== "") { if (
paramsList.length > 0 &&
paramsList[paramsList.length - 1].entry.key !== ""
) {
workingParams.value.push({ workingParams.value.push({
id: idTicker.value++,
entry: {
key: "", key: "",
value: "", value: "",
active: true, active: true,
isFile: false, isFile: false,
},
}) })
} }
}) })
@@ -208,19 +224,37 @@ watch(
bodyParams, bodyParams,
(newParamsList) => { (newParamsList) => {
// Sync should overwrite working params // Sync should overwrite working params
const filteredWorkingParams = workingParams.value.filter( const filteredWorkingParams = pipe(
(e) => e.key !== "" workingParams.value,
A.filterMap(
flow(
O.fromPredicate((e) => e.entry.key !== ""),
O.map((e) => e.entry)
)
)
) )
if (!isEqual(newParamsList, filteredWorkingParams)) { if (!isEqual(newParamsList, filteredWorkingParams)) {
workingParams.value = newParamsList workingParams.value = pipe(
newParamsList,
A.map((x) => ({ id: idTicker.value++, entry: x }))
)
} }
}, },
{ immediate: true } { immediate: true }
) )
watch(workingParams, (newWorkingParams) => { watch(workingParams, (newWorkingParams) => {
const fixedParams = newWorkingParams.filter((e) => e.key !== "") const fixedParams = pipe(
newWorkingParams,
A.filterMap(
flow(
O.fromPredicate((e) => e.entry.key !== ""),
O.map((e) => e.entry)
)
)
)
if (!isEqual(bodyParams.value, fixedParams)) { if (!isEqual(bodyParams.value, fixedParams)) {
bodyParams.value = fixedParams bodyParams.value = fixedParams
} }
@@ -228,16 +262,19 @@ watch(workingParams, (newWorkingParams) => {
const addBodyParam = () => { const addBodyParam = () => {
workingParams.value.push({ workingParams.value.push({
id: idTicker.value++,
entry: {
key: "", key: "",
value: "", value: "",
active: true, active: true,
isFile: false, isFile: false,
},
}) })
} }
const updateBodyParam = (index: number, param: FormDataKeyValue) => { const updateBodyParam = (index: number, entry: FormDataKeyValue) => {
workingParams.value = workingParams.value.map((h, i) => workingParams.value = workingParams.value.map((h, i) =>
i === index ? param : h i === index ? { id: h.id, entry } : h
) )
} }
@@ -280,11 +317,14 @@ const clearContent = () => {
// set params list to the initial state // set params list to the initial state
workingParams.value = [ workingParams.value = [
{ {
id: idTicker.value++,
entry: {
key: "", key: "",
value: "", value: "",
active: true, active: true,
isFile: false, isFile: false,
}, },
},
] ]
} }