Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

fix: fallback env to initial and make valid url in codegen (#5214)

Nivedinfe5c07faeddb
diff --git a/package.json b/package.json
index 45b094f16..06e878e6a 100644
--- a/package.json
+++ b/package.json
@@ -57,13 +57,6 @@
"[email protected]": "2.0.2",
"[email protected]": "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 20672166e..4d3b661dc 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 351739cf7..61ce60e82 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 cfbee0354..97abc47f8 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -27,8 +27,6 @@ overrides:
-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:
resolution: {integrity: sha512-/c6rf4UJlmHlC9b5BaNvzAcFv7HZ2QHaV0D4/HNlBdvFnvQq8RI4kYdhyPCl7Xj+oWvTWQ8ujhqS53LIgAe6KQ==}
- '@hoppscotch/[email protected]':
- resolution: {integrity: sha512-A7uoYLmkdjekFadnooOjpplFOIr0yHtY/wG63uoTbvL0hoEYH22fDQlJeYIlZe1kdv4q+xjnHj9VgdZQzMbauA==}
+ '@hoppscotch/[email protected]':
+ 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
- resolution: {integrity: sha512-4K0cK3L1hsqk9xIb2z9vs/XU+PGJZ9PNpJRDS9YLzmNdX6jmVPfamLvTJr0aDAusnHyCHO6MjzlkAsgtqp9teA==}
-
resolution: {integrity: sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==}
@@ -18489,9 +18484,8 @@ snapshots:
'@hapi/[email protected]': {}
- '@hoppscotch/[email protected]':
+ '@hoppscotch/[email protected]':
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
- 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
-
dependencies:
fast-deep-equal: 3.1.3