fix: params, headers, url encoded syncing issues

This commit is contained in:
Andrew Bastin
2022-02-17 19:13:28 +05:30
parent 79914b32a1
commit 819b111383
7 changed files with 329 additions and 189 deletions

View File

@@ -40,7 +40,7 @@
<div v-else>
<div
v-for="(param, index) in workingParams"
:key="`param-${index}`"
:key="`param-${param.id}`"
class="flex border-b divide-x divide-dividerLight border-dividerLight"
>
<SmartEnvInput
@@ -48,6 +48,7 @@
:placeholder="`${t('count.parameter', { count: index + 1 })}`"
@change="
updateParam(index, {
id: param.id,
key: $event,
value: param.value,
active: param.active,
@@ -59,6 +60,7 @@
:placeholder="`${t('count.value', { count: index + 1 })}`"
@change="
updateParam(index, {
id: param.id,
key: param.key,
value: $event,
active: param.active,
@@ -85,6 +87,7 @@
color="green"
@click.native="
updateParam(index, {
id: param.id,
key: param.key,
value: param.value,
active: param.hasOwnProperty('active') ? !param.active : false,
@@ -129,7 +132,9 @@
<script setup lang="ts">
import { ref, watch } from "@nuxtjs/composition-api"
import { pipe } from "fp-ts/function"
import { flow, pipe } from "fp-ts/function"
import * as O from "fp-ts/Option"
import * as A from "fp-ts/Array"
import * as RA from "fp-ts/ReadonlyArray"
import * as E from "fp-ts/Either"
import {
@@ -139,16 +144,20 @@ import {
RawKeyValueEntry,
} from "@hoppscotch/data"
import isEqual from "lodash/isEqual"
import clone from "lodash/clone"
import cloneDeep from "lodash/cloneDeep"
import linter from "~/helpers/editor/linting/rawKeyValue"
import { useCodemirror } from "~/helpers/editor/codemirror"
import { useI18n, useToast, useStream } from "~/helpers/utils/composables"
import { restParams$, setRESTParams } from "~/newstore/RESTSession"
import { throwError } from "~/helpers/functional/error"
import { objRemoveKey } from "~/helpers/functional/object"
const t = useI18n()
const toast = useToast()
const idTicker = ref(0)
const bulkMode = ref(false)
const bulkParams = ref("")
@@ -170,8 +179,9 @@ useCodemirror(bulkEditor, bulkParams, {
const params = useStream(restParams$, [], setRESTParams)
// The UI representation of the parameters list (has the empty end param)
const workingParams = ref<HoppRESTParam[]>([
const workingParams = ref<Array<HoppRESTParam & { id: number }>>([
{
id: idTicker.value++,
key: "",
value: "",
active: true,
@@ -182,6 +192,7 @@ const workingParams = ref<HoppRESTParam[]>([
watch(workingParams, (paramsList) => {
if (paramsList.length > 0 && paramsList[paramsList.length - 1].key !== "") {
workingParams.value.push({
id: idTicker.value++,
key: "",
value: "",
active: true,
@@ -189,88 +200,96 @@ watch(workingParams, (paramsList) => {
}
})
// Sync logic between params and working params
// Sync logic between params and working/bulk params
watch(
params,
(newParamsList) => {
// Sync should overwrite working params
const filteredWorkingParams = workingParams.value.filter(
(e) => e.key !== ""
const filteredWorkingParams: HoppRESTParam[] = pipe(
workingParams.value,
A.filterMap(
flow(
O.fromPredicate((e) => e.key !== ""),
O.map(objRemoveKey("id"))
)
)
)
const filteredBulkParams = pipe(
parseRawKeyValueEntriesE(bulkParams.value),
E.map(
flow(
RA.filter((e) => e.key !== ""),
RA.toArray
)
),
E.getOrElse(() => [] as RawKeyValueEntry[])
)
if (!isEqual(newParamsList, filteredWorkingParams)) {
workingParams.value = newParamsList
workingParams.value = pipe(
newParamsList,
A.map((x) => ({ id: idTicker.value++, ...x }))
)
}
if (!isEqual(newParamsList, filteredBulkParams)) {
bulkParams.value = rawKeyValueEntriesToString(newParamsList)
}
},
{ immediate: true }
)
watch(workingParams, (newWorkingParams) => {
const fixedParams = newWorkingParams.filter((e) => e.key !== "")
if (!isEqual(params.value, fixedParams)) {
params.value = fixedParams
}
})
// Bulk Editor Syncing with Working Params
watch(bulkParams, () => {
try {
const transformation = pipe(
bulkParams.value,
parseRawKeyValueEntriesE,
E.map(RA.toArray),
E.getOrElse(() => [] as RawKeyValueEntry[])
const fixedParams = pipe(
newWorkingParams,
A.filterMap(
flow(
O.fromPredicate((e) => e.key !== ""),
O.map(objRemoveKey("id"))
)
)
)
const filteredParams = workingParams.value.filter((x) => x.key !== "")
if (!isEqual(filteredParams, transformation)) {
workingParams.value = transformation
}
} catch (e) {
toast.error(`${t("error.something_went_wrong")}`)
console.error(e)
if (!isEqual(params.value, fixedParams)) {
params.value = cloneDeep(fixedParams)
}
})
watch(workingParams, (newParamsList) => {
// If we are in bulk mode, don't apply direct changes
if (bulkMode.value) return
watch(bulkParams, (newBulkParams) => {
const filteredBulkParams = pipe(
parseRawKeyValueEntriesE(newBulkParams),
E.map(
flow(
RA.filter((e) => e.key !== ""),
RA.toArray
)
),
E.getOrElse(() => [] as RawKeyValueEntry[])
)
try {
const currentBulkParams = bulkParams.value.split("\n").map((item) => ({
key: item.substring(0, item.indexOf(":")).trimLeft().replace(/^#/, ""),
value: item.substring(item.indexOf(":") + 1).trimLeft(),
active: !item.trim().startsWith("#"),
}))
const filteredParams = newParamsList.filter((x) => x.key !== "")
if (!isEqual(currentBulkParams, filteredParams)) {
bulkParams.value = rawKeyValueEntriesToString(filteredParams)
}
} catch (e) {
toast.error(`${t("error.something_went_wrong")}`)
console.error(e)
if (!isEqual(params.value, filteredBulkParams)) {
params.value = filteredBulkParams
}
})
const addParam = () => {
workingParams.value.push({
id: idTicker.value++,
key: "",
value: "",
active: true,
})
}
const updateParam = (index: number, param: HoppRESTParam) => {
const updateParam = (index: number, param: HoppRESTParam & { id: number }) => {
workingParams.value = workingParams.value.map((h, i) =>
i === index ? param : h
)
}
const deleteParam = (index: number) => {
const paramsBeforeDeletion = clone(workingParams.value)
const paramsBeforeDeletion = cloneDeep(workingParams.value)
if (
!(
@@ -301,13 +320,18 @@ const deleteParam = (index: number) => {
})
}
workingParams.value.splice(index, 1)
workingParams.value = pipe(
workingParams.value,
A.deleteAt(index),
O.getOrElseW(() => throwError("Working Params Deletion Out of Bounds"))
)
}
const clearContent = () => {
// set params list to the initial state
workingParams.value = [
{
id: idTicker.value++,
key: "",
value: "",
active: true,