Skip to content

Commit

Permalink
handle updating active environment from request scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Aug 23, 2023
1 parent a07b98f commit 7f89059
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class ScriptEvaluator {
async executeScript(
script: string,
data: ScriptContextData,
handlers: ScriptEventHandlers
userAvailableHandlers: ScriptEventHandlers
): Promise<ScriptContextData> {
try {
const worker = this.getWorker();
Expand All @@ -37,7 +37,7 @@ export class ScriptEvaluator {
}, this.timeout);

const allHandlers: AllScriptEventHandlers = {
...handlers,
...userAvailableHandlers,
executeComplete: (data: ScriptContextData) => {
clearTimeout(handle);
resolve(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ const initExecute = async (
}
});

if (res.__toSetActiveEnvironment) {
// update active environment
makeCall('updateActiveEnvironment', res.__toSetActiveEnvironment);
}
makeCall('executeComplete', res);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface ScriptEventHandlers {
) => Promise<void>;
getStorageItem: (key: string) => Promise<unknown>;
setStorageItem: (key: string, value: unknown) => Promise<void>;
updateActiveEnvironment: (environmentData: Record<string, unknown>) => Promise<void>;
}

// Extended event handler interface to include internal native events like scriptError as well
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ export class PreRequestService {
setStorageItem: (key: string, value: unknown) => {
return this.setStorageItem(key, value);
},
updateActiveEnvironment: (environmentData: Record<string, unknown>) => {
return this.updateActiveEnvironment(environmentData);
},
});
debug.debug('script result:', res);

Expand Down Expand Up @@ -158,48 +161,9 @@ export class PreRequestService {
throw new RequestScriptError(error);
}
if (clonedMutableData.__toSetActiveEnvironment) {
const activeEnvState = await this.store
.select(getActiveSubEnvironmentState)
.pipe(take(1))
.toPromise();

if (activeEnvState) {
try {
const envVariables = {
...JSON.parse(activeEnvState.variablesJson),
...clonedMutableData.__toSetActiveEnvironment,
};
const activeEnvStateId = activeEnvState.id;

if (!activeEnvStateId) {
throw new RequestScriptError('Invalid active environment state ID');
}

this.store.dispatch(
new environmentsActions.UpdateSubEnvironmentJsonAction({
id: activeEnvStateId,
value: JSON.stringify(envVariables, null, 2),
})
);
this.notifyService.info(
`Updated active environment variables: ${Object.keys(
clonedMutableData.__toSetActiveEnvironment
).join(', ')}.`,
'Request script'
);
} catch (error) {
this.notifyService.errorWithError(
error,
`Could not update active environment variables.`,
'Request script'
);
}
} else {
this.notifyService.warning(
'No active environment selected. Cannot update environment variables',
'Request script'
);
}
await this.updateActiveEnvironment(
clonedMutableData.__toSetActiveEnvironment
);
}
return clonedMutableData;
}
Expand All @@ -215,4 +179,50 @@ export class PreRequestService {
.pipe(take(1))
.toPromise();
}
private async updateActiveEnvironment(
environmentData: Record<string, unknown>
) {
const activeEnvState = await this.store
.select(getActiveSubEnvironmentState)
.pipe(take(1))
.toPromise();

if (activeEnvState) {
try {
const envVariables = {
...JSON.parse(activeEnvState.variablesJson),
...environmentData,
};
const activeEnvStateId = activeEnvState.id;

if (!activeEnvStateId) {
throw new RequestScriptError('Invalid active environment state ID');
}

this.store.dispatch(
new environmentsActions.UpdateSubEnvironmentJsonAction({
id: activeEnvStateId,
value: JSON.stringify(envVariables, null, 2),
})
);
this.notifyService.info(
`Updated active environment variables: ${Object.keys(
environmentData
).join(', ')}.`,
'Request script'
);
} catch (error) {
this.notifyService.errorWithError(
error,
`Could not update active environment variables.`,
'Request script'
);
}
} else {
this.notifyService.warning(
'No active environment selected. Cannot update environment variables',
'Request script'
);
}
}
}
3 changes: 3 additions & 0 deletions site/src/docs/features/settings-pane.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ Valid plugins in string format include: `altair-graphql-plugin-some-plugin`, `np
### `editor.shortcuts` - Contains shortcut to action mapping
_Default: {}_
You can add new editor shortcut mapping following the [CodeMirror key map pattern](https://codemirror.net/doc/manual.html#keymaps). For example, to add a new shortcut to toggle comments, you can add `{ "Ctrl-7": "toggleComment" }`. There are several editor actions you can add shortcuts for including: `showAutocomplete`, `toggleComment`, `showFinder`, `showInDocs`, `fillAllFields`, etc. If you want to disable an in-built shortcut, you can use the `noOp` action. For example to disable `Ctrl-/` from toggling comments, you can use `{ "Ctrl-/": "noOp" }`.

### `script.allowedCookies` - List of cookies to be accessible in the pre-request script
_Default: []_

0 comments on commit 7f89059

Please sign in to comment.