Skip to content

Commit ae3d73b

Browse files
fix(js-sandbox): improve scripting value handling and serialization
- Fix null/undefined environment variable handling across namespaces - Fix pm.request console.log output to display properly - Add pm.request.id and pm.request.name type definitions - Fix assertion error messages to show actual values - Strip `export {};` from collection exports and legacy sandbox editor display
1 parent c0e3ff4 commit ae3d73b

File tree

8 files changed

+511
-12
lines changed

8 files changed

+511
-12
lines changed

packages/hoppscotch-common/src/components/collections/index.vue

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,9 +2831,12 @@ const exportData = async (collection: HoppCollection | TeamCollection) => {
28312831
if (collectionsType.value.type === "my-collections") {
28322832
const collectionJSON = JSON.stringify(collection, null, 2)
28332833
2834+
// Strip `export {};\n` from `testScript` and `preRequestScript` fields
2835+
const cleanedCollectionJSON = collectionJSON.replace(/export \{\};\\n/g, "")
2836+
28342837
const name = (collection as HoppCollection).name
28352838
2836-
initializeDownloadCollection(collectionJSON, name)
2839+
initializeDownloadCollection(cleanedCollectionJSON, name)
28372840
} else {
28382841
if (!collection.id) return
28392842
exportLoading.value = true
@@ -2850,8 +2853,14 @@ const exportData = async (collection: HoppCollection | TeamCollection) => {
28502853
const hoppColl = teamCollToHoppRESTColl(coll)
28512854
const collectionJSONString = JSON.stringify(hoppColl, null, 2)
28522855
2856+
// Strip `export {};\n` from `testScript` and `preRequestScript` fields
2857+
const cleanedCollectionJSON = collectionJSONString.replace(
2858+
/export \{\};\\n/g,
2859+
""
2860+
)
2861+
28532862
await initializeDownloadCollection(
2854-
collectionJSONString,
2863+
cleanedCollectionJSON,
28552864
hoppColl.name
28562865
)
28572866
exportLoading.value = false

packages/hoppscotch-common/src/composables/codemirror.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,19 @@ const getEditorLanguage = (
268268
completer: Completer | undefined
269269
): Extension => hoppLang(getLanguage(langMime) ?? undefined, linter, completer)
270270

271+
const MODULE_PREFIX = "export {};\n" as const
272+
273+
/**
274+
* Strips the `export {};\n` prefix from the value for display in the editor.
275+
* The above is only used internally for Monaco editor's module scope,
276+
* and should not be visible in the CodeMirror editor.
277+
*/
278+
const stripModulePrefix = (value?: string): string | undefined => {
279+
return value?.startsWith(MODULE_PREFIX)
280+
? value.slice(MODULE_PREFIX.length)
281+
: value
282+
}
283+
271284
export function useCodemirror(
272285
el: Ref<any | null>,
273286
value: Ref<string | undefined>,
@@ -474,7 +487,10 @@ export function useCodemirror(
474487
view.value = new EditorView({
475488
parent: el,
476489
state: EditorState.create({
477-
doc: parseDoc(value.value, options.extendedEditorConfig.mode ?? ""),
490+
doc: parseDoc(
491+
stripModulePrefix(value.value),
492+
options.extendedEditorConfig.mode ?? ""
493+
),
478494
extensions,
479495
}),
480496
// scroll to top when mounting
@@ -514,13 +530,17 @@ export function useCodemirror(
514530
if (!view.value && el.value) {
515531
initView(el.value)
516532
}
533+
534+
// Strip `export {};\n` before displaying in CodeMirror
535+
const displayValue = stripModulePrefix(newVal) ?? ""
536+
517537
if (cachedValue.value !== newVal) {
518538
view.value?.dispatch({
519539
filter: false,
520540
changes: {
521541
from: 0,
522542
to: view.value.state.doc.length,
523-
insert: newVal,
543+
insert: displayValue,
524544
},
525545
})
526546
}

packages/hoppscotch-common/src/types/post-request.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,8 @@ declare namespace pm {
652652
}>
653653

654654
const request: Readonly<{
655+
readonly id: string
656+
readonly name: string
655657
readonly url: Readonly<{
656658
toString(): string
657659
readonly protocol: string

packages/hoppscotch-js-sandbox/src/__tests__/combined/env-fallback-behavior.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,8 @@ describe("Environment Variable Fallback Behavior - All Namespaces", () => {
106106
"$name - currentValue undefined/null fallback",
107107
({ get }) => {
108108
// Note: These tests check GET behavior with null/undefined in initial state.
109-
// Setting null via pm.environment.set(key, null) is currently NOT supported.
110-
// These tests validate fallback logic when null/undefined exists in the env state,
111-
// which can occur through other means (e.g., test setup, external sources).
109+
// Setting null via pm.environment.set(key, null) IS supported (via NULL_MARKER).
110+
// These tests validate fallback logic when null/undefined exists in the env state.
112111

113112
test("should fallback to initialValue when currentValue is undefined", async () => {
114113
const envs = {

0 commit comments

Comments
 (0)