mirror of
https://github.com/usebruno/bruno.git
synced 2025-01-22 05:38:40 +01:00
feat: request vars, bru.getRequestVar function (#2541)
This commit is contained in:
parent
45ff36d394
commit
c895d7f357
@ -316,7 +316,7 @@ const registerNetworkIpc = (mainWindow) => {
|
||||
const preRequestVars = get(request, 'vars.req', []);
|
||||
if (preRequestVars?.length) {
|
||||
const varsRuntime = new VarsRuntime();
|
||||
const result = varsRuntime.runPreRequestVars(
|
||||
varsRuntime.runPreRequestVars(
|
||||
preRequestVars,
|
||||
request,
|
||||
envVars,
|
||||
@ -324,15 +324,6 @@ const registerNetworkIpc = (mainWindow) => {
|
||||
collectionPath,
|
||||
processEnvVars
|
||||
);
|
||||
|
||||
if (result) {
|
||||
mainWindow.webContents.send('main:script-environment-update', {
|
||||
envVariables: result.envVariables,
|
||||
collectionVariables: result.collectionVariables,
|
||||
requestUid,
|
||||
collectionUid
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// run pre-request script
|
||||
|
@ -13,6 +13,7 @@ const getContentType = (headers = {}) => {
|
||||
};
|
||||
|
||||
const interpolateVars = (request, envVars = {}, collectionVariables = {}, processEnvVars = {}) => {
|
||||
const requestVariables = request?.requestVariables || {};
|
||||
// we clone envVars because we don't want to modify the original object
|
||||
envVars = cloneDeep(envVars);
|
||||
|
||||
@ -36,6 +37,7 @@ const interpolateVars = (request, envVars = {}, collectionVariables = {}, proces
|
||||
// collectionVariables take precedence over envVars
|
||||
const combinedVars = {
|
||||
...envVars,
|
||||
...requestVariables,
|
||||
...collectionVariables,
|
||||
process: {
|
||||
env: {
|
||||
|
@ -122,7 +122,7 @@ const mergeFolderLevelVars = (request, requestTreePath) => {
|
||||
const mergeFolderLevelScripts = (request, requestTreePath) => {
|
||||
let folderCombinedPreReqScript = [];
|
||||
let folderCombinedPostResScript = [];
|
||||
let folderCombinedTests = '';
|
||||
let folderCombinedTests = [];
|
||||
for (let i of requestTreePath) {
|
||||
if (i.type === 'folder') {
|
||||
let preReqScript = get(i, 'root.request.script.req', '');
|
||||
@ -136,8 +136,8 @@ const mergeFolderLevelScripts = (request, requestTreePath) => {
|
||||
}
|
||||
|
||||
let tests = get(i, 'root.request.tests', '');
|
||||
if (tests?.trim?.() !== '') {
|
||||
folderCombinedTests = `${folderCombinedTests} \n ${tests} \n`;
|
||||
if (tests && tests?.trim?.() !== '') {
|
||||
folderCombinedTests.push(tests);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -151,7 +151,7 @@ const mergeFolderLevelScripts = (request, requestTreePath) => {
|
||||
}
|
||||
|
||||
if (folderCombinedTests.length) {
|
||||
request.tests = `${request?.tests} \n ${folderCombinedTests}`;
|
||||
request.tests = compact([request?.tests || '', ...folderCombinedTests.reverse()]).join(os.EOL);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -4,10 +4,11 @@ const { interpolate } = require('@usebruno/common');
|
||||
const variableNameRegex = /^[\w-.]*$/;
|
||||
|
||||
class Bru {
|
||||
constructor(envVariables, collectionVariables, processEnvVars, collectionPath) {
|
||||
constructor(envVariables, collectionVariables, processEnvVars, collectionPath, requestVariables) {
|
||||
this.envVariables = envVariables || {};
|
||||
this.collectionVariables = collectionVariables || {};
|
||||
this.processEnvVars = cloneDeep(processEnvVars || {});
|
||||
this.requestVariables = requestVariables || {};
|
||||
this.collectionPath = collectionPath;
|
||||
}
|
||||
|
||||
@ -18,6 +19,7 @@ class Bru {
|
||||
|
||||
const combinedVars = {
|
||||
...this.envVariables,
|
||||
...this.requestVariables,
|
||||
...this.collectionVariables,
|
||||
process: {
|
||||
env: {
|
||||
@ -79,6 +81,10 @@ class Bru {
|
||||
return this._interpolate(this.collectionVariables[key]);
|
||||
}
|
||||
|
||||
getRequestVar(key) {
|
||||
return this._interpolate(this.requestVariables[key]);
|
||||
}
|
||||
|
||||
setNextRequest(nextRequest) {
|
||||
this.nextRequest = nextRequest;
|
||||
}
|
||||
|
@ -1,12 +1,16 @@
|
||||
const { interpolate } = require('@usebruno/common');
|
||||
|
||||
const interpolateString = (str, { envVariables = {}, collectionVariables = {}, processEnvVars = {} }) => {
|
||||
const interpolateString = (
|
||||
str,
|
||||
{ envVariables = {}, collectionVariables = {}, processEnvVars = {}, requestVariables = {} }
|
||||
) => {
|
||||
if (!str || !str.length || typeof str !== 'string') {
|
||||
return str;
|
||||
}
|
||||
|
||||
const combinedVars = {
|
||||
...envVariables,
|
||||
...requestVariables,
|
||||
...collectionVariables,
|
||||
process: {
|
||||
env: {
|
||||
|
@ -167,6 +167,7 @@ const evaluateRhsOperand = (rhsOperand, operator, context) => {
|
||||
}
|
||||
|
||||
const interpolationContext = {
|
||||
requestVariables: context.bru.requestVariables,
|
||||
collectionVariables: context.bru.collectionVariables,
|
||||
envVariables: context.bru.envVariables,
|
||||
processEnvVars: context.bru.processEnvVars
|
||||
@ -204,12 +205,13 @@ const evaluateRhsOperand = (rhsOperand, operator, context) => {
|
||||
|
||||
class AssertRuntime {
|
||||
runAssertions(assertions, request, response, envVariables, collectionVariables, processEnvVars) {
|
||||
const requestVariables = request?.requestVariables || {};
|
||||
const enabledAssertions = _.filter(assertions, (a) => a.enabled);
|
||||
if (!enabledAssertions.length) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const bru = new Bru(envVariables, collectionVariables, processEnvVars);
|
||||
const bru = new Bru(envVariables, collectionVariables, processEnvVars, undefined, requestVariables);
|
||||
const req = new BrunoRequest(request);
|
||||
const res = createResponseParser(response);
|
||||
|
||||
@ -221,6 +223,7 @@ class AssertRuntime {
|
||||
|
||||
const context = {
|
||||
...envVariables,
|
||||
...requestVariables,
|
||||
...collectionVariables,
|
||||
...processEnvVars,
|
||||
...bruContext
|
||||
|
@ -44,7 +44,8 @@ class ScriptRuntime {
|
||||
processEnvVars,
|
||||
scriptingConfig
|
||||
) {
|
||||
const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath);
|
||||
const requestVariables = request?.requestVariables || {};
|
||||
const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath, requestVariables);
|
||||
const req = new BrunoRequest(request);
|
||||
const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
|
||||
const moduleWhitelist = get(scriptingConfig, 'moduleWhitelist', []);
|
||||
@ -141,7 +142,8 @@ class ScriptRuntime {
|
||||
processEnvVars,
|
||||
scriptingConfig
|
||||
) {
|
||||
const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath);
|
||||
const requestVariables = request?.requestVariables || {};
|
||||
const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath, requestVariables);
|
||||
const req = new BrunoRequest(request);
|
||||
const res = new BrunoResponse(response);
|
||||
const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
|
||||
|
@ -45,7 +45,8 @@ class TestRuntime {
|
||||
processEnvVars,
|
||||
scriptingConfig
|
||||
) {
|
||||
const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath);
|
||||
const requestVariables = request?.requestVariables || {};
|
||||
const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath, requestVariables);
|
||||
const req = new BrunoRequest(request);
|
||||
const res = new BrunoResponse(response);
|
||||
const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
|
||||
|
@ -5,6 +5,9 @@ const { evaluateJsTemplateLiteral, evaluateJsExpression, createResponseParser }
|
||||
|
||||
class VarsRuntime {
|
||||
runPreRequestVars(vars, request, envVariables, collectionVariables, collectionPath, processEnvVars) {
|
||||
if (!request?.requestVariables) {
|
||||
request.requestVariables = {};
|
||||
}
|
||||
const enabledVars = _.filter(vars, (v) => v.enabled);
|
||||
if (!enabledVars.length) {
|
||||
return;
|
||||
@ -26,21 +29,18 @@ class VarsRuntime {
|
||||
|
||||
_.each(enabledVars, (v) => {
|
||||
const value = evaluateJsTemplateLiteral(v.value, context);
|
||||
bru.setVar(v.name, value);
|
||||
request?.requestVariables && (request.requestVariables[v.name] = value);
|
||||
});
|
||||
|
||||
return {
|
||||
collectionVariables
|
||||
};
|
||||
}
|
||||
|
||||
runPostResponseVars(vars, request, response, envVariables, collectionVariables, collectionPath, processEnvVars) {
|
||||
const requestVariables = request?.requestVariables || {};
|
||||
const enabledVars = _.filter(vars, (v) => v.enabled);
|
||||
if (!enabledVars.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const bru = new Bru(envVariables, collectionVariables, processEnvVars);
|
||||
const bru = new Bru(envVariables, collectionVariables, processEnvVars, undefined, requestVariables);
|
||||
const req = new BrunoRequest(request);
|
||||
const res = createResponseParser(response);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user