From 775bf9a9c347fee83cafdc74e6359a8616586f98 Mon Sep 17 00:00:00 2001 From: isaiM6 <98564922+isaiM6@users.noreply.github.com> Date: Wed, 3 Aug 2022 16:01:19 -0700 Subject: [PATCH] Merged environment.ts and variables.ts --- .../components/smart/EnvInput.vue | 1 - .../helpers/utils/EffectiveURL.ts | 3 +- packages/hoppscotch-data/src/environment.ts | 29 +++++++++++++++---- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/packages/hoppscotch-app/components/smart/EnvInput.vue b/packages/hoppscotch-app/components/smart/EnvInput.vue index 1062780fa..81bc6bbdf 100644 --- a/packages/hoppscotch-app/components/smart/EnvInput.vue +++ b/packages/hoppscotch-app/components/smart/EnvInput.vue @@ -229,7 +229,6 @@ onMounted(() => { if (editor.value) { if (!view.value) initView(editor.value) } - console.log("editor.value", editor.value) }) watch(editor, () => { diff --git a/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts b/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts index d2ede9952..276f9d08b 100644 --- a/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts +++ b/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts @@ -14,7 +14,6 @@ import { HoppRESTHeader, HoppRESTParam, } from "@hoppscotch/data" -import { parseTemplateStringV } from "@hoppscotch/data/src/variables" import { arrayFlatMap, arraySort } from "../functional/array" import { toFormData } from "../functional/formData" import { tupleToRecord } from "../functional/record" @@ -306,7 +305,7 @@ export function getEffectiveRESTRequest( return { ...request, - effectiveFinalURL: parseTemplateStringV( + effectiveFinalURL: parseTemplateString( request.endpoint, envVariables, request.vars diff --git a/packages/hoppscotch-data/src/environment.ts b/packages/hoppscotch-data/src/environment.ts index d8db48a3a..424a971a6 100644 --- a/packages/hoppscotch-data/src/environment.ts +++ b/packages/hoppscotch-data/src/environment.ts @@ -16,7 +16,7 @@ export type Variables = { const REGEX_ENV_VAR = /<<([^>]*)>>/g // "<>" -const REGEX_PATH_VAR = /{{([^}]*)}}/g // "{{myVariable}}" +const REGEX_MY_VAR = /{{([^}]*)}}/g // "{{myVariable}}" /** * How much times can we expand environment variables @@ -67,13 +67,20 @@ export const parseBodyEnvVariables = ( export function parseTemplateStringE( str: string, variables: Environment["variables"], + myVariables?: Variables ) { + /* if (!variables || !str ) { return E.right(str) } + */ + if (!variables || !str || !myVariables) { + return E.right(str) + } let result = str let depth = 0 + let errorBound = 0 while (result.match(REGEX_ENV_VAR) != null && depth <= ENV_MAX_EXPAND_LIMIT) { result = decodeURI(encodeURI(result)).replace( @@ -83,9 +90,18 @@ export function parseTemplateStringE( depth++ } - return depth > ENV_MAX_EXPAND_LIMIT - ? E.left(ENV_EXPAND_LOOP) - : E.right(result) + while (result.match(REGEX_MY_VAR) != null && errorBound <= ENV_MAX_EXPAND_LIMIT) { + result = decodeURI(encodeURI(result)).replace( + REGEX_MY_VAR, + (_, p1) => myVariables.find((x) => x.key === p1)?.value || "" + ) + errorBound++ + } + if(depth > ENV_MAX_EXPAND_LIMIT && errorBound <= ENV_MAX_EXPAND_LIMIT) { + return E.right(result) + }else{ + return E.left(ENV_EXPAND_LOOP) + } } /** @@ -93,9 +109,10 @@ export function parseTemplateStringE( */ export const parseTemplateString = ( str: string, - variables: Environment["variables"] + variables: Environment["variables"], + myVariables?: Variables ) => pipe( - parseTemplateStringE(str, variables), + parseTemplateStringE(str, variables, myVariables), E.getOrElse(() => str) )