From fe5c07faeddbb038f29f0adf5b1ae973ea888104 Mon Sep 17 00:00:00 2001 From: Nivedin <53208152+nivedin@users.noreply.github.com> Date: Thu, 10 Jul 2025 16:42:50 +0530 Subject: [PATCH] fix: fallback env to initial and make valid url in codegen (#5214) Co-authored-by: jamesgeorge007 <25279263+jamesgeorge007@users.noreply.github.com> --- package.json | 7 ---- packages/hoppscotch-common/package.json | 2 +- .../src/components/http/Codegen.vue | 33 +++++++++++++++++-- pnpm-lock.yaml | 23 +++---------- 4 files changed, 37 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 45b094f1..06e878e6 100644 --- a/package.json +++ b/package.json @@ -57,13 +57,6 @@ "brace-expansion@2.0.1": "2.0.2", "brace-expansion@1.1.11": "1.1.12" }, - "packageExtensions": { - "@hoppscotch/httpsnippet": { - "dependencies": { - "ajv": "6.12.3" - } - } - }, "onlyBuiltDependencies": [ "@apollo/protobufjs", "@import-meta-env/unplugin", diff --git a/packages/hoppscotch-common/package.json b/packages/hoppscotch-common/package.json index 20672166..4d3b661d 100644 --- a/packages/hoppscotch-common/package.json +++ b/packages/hoppscotch-common/package.json @@ -36,7 +36,7 @@ "@codemirror/view": "6.25.1", "@hoppscotch/codemirror-lang-graphql": "workspace:^", "@hoppscotch/data": "workspace:^", - "@hoppscotch/httpsnippet": "3.0.7", + "@hoppscotch/httpsnippet": "3.0.9", "@hoppscotch/js-sandbox": "workspace:^", "@hoppscotch/kernel": "workspace:^", "@hoppscotch/plugin-appload": "github:CuriousCorrelation/tauri-plugin-appload#0308b55e82f7f01d878a7fdf0f597d1dc975f2ce", diff --git a/packages/hoppscotch-common/src/components/http/Codegen.vue b/packages/hoppscotch-common/src/components/http/Codegen.vue index 351739cf..61ce60e8 100644 --- a/packages/hoppscotch-common/src/components/http/Codegen.vue +++ b/packages/hoppscotch-common/src/components/http/Codegen.vue @@ -202,6 +202,35 @@ const getCurrentValue = (env: AggregateEnvironment) => { )?.currentValue } +const getFinalURL = (input: string): string => { + // If the URL is empty, return "https://" + // This is to ensure that the URL is always valid and can be used in code generation + if (!input) { + return "https://" + } + + let url = input.trim() + + // Fix malformed protocols + url = url.replace(/^https?:\s*\/+\s*/i, (match) => + match.toLowerCase().startsWith("https") ? "https://" : "http://" + ) + + // If the URL does not start with http(s):// or is not a variable, prepend http(s):// + // If the URL starts with <<, it is a variable and should not be modified + if (!/^https?:\/\//i.test(url) && !url.startsWith("<<")) { + const endpoint = url + const domain = endpoint.split(/[/:#?]+/)[0] + + // Check if the domain is a local address or an IP address + // If it is, use http, otherwise use https + const isLocalOrIP = /^(localhost|(\d{1,3}\.){3}\d{1,3})$/.test(domain) + url = (isLocalOrIP ? "http://" : "https://") + endpoint + } + + return url +} + const requestCode = asyncComputed(async () => { // Generate code snippet action only applies to request documents if (currentActiveTabDocument.value.type !== "request") { @@ -230,7 +259,7 @@ const requestCode = asyncComputed(async () => { ...(requestVariables as Environment["variables"]), ...aggregateEnvs.map((env) => ({ ...env, - currentValue: getCurrentValue(env) ?? env.currentValue, + currentValue: getCurrentValue(env) || env.initialValue, })), ], } @@ -282,7 +311,7 @@ const requestCode = asyncComputed(async () => { ...param, active: true, })), - endpoint: effectiveRequest.effectiveFinalURL, + endpoint: getFinalURL(effectiveRequest.effectiveFinalURL), requestVariables: effectiveRequest.effectiveFinalRequestVariables.map( (requestVariable) => ({ ...requestVariable, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cfbee035..97abc47f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -27,8 +27,6 @@ overrides: brace-expansion@2.0.1: 2.0.2 brace-expansion@1.1.11: 1.1.12 -packageExtensionsChecksum: sha256-Qhsch/G1LLagBL1kRb8nf11C5HcyCWi8Px3h3uWxYUw= - importers: .: @@ -518,8 +516,8 @@ importers: specifier: workspace:^ version: link:../hoppscotch-data '@hoppscotch/httpsnippet': - specifier: 3.0.7 - version: 3.0.7 + specifier: 3.0.9 + version: 3.0.9 '@hoppscotch/js-sandbox': specifier: workspace:^ version: link:../hoppscotch-js-sandbox @@ -4557,8 +4555,8 @@ packages: '@hapi/hoek@9.3.0': resolution: {integrity: sha512-/c6rf4UJlmHlC9b5BaNvzAcFv7HZ2QHaV0D4/HNlBdvFnvQq8RI4kYdhyPCl7Xj+oWvTWQ8ujhqS53LIgAe6KQ==} - '@hoppscotch/httpsnippet@3.0.7': - resolution: {integrity: sha512-A7uoYLmkdjekFadnooOjpplFOIr0yHtY/wG63uoTbvL0hoEYH22fDQlJeYIlZe1kdv4q+xjnHj9VgdZQzMbauA==} + '@hoppscotch/httpsnippet@3.0.9': + resolution: {integrity: sha512-XTs2SYxOItC7Go38VsFYtnxEk6C5JhscEpZHgd9+klyah+Iy2uKLFaBFq9M/10YLhwfPNVP3UpGsL/jY50zQgQ==} engines: {node: '^14.19.1 || ^16.14.2 || ^18.0.0 '} hasBin: true @@ -7023,9 +7021,6 @@ packages: peerDependencies: ajv: ^8.8.2 - ajv@6.12.3: - resolution: {integrity: sha512-4K0cK3L1hsqk9xIb2z9vs/XU+PGJZ9PNpJRDS9YLzmNdX6jmVPfamLvTJr0aDAusnHyCHO6MjzlkAsgtqp9teA==} - ajv@6.12.6: resolution: {integrity: sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==} @@ -18489,9 +18484,8 @@ snapshots: '@hapi/hoek@9.3.0': {} - '@hoppscotch/httpsnippet@3.0.7': + '@hoppscotch/httpsnippet@3.0.9': dependencies: - ajv: 6.12.3 chalk: 4.1.2 event-stream: 4.0.1 form-data: 4.0.0 @@ -21608,13 +21602,6 @@ snapshots: ajv: 8.17.1 fast-deep-equal: 3.1.3 - ajv@6.12.3: - dependencies: - fast-deep-equal: 3.1.3 - fast-json-stable-stringify: 2.1.0 - json-schema-traverse: 0.4.1 - uri-js: 4.4.1 - ajv@6.12.6: dependencies: fast-deep-equal: 3.1.3