Merging my branch into codeday/main

This commit is contained in:
Adrian Tuschek
2022-08-03 16:47:52 -07:00
2 changed files with 23 additions and 8 deletions

View File

@@ -14,7 +14,6 @@ import {
HoppRESTHeader, HoppRESTHeader,
HoppRESTParam, HoppRESTParam,
} from "@hoppscotch/data" } from "@hoppscotch/data"
import { parseTemplateStringV } from "@hoppscotch/data/src/variables"
import { arrayFlatMap, arraySort } from "../functional/array" import { arrayFlatMap, arraySort } from "../functional/array"
import { toFormData } from "../functional/formData" import { toFormData } from "../functional/formData"
import { tupleToRecord } from "../functional/record" import { tupleToRecord } from "../functional/record"
@@ -306,7 +305,7 @@ export function getEffectiveRESTRequest(
return { return {
...request, ...request,
effectiveFinalURL: parseTemplateStringV( effectiveFinalURL: parseTemplateString(
request.endpoint, request.endpoint,
envVariables, envVariables,
request.vars request.vars

View File

@@ -9,7 +9,13 @@ export type Environment = {
}[] }[]
} }
export type Variables = {
key: string
value: string
}[]
const REGEX_ENV_VAR = /<<([^>]*)>>/g // "<<myVariable>>" const REGEX_ENV_VAR = /<<([^>]*)>>/g // "<<myVariable>>"
const REGEX_MY_VAR = /{{([^}]*)}}/g // "{{myVariable}}"
/** /**
* How much times can we expand environment variables * How much times can we expand environment variables
@@ -60,13 +66,16 @@ export const parseBodyEnvVariables = (
export function parseTemplateStringE( export function parseTemplateStringE(
str: string, str: string,
variables: Environment["variables"], variables: Environment["variables"],
myVariables?: Variables
) { ) {
if (!variables || !str ) {
if (!variables || !str || !myVariables) {
return E.right(str) return E.right(str)
} }
let result = str let result = str
let depth = 0 let depth = 0
let errorBound = 0
while (result.match(REGEX_ENV_VAR) != null && depth <= ENV_MAX_EXPAND_LIMIT) { while (result.match(REGEX_ENV_VAR) != null && depth <= ENV_MAX_EXPAND_LIMIT) {
result = decodeURI(encodeURI(result)).replace( result = decodeURI(encodeURI(result)).replace(
@@ -76,9 +85,15 @@ export function parseTemplateStringE(
depth++ depth++
} }
return depth > ENV_MAX_EXPAND_LIMIT while (result.match(REGEX_MY_VAR) != null && errorBound <= ENV_MAX_EXPAND_LIMIT) {
? E.left(ENV_EXPAND_LOOP) result = decodeURI(encodeURI(result)).replace(
: E.right(result) REGEX_MY_VAR,
(_, p1) => myVariables.find((x) => x.key === p1)?.value || ""
)
errorBound++
}
return depth <= ENV_MAX_EXPAND_LIMIT && errorBound <= ENV_MAX_EXPAND_LIMIT ? E.right(result) : E.left(ENV_EXPAND_LOOP);
} }
/** /**
@@ -86,9 +101,10 @@ export function parseTemplateStringE(
*/ */
export const parseTemplateString = ( export const parseTemplateString = (
str: string, str: string,
variables: Environment["variables"] variables: Environment["variables"],
myVariables?: Variables
) => ) =>
pipe( pipe(
parseTemplateStringE(str, variables), parseTemplateStringE(str, variables, myVariables),
E.getOrElse(() => str) E.getOrElse(() => str)
) )