From 4ecd69cc5adecbf096685fdf8c62249ecea99c0d Mon Sep 17 00:00:00 2001 From: liyasthomas Date: Fri, 31 Dec 2021 23:25:46 +0530 Subject: [PATCH] fix: race condition mutating defaultRESTRequest --- .../hoppscotch-app/components/http/Headers.vue | 14 ++++++-------- .../hoppscotch-app/components/http/Parameters.vue | 14 ++++++-------- .../hoppscotch-app/helpers/RESTExtURLParams.ts | 7 +++---- packages/hoppscotch-app/newstore/RESTSession.ts | 8 ++++---- 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/packages/hoppscotch-app/components/http/Headers.vue b/packages/hoppscotch-app/components/http/Headers.vue index 2aa86b31e..facdd5a08 100644 --- a/packages/hoppscotch-app/components/http/Headers.vue +++ b/packages/hoppscotch-app/components/http/Headers.vue @@ -233,12 +233,9 @@ watch(bulkHeaders, () => { .split("\n") .filter((x) => x.trim().length > 0 && x.includes(":")) .map((item) => ({ - key: item - .substring(0, item.indexOf(":")) - .trimLeft() - .replace(/^\/\//, ""), + key: item.substring(0, item.indexOf(":")).trimLeft().replace(/^#/, ""), value: item.substring(item.indexOf(":") + 1).trimLeft(), - active: !item.trim().startsWith("//"), + active: !item.trim().startsWith("#"), })) const filteredHeaders = workingHeaders.value.filter((x) => x.key !== "") @@ -251,15 +248,16 @@ watch(bulkHeaders, () => { console.error(e) } }) + watch(workingHeaders, (newHeadersList) => { // If we are in bulk mode, don't apply direct changes if (bulkMode.value) return try { const currentBulkHeaders = bulkHeaders.value.split("\n").map((item) => ({ - key: item.substring(0, item.indexOf(":")).trimLeft().replace(/^\/\//, ""), + key: item.substring(0, item.indexOf(":")).trimLeft().replace(/^#/, ""), value: item.substring(item.indexOf(":") + 1).trimLeft(), - active: !item.trim().startsWith("//"), + active: !item.trim().startsWith("#"), })) const filteredHeaders = newHeadersList.filter((x) => x.key !== "") @@ -267,7 +265,7 @@ watch(workingHeaders, (newHeadersList) => { if (!isEqual(currentBulkHeaders, filteredHeaders)) { bulkHeaders.value = filteredHeaders .map((header) => { - return `${header.active ? "" : "//"}${header.key}: ${header.value}` + return `${header.active ? "" : "#"}${header.key}: ${header.value}` }) .join("\n") } diff --git a/packages/hoppscotch-app/components/http/Parameters.vue b/packages/hoppscotch-app/components/http/Parameters.vue index 430d8faf5..48f03ab5a 100644 --- a/packages/hoppscotch-app/components/http/Parameters.vue +++ b/packages/hoppscotch-app/components/http/Parameters.vue @@ -224,12 +224,9 @@ watch(bulkParams, () => { .split("\n") .filter((x) => x.trim().length > 0 && x.includes(":")) .map((item) => ({ - key: item - .substring(0, item.indexOf(":")) - .trimLeft() - .replace(/^\/\//, ""), + key: item.substring(0, item.indexOf(":")).trimLeft().replace(/^#/, ""), value: item.substring(item.indexOf(":") + 1).trimLeft(), - active: !item.trim().startsWith("//"), + active: !item.trim().startsWith("#"), })) const filteredParams = workingParams.value.filter((x) => x.key !== "") @@ -242,15 +239,16 @@ watch(bulkParams, () => { console.error(e) } }) + watch(workingParams, (newParamsList) => { // If we are in bulk mode, don't apply direct changes if (bulkMode.value) return try { const currentBulkParams = bulkParams.value.split("\n").map((item) => ({ - key: item.substring(0, item.indexOf(":")).trimLeft().replace(/^\/\//, ""), + key: item.substring(0, item.indexOf(":")).trimLeft().replace(/^#/, ""), value: item.substring(item.indexOf(":") + 1).trimLeft(), - active: !item.trim().startsWith("//"), + active: !item.trim().startsWith("#"), })) const filteredParams = newParamsList.filter((x) => x.key !== "") @@ -258,7 +256,7 @@ watch(workingParams, (newParamsList) => { if (!isEqual(currentBulkParams, filteredParams)) { bulkParams.value = filteredParams .map((param) => { - return `${param.active ? "" : "//"}${param.key}: ${param.value}` + return `${param.active ? "" : "#"}${param.key}: ${param.value}` }) .join("\n") } diff --git a/packages/hoppscotch-app/helpers/RESTExtURLParams.ts b/packages/hoppscotch-app/helpers/RESTExtURLParams.ts index a19cd1a66..1b9bf5c2b 100644 --- a/packages/hoppscotch-app/helpers/RESTExtURLParams.ts +++ b/packages/hoppscotch-app/helpers/RESTExtURLParams.ts @@ -1,7 +1,6 @@ -import clone from "lodash/clone" import { FormDataKeyValue, HoppRESTRequest } from "@hoppscotch/data" import { isJSONContentType } from "./utils/contenttypes" -import { defaultRESTRequest } from "~/newstore/RESTSession" +import { getDefaultRESTRequest } from "~/newstore/RESTSession" /** * Handles translations for all the hopp.io REST Shareable URL params @@ -14,7 +13,7 @@ export function translateExtURLParams( } function parseV0ExtURL(urlParams: Record): HoppRESTRequest { - const resolvedReq = clone(defaultRESTRequest) + const resolvedReq = getDefaultRESTRequest() if (urlParams.method && typeof urlParams.method === "string") { resolvedReq.method = urlParams.method @@ -91,7 +90,7 @@ function parseV0ExtURL(urlParams: Record): HoppRESTRequest { } function parseV1ExtURL(urlParams: Record): HoppRESTRequest { - const resolvedReq = clone(defaultRESTRequest) + const resolvedReq = getDefaultRESTRequest() if (urlParams.headers && typeof urlParams.headers === "string") { resolvedReq.headers = JSON.parse(urlParams.headers) diff --git a/packages/hoppscotch-app/newstore/RESTSession.ts b/packages/hoppscotch-app/newstore/RESTSession.ts index 5ffd5ecc2..324f1aaf9 100644 --- a/packages/hoppscotch-app/newstore/RESTSession.ts +++ b/packages/hoppscotch-app/newstore/RESTSession.ts @@ -23,7 +23,7 @@ type RESTSession = { saveContext: HoppRequestSaveContext | null } -export const defaultRESTRequest: HoppRESTRequest = { +export const getDefaultRESTRequest = (): HoppRESTRequest => ({ v: RESTReqSchemaVersion, endpoint: "https://echo.hoppscotch.io", name: "Untitled request", @@ -40,10 +40,10 @@ export const defaultRESTRequest: HoppRESTRequest = { contentType: null, body: null, }, -} +}) const defaultRESTSession: RESTSession = { - request: defaultRESTRequest, + request: getDefaultRESTRequest(), response: null, testResults: null, saveContext: null, @@ -387,7 +387,7 @@ export function getRESTSaveContext() { } export function resetRESTRequest() { - setRESTRequest(defaultRESTRequest) + setRESTRequest(getDefaultRESTRequest()) } export function setRESTEndpoint(newEndpoint: string) {