diff --git a/src/replaceVars.ts b/src/replaceVars.ts index 3e78449..1accb4a 100644 --- a/src/replaceVars.ts +++ b/src/replaceVars.ts @@ -137,9 +137,10 @@ export function replaceVariablesInRequest(request: RequestSpec, variables: Varia type keyOfHttp = Exclude; const httpPropertiesToReplace: string[] = ["baseUrl", "url", "params", "headers", "body"]; - httpPropertiesToReplace.forEach((key) => { - const replacedData = replaceVariables(request.httpRequest[key as keyOfHttp], variables); - request.httpRequest[key as keyOfHttp] = replacedData.data; + httpPropertiesToReplace.forEach((prop) => { + const httpKey = prop as keyOfHttp; + const replacedData = replaceVariables(request.httpRequest[httpKey], variables); + request.httpRequest[httpKey] = replacedData.data; undefs.push(...replacedData.undefinedVars); }); diff --git a/src/variableParser.ts b/src/variableParser.ts index 69f88f9..0d2e715 100644 --- a/src/variableParser.ts +++ b/src/variableParser.ts @@ -38,6 +38,22 @@ export function getEnvironments(bundleContent: string | undefined, varFileConten return [...uniqueNames]; } +function replaceEnvironmentVariables(vars: Variables): Variables { + const PREFIX = "$env."; + + const getVal = (val: any): any => { + if (typeof val !== "string" || !val.startsWith(PREFIX)) return val; + + const envVarName = val.slice(PREFIX.length); + return envVarName in process.env ? process.env[envVarName] : val; + }; + + const replacedVars: Variables = {}; + for (const key in vars) replacedVars[key] = getVal(vars[key]); + + return replacedVars; +} + export function loadVariables( envName: string | undefined, bundleContent: string | undefined, @@ -46,15 +62,16 @@ export function loadVariables( if (!envName) return {}; const allBundleVariables = getBundleVariables(bundleContent); - const bundleVars = allBundleVariables.hasOwnProperty(envName) ? allBundleVariables[envName] : {}; + const bundleVars: Variables = allBundleVariables[envName] ?? {}; - let envVars = {}; + const envVars: Variables = {}; varFileContents.forEach((fileContents) => { const parsedData = YAML.parse(fileContents); - if (parsedData && isDict(parsedData[envName])) { - Object.assign(envVars, parsedData[envName]); - } + if (parsedData && isDict(parsedData[envName])) Object.assign(envVars, parsedData[envName]); }); - return Object.assign({}, envVars, bundleVars); + const basicVars = Object.assign({}, envVars, bundleVars); + const vars = replaceEnvironmentVariables(basicVars); + + return vars; }