Skip to content

feat: enhance script execution with environment variable support - #4209

Open
ajaaypranav-bt wants to merge 1 commit into
sparrowapp-dev:mainfrom
ajaaypranav-bt:feat/post-script-environment-variables
Open

feat: enhance script execution with environment variable support#4209
ajaaypranav-bt wants to merge 1 commit into
sparrowapp-dev:mainfrom
ajaaypranav-bt:feat/post-script-environment-variables

Conversation

@ajaaypranav-bt

@ajaaypranav-bt ajaaypranav-bt commented Jul 13, 2026

Copy link
Copy Markdown

Description

sp.global.set / sp.environment.set were only available in pre-request scripts. This PR adds the full variable API — sp.global.set/get and sp.environment.set/get — to post-response (post-processing) scripts, in both the desktop and web apps, for REST requests as well as Testflows.

Changes:

  • test-script-worker.ts (desktop + web): the post-script sandbox now receives the active environment variables and exposes sp.environment (type E) and sp.global (type G) with the same set/get implementation as the pre-script worker, returning the mutated env to the caller.
  • Persistence (RestExplorerPage.ViewModel.ts + TestflowExplorerPage.ViewModel.ts, desktop + web): unlike pre-request scripts (where set only affects the in-flight request's variable substitution), a set in post-processing would be a no-op without persistence. A new persistScriptEnvironmentChanges helper diffs the env returned by the script against the snapshot sent to it, then upserts changed keys into the workspace's global environment (G) or the currently selected environment (E) — RxDB only for guest users, environmentService.updateEnvironment + repository/tab sync for logged-in users, mirroring the existing updateEnvironment flow (without toast notifications).
  • Testflow chaining: during a flow run, the mutated env from each node's post-script is carried into subsequent nodes, so e.g. a token extracted from a login response in node 1 is usable as {{token}} in node 2 within the same run.
  • Editor autocomplete (@sparrow-library editor.ts): the post-script editor now completes sp.environment. / sp.global.set/get.
  • Snippets: added "Set/Get Global Variable" and "Set/Get Environment Variable" entries to the post-script snippet lists (rest-explorer + testflow-explorer).

Example (post-response script):

const token = sp.response.body.json().token;
sp.global.set("token", token);
sp.environment.set("sessionId", sp.response.headers["x-session-id"]);

Add Issue Number

Fixes #<your_issue_number>

Add Screenshots/GIFs

N/A

Add Known Issue

  • If no environment is selected in the workspace, sp.environment.set changes have nowhere to persist and are dropped silently.
  • Pre-request script behavior is unchanged: its set calls still only affect the outgoing request's variable substitution and are not persisted.

Contribution Checklist:

  • The pull request only addresses one issue or adds one feature.
  • I have linked an issue to the pull request.
  • I have linked a PR type label to the pull request.
  • The pull request does not introduce any breaking changes
  • I have added screenshots or GIFs to help explain the change if applicable.
  • I have read the contribution guidelines.

@ajaaypranav-bt
ajaaypranav-bt requested a review from gc-codes as a code owner July 13, 2026 20:48
@dev-vivid

Copy link
Copy Markdown

Hi Team,
This feature will be much needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants