From cabc775f58783b63f1c3f98d6ee1dc9c43ffb31b Mon Sep 17 00:00:00 2001 From: isaiM6 <98564922+isaiM6@users.noreply.github.com> Date: Mon, 1 Aug 2022 14:08:29 -0700 Subject: [PATCH 1/2] commit for merge --- .../components/http/Request.vue | 3 +- .../components/http/Variables.vue | 18 ++--- .../components/smart/EnvInput.vue | 2 + .../helpers/utils/EffectiveURL.ts | 6 ++ packages/hoppscotch-data/src/environment.ts | 7 ++ packages/hoppscotch-data/src/variables.ts | 78 +++++++++++++++++++ 6 files changed, 104 insertions(+), 10 deletions(-) create mode 100644 packages/hoppscotch-data/src/variables.ts diff --git a/packages/hoppscotch-app/components/http/Request.vue b/packages/hoppscotch-app/components/http/Request.vue index d2d1b1385..f9600f608 100644 --- a/packages/hoppscotch-app/components/http/Request.vue +++ b/packages/hoppscotch-app/components/http/Request.vue @@ -348,7 +348,8 @@ const newSendRequest = async () => { const ensureMethodInEndpoint = () => { if ( !/^http[s]?:\/\//.test(newEndpoint.value) && - !newEndpoint.value.startsWith("<<") + !newEndpoint.value.startsWith("<<") && + !newEndpoint.value.startsWith("{{") ) { const domain = newEndpoint.value.split(/[/:#?]+/)[0] if (domain === "localhost" || /([0-9]+\.)*[0-9]/.test(domain)) { diff --git a/packages/hoppscotch-app/components/http/Variables.vue b/packages/hoppscotch-app/components/http/Variables.vue index d7e8ff06a..daf98d62d 100644 --- a/packages/hoppscotch-app/components/http/Variables.vue +++ b/packages/hoppscotch-app/components/http/Variables.vue @@ -3,7 +3,7 @@
- +
const aggregateVars = useReadonlyStream(restVars$, []) as Ref +const aggregateVars = useReadonlyStream(restVars$, []) as Ref + const envVars = computed(() => props.envs ? props.envs.map((x) => ({ diff --git a/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts b/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts index c90c012b0..94dff0c27 100644 --- a/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts +++ b/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts @@ -14,7 +14,11 @@ import { HoppRESTHeader, HoppRESTParam, } from "@hoppscotch/data" +<<<<<<< HEAD import { parseTemplateStringV } from "@hoppscotch/data/src/pathVariables" +======= +import { parseTemplateStringV } from "@hoppscotch/data/src/variables" +>>>>>>> origin/codeday/main import { arrayFlatMap, arraySort } from "../functional/array" import { toFormData } from "../functional/formData" import { tupleToRecord } from "../functional/record" @@ -302,6 +306,8 @@ export function getEffectiveRESTRequest( ) const effectiveFinalVars = request.vars + const effectiveFinalVars = request.vars + const effectiveFinalBody = getFinalBodyFromRequest(request, envVariables) return { diff --git a/packages/hoppscotch-data/src/environment.ts b/packages/hoppscotch-data/src/environment.ts index 1a6e58b36..45d3838f5 100644 --- a/packages/hoppscotch-data/src/environment.ts +++ b/packages/hoppscotch-data/src/environment.ts @@ -12,11 +12,18 @@ export type Environment = { export type Variables = { key: string value: string +<<<<<<< HEAD }[] const REGEX_ENV_VAR = /<<([^>]*)>>/g // "<>" const REGEX_PATHVAR = /{{([^>]*)}}/g // "{{myVariable}}" +======= +}[] + +const REGEX_ENV_VAR = /<<([^>]*)>>/g // "<>" +const REGEX_PATH_VAR = /{{([^>]*)}}/g // "{{myVariable}}" +>>>>>>> origin/codeday/main /** * How much times can we expand environment variables diff --git a/packages/hoppscotch-data/src/variables.ts b/packages/hoppscotch-data/src/variables.ts new file mode 100644 index 000000000..239a8d2ac --- /dev/null +++ b/packages/hoppscotch-data/src/variables.ts @@ -0,0 +1,78 @@ +import { pipe } from "fp-ts/function" +import * as E from "fp-ts/Either" +import {parseTemplateStringE} from "./environment"; + +export type Environment = { + name: string + variables: { + key: string + value: string + }[] +} + +export type Variables = { + key: string + value: string +}[] + +const REGEX_ENV_VAR = /<<([^>]*)>>/g // "<>" +const REGEX_MY_VAR = /{{([^}]*)}}/g // "{{myVariable}}" + +/** + * How much times can we expand environment variables + */ +const ENV_MAX_EXPAND_LIMIT = 10 + +/** + * Error state when there is a suspected loop while + * recursively expanding variables + */ +const ENV_EXPAND_LOOP = "ENV_EXPAND_LOOP" as const + +export function parseTemplateStringEV( + str: string, + variables: Environment["variables"], + myVariables: Variables +) { + if (!variables || !str || !myVariables) { + return E.right(str) + } + + let result = str + let depth = 0 + + while (result.match(REGEX_ENV_VAR) != null && depth <= ENV_MAX_EXPAND_LIMIT) { + result = decodeURI(encodeURI(result)).replace( + REGEX_ENV_VAR, + (_, p1) => variables.find((x) => x.key === p1)?.value || "" + ) + depth++ + } + + /** + * TODO: Create an error state when there is a suspected loop while recursively expanding these variables + */ + while (result.match(REGEX_MY_VAR) != null && depth <= ENV_MAX_EXPAND_LIMIT) { + result = decodeURI(encodeURI(result)).replace( + REGEX_MY_VAR, + (_, p1) => myVariables.find((x) => x.key === p1)?.value || "" + ) + } + + return depth > ENV_MAX_EXPAND_LIMIT + ? E.left(ENV_EXPAND_LOOP) + : E.right(result) +} + +/** + * @deprecated Use `parseTemplateStringEV` instead + */ +export const parseTemplateStringV = ( + str: string, + variables: Environment["variables"], + myVariables: Variables +) => + pipe( + parseTemplateStringEV(str, variables, myVariables), + E.getOrElse(() => str) + ) From bdfdb44743af2f120f4e6622562068932a076230 Mon Sep 17 00:00:00 2001 From: isaiM6 <98564922+isaiM6@users.noreply.github.com> Date: Mon, 1 Aug 2022 14:42:48 -0700 Subject: [PATCH 2/2] forced commit --- packages/hoppscotch-data/src/pathVariables.ts | 76 ------------------- 1 file changed, 76 deletions(-) delete mode 100644 packages/hoppscotch-data/src/pathVariables.ts diff --git a/packages/hoppscotch-data/src/pathVariables.ts b/packages/hoppscotch-data/src/pathVariables.ts deleted file mode 100644 index 44892680d..000000000 --- a/packages/hoppscotch-data/src/pathVariables.ts +++ /dev/null @@ -1,76 +0,0 @@ -import { pipe } from "fp-ts/function" -import * as E from "fp-ts/Either" -import {parseTemplateStringE} from "./environment"; - -export type Environment = { - name: string - variables: { - key: string - value: string - }[] -} - -export type Variables = { - key: string - value: string -}[] - -const REGEX_ENV_VAR = /<<([^>]*)>>/g // "<>" -const REGEX_PATHVAR = /{{([^>]*)}}/g // "{{myVariable}}" - -/** - * How much times can we expand environment variables - */ -const ENV_MAX_EXPAND_LIMIT = 10 - -/** - * Error state when there is a suspected loop while - * recursively expanding variables - */ -const ENV_EXPAND_LOOP = "ENV_EXPAND_LOOP" as const - -export function parseTemplateStringEV( - str: string, - variables: Environment["variables"], - pathVariables: Variables -) { - if (!variables || !str || !pathVariables) { - return E.right(str) - } - - let result = str - let depth = 0 - - while (result.match(REGEX_ENV_VAR) != null && depth <= ENV_MAX_EXPAND_LIMIT) { - result = decodeURI(encodeURI(result)).replace( - REGEX_ENV_VAR, - (_, p1) => variables.find((x) => x.key === p1)?.value || "" - ) - depth++ - } - - while (result.match(REGEX_PATHVAR) != null && depth <= ENV_MAX_EXPAND_LIMIT) { - result = decodeURI(encodeURI(result)).replace( - REGEX_PATHVAR, - (_, p1) => pathVariables.find((x) => x.key === p1)?.value || "" - ) - } - - return depth > ENV_MAX_EXPAND_LIMIT - ? E.left(ENV_EXPAND_LOOP) - : E.right(result) -} - -/** - * @deprecated Use `parseTemplateStringE` instead - */ -export const parseTemplateStringV = ( - str: string, - variables: Environment["variables"], - pathVariables: Variables -) => - pipe( - parseTemplateStringEV(str, variables, pathVariables), - E.getOrElse(() => str) - ) -