From 8a25edcdc3ca07e6e45f12236f486f9bd7dcd57e Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Sat, 8 Jun 2024 19:53:53 +0530 Subject: [PATCH 01/12] feat: added basic functionality for env var replacement in values of existing variables --- src/variableParser.ts | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/variableParser.ts b/src/variableParser.ts index 69f88f9..ce83450 100644 --- a/src/variableParser.ts +++ b/src/variableParser.ts @@ -38,17 +38,33 @@ 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]); +} + export function loadVariables( envName: string | undefined, bundleContent: string | undefined, - varFileContents: string[], + varFileContents: string[] ): Variables { if (!envName) return {}; const allBundleVariables = getBundleVariables(bundleContent); - const bundleVars = allBundleVariables.hasOwnProperty(envName) ? allBundleVariables[envName] : {}; + const bundleVars: Variables = allBundleVariables.hasOwnProperty(envName) + ? allBundleVariables[envName] + : {}; - let envVars = {}; + let envVars: Variables = {}; varFileContents.forEach((fileContents) => { const parsedData = YAML.parse(fileContents); if (parsedData && isDict(parsedData[envName])) { @@ -56,5 +72,7 @@ export function loadVariables( } }); - return Object.assign({}, envVars, bundleVars); + const basicVars = Object.assign({}, envVars, bundleVars); + const envReplaced = replaceEnvironmentVariables(basicVars); + return envReplaced; } From 64a99bbb7c5e80e4e83d28a391f93e168ff2eefc Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Sat, 8 Jun 2024 19:54:28 +0530 Subject: [PATCH 02/12] refactor: prettier --- src/variableParser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/variableParser.ts b/src/variableParser.ts index ce83450..c10857f 100644 --- a/src/variableParser.ts +++ b/src/variableParser.ts @@ -55,7 +55,7 @@ function replaceEnvironmentVariables(vars: Variables): Variables { export function loadVariables( envName: string | undefined, bundleContent: string | undefined, - varFileContents: string[] + varFileContents: string[], ): Variables { if (!envName) return {}; From 5fbe9aad28ca43d1d272cee1eec009493fb87288 Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Sat, 8 Jun 2024 19:55:58 +0530 Subject: [PATCH 03/12] bug: returning replaced vars in helper --- src/variableParser.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/variableParser.ts b/src/variableParser.ts index c10857f..e10cfa3 100644 --- a/src/variableParser.ts +++ b/src/variableParser.ts @@ -50,12 +50,14 @@ function replaceEnvironmentVariables(vars: Variables): Variables { const replacedVars: Variables = {}; for (const key in vars) replacedVars[key] = getVal(vars[key]); + + return replacedVars; } export function loadVariables( envName: string | undefined, bundleContent: string | undefined, - varFileContents: string[], + varFileContents: string[] ): Variables { if (!envName) return {}; From a641c3a52e05a23f353db47aa0aeb373b291291a Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Sat, 8 Jun 2024 19:58:09 +0530 Subject: [PATCH 04/12] refactor: prettier --- src/variableParser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/variableParser.ts b/src/variableParser.ts index e10cfa3..1743d5f 100644 --- a/src/variableParser.ts +++ b/src/variableParser.ts @@ -57,7 +57,7 @@ function replaceEnvironmentVariables(vars: Variables): Variables { export function loadVariables( envName: string | undefined, bundleContent: string | undefined, - varFileContents: string[] + varFileContents: string[], ): Variables { if (!envName) return {}; From 98ae00d3247f6a6c3f38e83ff909709d8b1650ba Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Sat, 8 Jun 2024 21:40:38 +0530 Subject: [PATCH 05/12] refactor: simplified bundle variables code --- src/variableParser.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/variableParser.ts b/src/variableParser.ts index 1743d5f..e113aed 100644 --- a/src/variableParser.ts +++ b/src/variableParser.ts @@ -62,16 +62,12 @@ export function loadVariables( if (!envName) return {}; const allBundleVariables = getBundleVariables(bundleContent); - const bundleVars: Variables = allBundleVariables.hasOwnProperty(envName) - ? allBundleVariables[envName] - : {}; + const bundleVars: Variables = allBundleVariables[envName] ?? {}; - let envVars: Variables = {}; + 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]); }); const basicVars = Object.assign({}, envVars, bundleVars); From c5416550b508e7437cfab389d37ba6bc50dcac9c Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Mon, 10 Jun 2024 12:17:02 +0530 Subject: [PATCH 06/12] feat: accounting for undefined variables in environment variable replacement --- src/variableParser.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/variableParser.ts b/src/variableParser.ts index e113aed..1163849 100644 --- a/src/variableParser.ts +++ b/src/variableParser.ts @@ -38,28 +38,34 @@ export function getEnvironments(bundleContent: string | undefined, varFileConten return [...uniqueNames]; } -function replaceEnvironmentVariables(vars: Variables): Variables { +function replaceEnvironmentVariables(vars: Variables): { + replacedVars: Variables; + undefinedVars: string[]; +} { const PREFIX = "$env."; + const undefinedVars: string[] = []; 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; + if (envVarName in process.env) return process.env[envVarName]; + + undefinedVars.push(envVarName); }; const replacedVars: Variables = {}; for (const key in vars) replacedVars[key] = getVal(vars[key]); - return replacedVars; + return { replacedVars, undefinedVars }; } export function loadVariables( envName: string | undefined, bundleContent: string | undefined, varFileContents: string[], -): Variables { - if (!envName) return {}; +): { vars: Variables; undefinedVars: string[] } { + if (!envName) return { vars: {}, undefinedVars: [] }; const allBundleVariables = getBundleVariables(bundleContent); const bundleVars: Variables = allBundleVariables[envName] ?? {}; @@ -71,6 +77,7 @@ export function loadVariables( }); const basicVars = Object.assign({}, envVars, bundleVars); - const envReplaced = replaceEnvironmentVariables(basicVars); - return envReplaced; + const { replacedVars: vars, undefinedVars } = replaceEnvironmentVariables(basicVars); + + return { vars, undefinedVars }; } From 28b91db73999a2544153f740c5cf0e62412c2327 Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Mon, 10 Jun 2024 12:19:06 +0530 Subject: [PATCH 07/12] bug: returning original string in case of undefined variable --- src/variableParser.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/variableParser.ts b/src/variableParser.ts index 1163849..f78da04 100644 --- a/src/variableParser.ts +++ b/src/variableParser.ts @@ -52,6 +52,7 @@ function replaceEnvironmentVariables(vars: Variables): { if (envVarName in process.env) return process.env[envVarName]; undefinedVars.push(envVarName); + return val; }; const replacedVars: Variables = {}; @@ -63,7 +64,7 @@ function replaceEnvironmentVariables(vars: Variables): { export function loadVariables( envName: string | undefined, bundleContent: string | undefined, - varFileContents: string[], + varFileContents: string[] ): { vars: Variables; undefinedVars: string[] } { if (!envName) return { vars: {}, undefinedVars: [] }; From 5431fa9b88da21775aac2b172f9127e53e37c517 Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Mon, 10 Jun 2024 12:19:28 +0530 Subject: [PATCH 08/12] refactor: prettier --- src/variableParser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/variableParser.ts b/src/variableParser.ts index f78da04..786dab9 100644 --- a/src/variableParser.ts +++ b/src/variableParser.ts @@ -64,7 +64,7 @@ function replaceEnvironmentVariables(vars: Variables): { export function loadVariables( envName: string | undefined, bundleContent: string | undefined, - varFileContents: string[] + varFileContents: string[], ): { vars: Variables; undefinedVars: string[] } { if (!envName) return { vars: {}, undefinedVars: [] }; From 2ecc883566332685add06fe55fb39e087b7b8ad7 Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Mon, 10 Jun 2024 12:42:13 +0530 Subject: [PATCH 09/12] refactor: reducing repetition in httpRequest's key --- src/replaceVars.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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); }); From ae7918f96b9099ac6d1f9d5d5a36e77fd2dcea47 Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Mon, 10 Jun 2024 23:08:35 +0530 Subject: [PATCH 10/12] feat: showing entire val in undefined instead of just $env. for clarity --- src/variableParser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/variableParser.ts b/src/variableParser.ts index 786dab9..035c2f8 100644 --- a/src/variableParser.ts +++ b/src/variableParser.ts @@ -51,7 +51,7 @@ function replaceEnvironmentVariables(vars: Variables): { const envVarName = val.slice(PREFIX.length); if (envVarName in process.env) return process.env[envVarName]; - undefinedVars.push(envVarName); + undefinedVars.push(val); return val; }; From e080398226b327a88c5aba3f3e50b91917ab1ac9 Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Wed, 26 Jun 2024 03:01:11 +0530 Subject: [PATCH 11/12] feat: removing undefined vars from env var replacement --- src/variableParser.ts | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/variableParser.ts b/src/variableParser.ts index 035c2f8..acdd558 100644 --- a/src/variableParser.ts +++ b/src/variableParser.ts @@ -38,34 +38,27 @@ export function getEnvironments(bundleContent: string | undefined, varFileConten return [...uniqueNames]; } -function replaceEnvironmentVariables(vars: Variables): { - replacedVars: Variables; - undefinedVars: string[]; -} { +function replaceEnvironmentVariables(vars: Variables): Variables { const PREFIX = "$env."; - const undefinedVars: string[] = []; const getVal = (val: any): any => { if (typeof val !== "string" || !val.startsWith(PREFIX)) return val; const envVarName = val.slice(PREFIX.length); - if (envVarName in process.env) return process.env[envVarName]; - - undefinedVars.push(val); - return val; + return envVarName in process.env ? process.env[envVarName] : val; }; const replacedVars: Variables = {}; for (const key in vars) replacedVars[key] = getVal(vars[key]); - return { replacedVars, undefinedVars }; + return replacedVars; } export function loadVariables( envName: string | undefined, bundleContent: string | undefined, varFileContents: string[], -): { vars: Variables; undefinedVars: string[] } { +): Variables { if (!envName) return { vars: {}, undefinedVars: [] }; const allBundleVariables = getBundleVariables(bundleContent); From 00b55c0a9a1c051be5a9c6e0246e07bd293d179f Mon Sep 17 00:00:00 2001 From: Varun0157 Date: Wed, 26 Jun 2024 03:03:04 +0530 Subject: [PATCH 12/12] bug: removing returns of empty undefined vars from loadVars --- src/variableParser.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/variableParser.ts b/src/variableParser.ts index acdd558..0d2e715 100644 --- a/src/variableParser.ts +++ b/src/variableParser.ts @@ -59,7 +59,7 @@ export function loadVariables( bundleContent: string | undefined, varFileContents: string[], ): Variables { - if (!envName) return { vars: {}, undefinedVars: [] }; + if (!envName) return {}; const allBundleVariables = getBundleVariables(bundleContent); const bundleVars: Variables = allBundleVariables[envName] ?? {}; @@ -71,7 +71,7 @@ export function loadVariables( }); const basicVars = Object.assign({}, envVars, bundleVars); - const { replacedVars: vars, undefinedVars } = replaceEnvironmentVariables(basicVars); + const vars = replaceEnvironmentVariables(basicVars); - return { vars, undefinedVars }; + return vars; }