From 01d96fa5771f5c1a5ba4cf7e770ca1e2522939b0 Mon Sep 17 00:00:00 2001 From: Nivedin <53208152+nivedin@users.noreply.github.com> Date: Fri, 23 May 2025 22:38:36 +0530 Subject: [PATCH] feat: initial and current value for environment variables (#5055) --- .../src/__tests__/e2e/commands/test.spec.ts | 1 + .../environments/aws-signature-auth-envs.json | 18 +- .../environments/digest-auth-envs.json | 11 +- .../e2e/fixtures/environments/env-v1.json | 18 +- .../e2e/fixtures/environments/env-v2.json | 13 ++ .../environments/request-vars-envs.json | 17 +- ...ecret-envs-persistence-scripting-envs.json | 12 +- .../fixtures/environments/secret-envs.json | 22 ++- .../secret-supplied-values-envs.json | 28 ++- .../src/__tests__/unit/getters.spec.ts | 40 ++-- packages/hoppscotch-cli/src/commands/test.ts | 3 +- .../hoppscotch-cli/src/options/test/env.ts | 13 +- packages/hoppscotch-cli/src/utils/getters.ts | 22 ++- packages/hoppscotch-cli/src/utils/request.ts | 6 +- packages/hoppscotch-common/locales/en.json | 6 +- .../src/components/environments/Add.vue | 96 ++++++--- .../components/environments/ImportExport.vue | 6 +- .../src/components/environments/Selector.vue | 48 +++-- .../src/components/environments/index.vue | 3 +- .../components/environments/my/Details.vue | 144 ++++++++++---- .../environments/my/Environment.vue | 3 + .../components/environments/teams/Details.vue | 113 ++++++++--- .../environments/teams/Environment.vue | 3 + .../src/components/http/Headers.vue | 24 ++- .../src/components/smart/EnvInput.vue | 18 +- .../src/helpers/RequestRunner.ts | 142 +++++++++---- .../editor/extensions/HoppEnvironment.ts | 31 ++- .../import-export/export/environment.ts | 19 +- .../helpers/import-export/import/hoppEnv.ts | 4 +- .../import-export/import/insomniaEnv.ts | 13 +- .../import-export/import/postmanEnv.ts | 5 +- .../src/helpers/preRequest.ts | 39 ++-- .../helpers/teams/TeamEnvironmentAdapter.ts | 6 +- .../src/newstore/environments.ts | 186 ++++++++++++------ .../current-environment-value.service.spec.ts | 178 +++++++++++++++++ .../current-environment-value.service.ts | 162 +++++++++++++++ .../__tests__/environment.inspector.spec.ts | 116 +++++++++-- .../inspectors/environment.inspector.ts | 18 +- .../persistence/__tests__/__mocks__/index.ts | 27 ++- .../src/services/persistence/index.ts | 58 ++++++ .../persistence/validation-schemas/index.ts | 27 ++- .../services/secret-environment.service.ts | 4 +- .../searchers/environment.searcher.ts | 19 +- .../hoppscotch-data/src/environment/index.ts | 39 ++-- .../hoppscotch-data/src/environment/v/2.ts | 40 ++++ .../src/global-environment/index.ts | 4 +- .../src/global-environment/v/2.ts | 39 ++++ .../__tests__/base64-helper-functions.spec.ts | 18 +- .../src/__tests__/env/get.spec.ts | 15 +- .../src/__tests__/env/getResolve.spec.ts | 24 ++- .../src/__tests__/env/resolve.spec.ts | 18 +- .../src/__tests__/env/set.spec.ts | 27 ++- .../src/__tests__/env/unset.spec.ts | 42 ++-- .../src/__tests__/pre-request.spec.ts | 74 +++++-- .../hoppscotch-js-sandbox/src/shared-utils.ts | 18 +- .../hoppscotch-js-sandbox/src/types/index.ts | 3 +- .../platform/environments/desktop/index.ts | 4 +- .../src/platform/environments/desktop/sync.ts | 2 +- .../src/platform/environments/web/index.ts | 4 +- .../src/platform/environments/web/sync.ts | 2 +- 60 files changed, 1684 insertions(+), 431 deletions(-) create mode 100644 packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/env-v2.json create mode 100644 packages/hoppscotch-common/src/services/__tests__/current-environment-value.service.spec.ts create mode 100644 packages/hoppscotch-common/src/services/current-environment-value.service.ts create mode 100644 packages/hoppscotch-data/src/environment/v/2.ts create mode 100644 packages/hoppscotch-data/src/global-environment/v/2.ts diff --git a/packages/hoppscotch-cli/src/__tests__/e2e/commands/test.spec.ts b/packages/hoppscotch-cli/src/__tests__/e2e/commands/test.spec.ts index e350e077..3de3367f 100644 --- a/packages/hoppscotch-cli/src/__tests__/e2e/commands/test.spec.ts +++ b/packages/hoppscotch-cli/src/__tests__/e2e/commands/test.spec.ts @@ -95,6 +95,7 @@ describe("hopp test [options] ", { timeout: 100000 }, () => { const testFixtures = [ { fileName: "env-v0.json", version: 0 }, { fileName: "env-v1.json", version: 1 }, + { fileName: "env-v2.json", version: 2 }, ]; testFixtures.forEach(({ fileName, version }) => { diff --git a/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/aws-signature-auth-envs.json b/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/aws-signature-auth-envs.json index 534bf711..486e69c6 100644 --- a/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/aws-signature-auth-envs.json +++ b/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/aws-signature-auth-envs.json @@ -1,34 +1,42 @@ { - "v": 1, + "v": 2, "id": "cm0dsn3v70004p4qk3l9b7sjm", "name": "AWS Signature - environments", "variables": [ { "key": "awsRegion", - "value": "us-east-1", + "currentValue": "us-east-1", + "initialValue": "us-east-1", "secret": false }, { "key": "serviceName", - "value": "s3", + "currentValue": "s3", + "initialValue": "s3", "secret": false }, { "key": "accessKey", - "value": "test-access-key", + "currentValue": "test-access-key", + "initialValue": "test-access-key", "secret": true }, { "key": "secretKey", + "currentValue": "", + "initialValue": "", "secret": true }, { "key": "url", - "value": "https://echo.hoppscotch.io", + "currentValue": "https://echo.hoppscotch.io", + "initialValue": "https://echo.hoppscotch.io", "secret": false }, { "key": "serviceToken", + "currentValue": "", + "initialValue": "", "secret": true } ] diff --git a/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/digest-auth-envs.json b/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/digest-auth-envs.json index eacbcbc0..eb1c2aea 100644 --- a/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/digest-auth-envs.json +++ b/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/digest-auth-envs.json @@ -1,21 +1,24 @@ { - "v": 1, + "v": 2, "id": "cm0dsn3v70004p4qk3l9b7sjm", "name": "Digest Auth - environments", "variables": [ { "key": "username", - "value": "admin", + "currentValue": "", + "initialValue": "admin", "secret": true }, { "key": "password", - "value": "admin", + "currentValue": "", + "initialValue": "admin", "secret": true }, { "key": "url", - "value": "https://test.insightres.org/digest/" + "currentValue": "", + "initialValue": "https://test.insightres.org/digest/" } ] } diff --git a/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/env-v1.json b/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/env-v1.json index 4ab5aa65..c6f7c919 100644 --- a/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/env-v1.json +++ b/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/env-v1.json @@ -1,10 +1,10 @@ { - "name": "env-v0", - "variables": [ - { - "key": "baseURL", - "value": "https://echo.hoppscotch.io", - "secret": false - } - ] - } \ No newline at end of file + "name": "env-v1", + "variables": [ + { + "key": "baseURL", + "value": "https://echo.hoppscotch.io", + "secret": false + } + ] +} diff --git a/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/env-v2.json b/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/env-v2.json new file mode 100644 index 00000000..1c170dbd --- /dev/null +++ b/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/env-v2.json @@ -0,0 +1,13 @@ +{ + "id": "env-v2", + "v": 2, + "name": "env-v2", + "variables": [ + { + "key": "baseURL", + "initialValue": "https://echo.hoppscotch.io", + "currentValue": "https://echo.hoppscotch.io", + "secret": false + } + ] +} diff --git a/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/request-vars-envs.json b/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/request-vars-envs.json index 844af010..ff96dced 100644 --- a/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/request-vars-envs.json +++ b/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/request-vars-envs.json @@ -1,33 +1,42 @@ { - "v": 1, + "v": 2, "id": "cm00r7kpb0006mbd2nq1560w6", "name": "Request variables alongside environment variables", "variables": [ { "key": "url", - "value": "https://echo.hoppscotch.io", + "initialValue": "https://echo.hoppscotch.io", + "currentValue": "https://echo.hoppscotch.io", "secret": false }, { "key": "secretBasicAuthPasswordEnvVar", + "initialValue": "", + "currentValue": "", "secret": true }, { "key": "secretBasicAuthUsernameEnvVar", - "value": "username", + "initialValue": "username", + "currentValue": "", "secret": true }, { "key": "username", + "initialValue": "", + "currentValue": "", "secret": true }, { "key": "password", + "initialValue": "", + "currentValue": "", "secret": true }, { "key": "testHeaderValue", - "value": "test-header-value", + "initialValue": "test-header-value", + "currentValue": "", "secret": false } ] diff --git a/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/secret-envs-persistence-scripting-envs.json b/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/secret-envs-persistence-scripting-envs.json index b03e0508..e1411452 100644 --- a/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/secret-envs-persistence-scripting-envs.json +++ b/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/secret-envs-persistence-scripting-envs.json @@ -1,26 +1,36 @@ { - "v": 1, + "v": 2, "id": "2", "name": "secret-envs-persistence-scripting-envs", "variables": [ { "key": "preReqVarOne", + "initialValue": "", + "currentValue": "", "secret": true }, { "key": "preReqVarTwo", + "initialValue": "", + "currentValue": "", "secret": true }, { "key": "postReqVarOne", + "initialValue": "", + "currentValue": "", "secret": true }, { "key": "preReqVarTwo", + "initialValue": "", + "currentValue": "", "secret": true }, { "key": "customHeaderValueFromSecretVar", + "initialValue": "", + "currentValue": "", "secret": true } ] diff --git a/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/secret-envs.json b/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/secret-envs.json index 266f821c..1c4ae820 100644 --- a/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/secret-envs.json +++ b/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/secret-envs.json @@ -1,44 +1,60 @@ { "id": "2", - "v": 1, + "v": 2, "name": "secret-envs", "variables": [ { "key": "secretBearerToken", + "initialValue": "", + "currentValue": "", "secret": true }, { "key": "secretBasicAuthUsername", + "initialValue": "", + "currentValue": "", "secret": true }, { "key": "secretBasicAuthPassword", + "initialValue": "", + "currentValue": "", "secret": true }, { "key": "secretQueryParamValue", + "initialValue": "", + "currentValue": "", "secret": true }, { "key": "secretBodyValue", + "initialValue": "", + "currentValue": "", "secret": true }, { "key": "secretHeaderValue", + "initialValue": "", + "currentValue": "", "secret": true }, { "key": "nonExistentValueInSystemEnv", + "initialValue": "", + "currentValue": "", "secret": true }, { "key": "echoHoppBaseURL", - "value": "https://echo.hoppscotch.io", + "initialValue": "https://echo.hoppscotch.io", + "currentValue": "", "secret": false }, { "key": "httpbinBaseURL", - "value": "https://httpbin.org", + "initialValue": "https://httpbin.org", + "currentValue": "", "secret": false } ] diff --git a/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/secret-supplied-values-envs.json b/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/secret-supplied-values-envs.json index 88155f84..9c331bd9 100644 --- a/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/secret-supplied-values-envs.json +++ b/packages/hoppscotch-cli/src/__tests__/e2e/fixtures/environments/secret-supplied-values-envs.json @@ -1,50 +1,60 @@ { - "v": 1, + "v": 2, "id": "2", "name": "secret-values-envs", "variables": [ { "key": "secretBearerToken", - "value": "test-token", + "initialValue": "test-token", + "currentValue": "test-token", "secret": true }, { "key": "secretBasicAuthUsername", - "value": "test-user", + "initialValue": "test-user", + "currentValue": "test-user", "secret": true }, { "key": "secretBasicAuthPassword", - "value": "test-pass", + "initialValue": "test-pass", + "currentValue": "test-pass", "secret": true }, { "key": "secretQueryParamValue", - "value": "secret-query-param-value", + "initialValue": "secret-query-param-value", + "currentValue": "secret-query-param-value", "secret": true }, { "key": "secretBodyValue", - "value": "secret-body-value", + "initialValue": "secret-body-value", + "currentValue": "secret-body-value", "secret": true }, { "key": "secretHeaderValue", - "value": "secret-header-value", + "initialValue": "secret-header-value", + "currentValue": "secret-header-value", "secret": true }, { "key": "nonExistentValueInSystemEnv", + "initialValue": "", + "currentValue": "", "secret": true }, { "key": "echoHoppBaseURL", - "value": "https://echo.hoppscotch.io", + "initialValue": "https://echo.hoppscotch.io", + "currentValue": "https://echo.hoppscotch.io", "secret": false }, { "key": "httpbinBaseURL", - "value": "https://httpbin.org", + "initialValue": "https://httpbin.org", + "currentValue": "https://httpbin.org", "secret": false } ] diff --git a/packages/hoppscotch-cli/src/__tests__/unit/getters.spec.ts b/packages/hoppscotch-cli/src/__tests__/unit/getters.spec.ts index 3950a9fe..627683eb 100644 --- a/packages/hoppscotch-cli/src/__tests__/unit/getters.spec.ts +++ b/packages/hoppscotch-cli/src/__tests__/unit/getters.spec.ts @@ -44,7 +44,12 @@ describe("getters", () => { describe("getEffectiveFinalMetaData", () => { const environmentVariables = [ - { key: "PARAM", value: "parsed_param", secret: false }, + { + key: "PARAM", + initialValue: "parsed_param", + currentValue: "parsed_param", + secret: false, + }, ]; test("Empty list of meta-data", () => { @@ -421,27 +426,32 @@ describe("getters", () => { const environmentVariables = [ { key: "SHARED_KEY_I", - value: "environment-variable-shared-value-I", + initialValue: "environment-variable-shared-value-I", + currentValue: "environment-variable-shared-value-I", secret: false, }, { key: "SHARED_KEY_II", - value: "environment-variable-shared-value-II", + initialValue: "environment-variable-shared-value-II", + currentValue: "environment-variable-shared-value-II", secret: false, }, { key: "ENV_VAR_III", - value: "environment-variable-value-III", + initialValue: "environment-variable-value-III", + currentValue: "environment-variable-value-III", secret: false, }, { key: "ENV_VAR_IV", - value: "environment-variable-value-IV", + initialValue: "environment-variable-value-IV", + currentValue: "environment-variable-value-IV", secret: false, }, { key: "ENV_VAR_V", - value: "environment-variable-value-V", + initialValue: "environment-variable-value-V", + currentValue: "environment-variable-value-V", secret: false, }, ]; @@ -450,32 +460,38 @@ describe("getters", () => { const expected = [ { key: "SHARED_KEY_I", - value: "request-variable-shared-value-I", + currentValue: "request-variable-shared-value-I", + initialValue: "request-variable-shared-value-I", secret: false, }, { key: "REQUEST_VAR_III", - value: "request-variable-value-III", + currentValue: "request-variable-value-III", + initialValue: "request-variable-value-III", secret: false, }, { key: "SHARED_KEY_II", - value: "environment-variable-shared-value-II", + currentValue: "environment-variable-shared-value-II", + initialValue: "environment-variable-shared-value-II", secret: false, }, { key: "ENV_VAR_III", - value: "environment-variable-value-III", + currentValue: "environment-variable-value-III", + initialValue: "environment-variable-value-III", secret: false, }, { key: "ENV_VAR_IV", - value: "environment-variable-value-IV", + currentValue: "environment-variable-value-IV", + initialValue: "environment-variable-value-IV", secret: false, }, { key: "ENV_VAR_V", - value: "environment-variable-value-V", + currentValue: "environment-variable-value-V", + initialValue: "environment-variable-value-V", secret: false, }, ]; diff --git a/packages/hoppscotch-cli/src/commands/test.ts b/packages/hoppscotch-cli/src/commands/test.ts index 0291fa31..3e40978c 100644 --- a/packages/hoppscotch-cli/src/commands/test.ts +++ b/packages/hoppscotch-cli/src/commands/test.ts @@ -75,7 +75,8 @@ export const test = (pathOrId: string, options: TestCmdOptions) => async () => { (key) => { key: key, - value: iterationDataItem[key], + initialValue: iterationDataItem[key], + currentValue: iterationDataItem[key], secret: false, } ) diff --git a/packages/hoppscotch-cli/src/options/test/env.ts b/packages/hoppscotch-cli/src/options/test/env.ts index cc674652..8bd3fa70 100644 --- a/packages/hoppscotch-cli/src/options/test/env.ts +++ b/packages/hoppscotch-cli/src/options/test/env.ts @@ -60,11 +60,16 @@ export async function parseEnvsData(options: TestCmdEnvironmentOptions) { if (HoppEnvKeyPairResult.success) { for (const [key, value] of Object.entries(HoppEnvKeyPairResult.data)) { - envPairs.push({ key, value, secret: false }); + envPairs.push({ + key, + initialValue: value, + currentValue: value, + secret: false, + }); } } else if (HoppEnvExportObjectResult.type === "ok") { // Original environment variables from the supplied export file - const originalEnvVariables = (contents as NonSecretEnvironment).variables; + const originalEnvVariables = (contents as Environment).variables; // Above environment variables conforming to the latest schema // `value` fields if specified will be omitted for secret environment variables @@ -73,10 +78,10 @@ export async function parseEnvsData(options: TestCmdEnvironmentOptions) { // The values supplied for secret environment variables have to be considered in the CLI // For each secret environment variable, include the value in case supplied const resolvedEnvVariables = migratedEnvVariables.map((variable, idx) => { - if (variable.secret && originalEnvVariables[idx].value) { + if (variable.secret && originalEnvVariables[idx].initialValue) { return { ...variable, - value: originalEnvVariables[idx].value, + initialValue: originalEnvVariables[idx].initialValue, }; } diff --git a/packages/hoppscotch-cli/src/utils/getters.ts b/packages/hoppscotch-cli/src/utils/getters.ts index 07fe43fc..c76ff0ea 100644 --- a/packages/hoppscotch-cli/src/utils/getters.ts +++ b/packages/hoppscotch-cli/src/utils/getters.ts @@ -275,9 +275,15 @@ export const getResolvedVariables = ( requestVariables: HoppRESTRequestVariables, environmentVariables: EnvironmentVariable[] ): EnvironmentVariable[] => { + // Transforming request variables to the shape of environment variables const activeRequestVariables = requestVariables .filter(({ active, value }) => active && value) - .map(({ key, value }) => ({ key, value, secret: false })); + .map(({ key, value }) => ({ + key, + initialValue: value, + currentValue: value, + secret: false, + })); const requestVariableKeys = activeRequestVariables.map(({ key }) => key); @@ -286,5 +292,17 @@ export const getResolvedVariables = ( ({ key }) => !requestVariableKeys.includes(key) ); - return [...activeRequestVariables, ...filteredEnvironmentVariables]; + // Setting currentValue to initialValue for environment variables + // because the exported file might not have the currentValue field + const processedEnvironmentVariables = filteredEnvironmentVariables.map( + ({ key, initialValue, currentValue, secret }) => ({ + key, + initialValue, + currentValue: + currentValue && currentValue !== "" ? currentValue : initialValue, + secret, + }) + ); + + return [...activeRequestVariables, ...processedEnvironmentVariables]; }; diff --git a/packages/hoppscotch-cli/src/utils/request.ts b/packages/hoppscotch-cli/src/utils/request.ts index a80b8244..d769c5ac 100644 --- a/packages/hoppscotch-cli/src/utils/request.ts +++ b/packages/hoppscotch-cli/src/utils/request.ts @@ -42,8 +42,10 @@ const processVariables = (variable: Environment["variables"][number]) => { if (variable.secret) { return { ...variable, - value: - "value" in variable ? variable.value : process.env[variable.key] || "", + currentValue: + "currentValue" in variable && variable.currentValue !== "" + ? variable.currentValue + : process.env[variable.key] || "", }; } return variable; diff --git a/packages/hoppscotch-common/locales/en.json b/packages/hoppscotch-common/locales/en.json index 4b8cf526..677ba431 100644 --- a/packages/hoppscotch-common/locales/en.json +++ b/packages/hoppscotch-common/locales/en.json @@ -340,11 +340,13 @@ } }, "count": { + "currentValue": "Current value {count}", + "description": "Description {count}", "header": "Header {count}", + "initialValue": "Initial value {count}", + "key": "Key {count}", "message": "Message {count}", "parameter": "Parameter {count}", - "key": "Key {count}", - "description": "Description {count}", "protocol": "Protocol {count}", "value": "Value {count}", "variable": "Variable {count}" diff --git a/packages/hoppscotch-common/src/components/environments/Add.vue b/packages/hoppscotch-common/src/components/environments/Add.vue index 5eed9be6..71dedb1d 100644 --- a/packages/hoppscotch-common/src/components/environments/Add.vue +++ b/packages/hoppscotch-common/src/components/environments/Add.vue @@ -70,7 +70,6 @@