From 0ac84b58e355a56037c17bd5b35f9146ed0c03ea Mon Sep 17 00:00:00 2001 From: 0xc0Der <59133155+0xc0Der@users.noreply.github.com> Date: Wed, 10 Nov 2021 16:22:11 +0200 Subject: [PATCH] allow environment variables in request body. (#1942) * feat: allow environment variables in request body * chore(ui): minor ui improvements * chore(deps): bump * fix: track env vars changes * feat: allow environment variables in request body * refactor: better implementation Co-authored-by: liyasthomas Co-authored-by: Andrew Bastin --- .../hoppscotch-app/components/smart/EnvInput.vue | 3 ++- packages/hoppscotch-app/helpers/templating.ts | 12 +++++++++++- .../hoppscotch-app/helpers/utils/EffectiveURL.ts | 4 ++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/hoppscotch-app/components/smart/EnvInput.vue b/packages/hoppscotch-app/components/smart/EnvInput.vue index f392bfc32..fbc4b05a2 100644 --- a/packages/hoppscotch-app/components/smart/EnvInput.vue +++ b/packages/hoppscotch-app/components/smart/EnvInput.vue @@ -461,7 +461,8 @@ export default defineComponent({ return "choose an environment" }, getEnvValue(value) { - if (value) return value + if (value) return value.replace(/"/g, """) + // it does not filter special characters before adding them to HTML. return "not found" }, }, diff --git a/packages/hoppscotch-app/helpers/templating.ts b/packages/hoppscotch-app/helpers/templating.ts index 073a764f5..64f9556be 100644 --- a/packages/hoppscotch-app/helpers/templating.ts +++ b/packages/hoppscotch-app/helpers/templating.ts @@ -1,6 +1,16 @@ import { Environment } from "~/newstore/environments" -export default function parseTemplateString( +export function parseBodyEnvVariables( + body: string, + env: Environment["variables"] +) { + return body.replace(/<<\w+>>/g, (key) => { + const found = env.find((envVar) => envVar.key === key.replace(/[<>]/g, "")) + return found ? found.value : key + }) +} + +export function parseTemplateString( str: string, variables: Environment["variables"] ) { diff --git a/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts b/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts index e17494fb1..9190bd188 100644 --- a/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts +++ b/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts @@ -1,7 +1,7 @@ import { combineLatest, Observable } from "rxjs" import { map } from "rxjs/operators" import { FormDataKeyValue, HoppRESTRequest } from "../types/HoppRESTRequest" -import parseTemplateString from "../templating" +import { parseTemplateString, parseBodyEnvVariables } from "../templating" import { Environment, getGlobalVariables } from "~/newstore/environments" export interface EffectiveHoppRESTRequest extends HoppRESTRequest { @@ -46,7 +46,7 @@ function getFinalBodyFromRequest( }) return formData - } else return request.body.body + } else return parseBodyEnvVariables(request.body.body, env.variables) } /**