Skip to content

Commit fe5c07f

Browse files
fix: fallback env to initial and make valid url in codegen (hoppscotch#5214)
Co-authored-by: jamesgeorge007 <[email protected]>
1 parent bf3e135 commit fe5c07f

File tree

4 files changed

+37
-28
lines changed

4 files changed

+37
-28
lines changed

package.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,6 @@
5757
"[email protected]": "2.0.2",
5858
"[email protected]": "1.1.12"
5959
},
60-
"packageExtensions": {
61-
"@hoppscotch/httpsnippet": {
62-
"dependencies": {
63-
"ajv": "6.12.3"
64-
}
65-
}
66-
},
6760
"onlyBuiltDependencies": [
6861
"@apollo/protobufjs",
6962
"@import-meta-env/unplugin",

packages/hoppscotch-common/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"@codemirror/view": "6.25.1",
3737
"@hoppscotch/codemirror-lang-graphql": "workspace:^",
3838
"@hoppscotch/data": "workspace:^",
39-
"@hoppscotch/httpsnippet": "3.0.7",
39+
"@hoppscotch/httpsnippet": "3.0.9",
4040
"@hoppscotch/js-sandbox": "workspace:^",
4141
"@hoppscotch/kernel": "workspace:^",
4242
"@hoppscotch/plugin-appload": "github:CuriousCorrelation/tauri-plugin-appload#0308b55e82f7f01d878a7fdf0f597d1dc975f2ce",

packages/hoppscotch-common/src/components/http/Codegen.vue

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,35 @@ const getCurrentValue = (env: AggregateEnvironment) => {
202202
)?.currentValue
203203
}
204204
205+
const getFinalURL = (input: string): string => {
206+
// If the URL is empty, return "https://"
207+
// This is to ensure that the URL is always valid and can be used in code generation
208+
if (!input) {
209+
return "https://"
210+
}
211+
212+
let url = input.trim()
213+
214+
// Fix malformed protocols
215+
url = url.replace(/^https?:\s*\/+\s*/i, (match) =>
216+
match.toLowerCase().startsWith("https") ? "https://" : "http://"
217+
)
218+
219+
// If the URL does not start with http(s):// or is not a variable, prepend http(s)://
220+
// If the URL starts with <<, it is a variable and should not be modified
221+
if (!/^https?:\/\//i.test(url) && !url.startsWith("<<")) {
222+
const endpoint = url
223+
const domain = endpoint.split(/[/:#?]+/)[0]
224+
225+
// Check if the domain is a local address or an IP address
226+
// If it is, use http, otherwise use https
227+
const isLocalOrIP = /^(localhost|(\d{1,3}\.){3}\d{1,3})$/.test(domain)
228+
url = (isLocalOrIP ? "http://" : "https://") + endpoint
229+
}
230+
231+
return url
232+
}
233+
205234
const requestCode = asyncComputed(async () => {
206235
// Generate code snippet action only applies to request documents
207236
if (currentActiveTabDocument.value.type !== "request") {
@@ -230,7 +259,7 @@ const requestCode = asyncComputed(async () => {
230259
...(requestVariables as Environment["variables"]),
231260
...aggregateEnvs.map((env) => ({
232261
...env,
233-
currentValue: getCurrentValue(env) ?? env.currentValue,
262+
currentValue: getCurrentValue(env) || env.initialValue,
234263
})),
235264
],
236265
}
@@ -282,7 +311,7 @@ const requestCode = asyncComputed(async () => {
282311
...param,
283312
active: true,
284313
})),
285-
endpoint: effectiveRequest.effectiveFinalURL,
314+
endpoint: getFinalURL(effectiveRequest.effectiveFinalURL),
286315
requestVariables: effectiveRequest.effectiveFinalRequestVariables.map(
287316
(requestVariable) => ({
288317
...requestVariable,

pnpm-lock.yaml

Lines changed: 5 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)