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 0590a6a47..25fcb08da 100644 --- a/packages/hoppscotch-app/components/http/Variables.vue +++ b/packages/hoppscotch-app/components/http/Variables.vue @@ -3,7 +3,7 @@
- +
]*)>>/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) + )