feat: simple vars runtime is working!

This commit is contained in:
Anoop M D 2023-02-07 04:33:25 +05:30
parent 580d681e0a
commit e22f164cbc
4 changed files with 58 additions and 3 deletions

View File

@ -12,6 +12,7 @@
"dependencies": {
"@usebruno/js": "0.1.0",
"@usebruno/lang": "0.1.0",
"axios": "^1.3.2",
"chalk": "^3.0.0",
"fs-extra": "^10.1.0",
"inquirer": "^9.1.4",

View File

@ -5,7 +5,7 @@ const FormData = require('form-data');
const path = require('path');
const axios = require('axios');
const prepareRequest = require('./prepare-request');
const { ScriptRuntime, TestRuntime } = require('@usebruno/js');
const { ScriptRuntime, TestRuntime, VarsRuntime } = require('@usebruno/js');
const {
bruToJson
} = require('./bru');
@ -63,7 +63,10 @@ const runSingleRequest = async function (filename, collectionPath, collectionVar
const response = await axios(request);
const responseScriptFile = get(bruJson, 'request.script.req');
const varsRuntime = new VarsRuntime();
varsRuntime.runResponseVars(bruJson.request.vars.res, request, response, envVars, collectionVariables);
const responseScriptFile = get(bruJson, 'request.script.res');
if(responseScriptFile && responseScriptFile.length) {
const scriptRuntime = new ScriptRuntime();
const result = scriptRuntime.runResponseScript(responseScriptFile, response, envVars, collectionVariables, collectionPath);

View File

@ -6,7 +6,12 @@ const {
TestRuntime
} = require('./test-runtime');
const {
VarsRuntime
} = require('./vars-runtime');
module.exports = {
ScriptRuntime,
TestRuntime
TestRuntime,
VarsRuntime
};

View File

@ -0,0 +1,46 @@
const Bru = require('./bru');
const BrunoRequest = require('./bruno-request');
const BrunoResponse = require('./bruno-response');
const _ = require('lodash');
const jsonQuery = require('json-query');
class VarsRuntime {
constructor() {
}
runResponseVars(vars, request, response, environment, collectionVariables, collectionPath) {
const bru = new Bru(environment, collectionVariables);
const req = new BrunoRequest(request);
const res = new BrunoResponse(response);
res.q = function(expr) {
const output = jsonQuery(expr, {data: res.body});
return output.value;
}
const result = {
};
const context = {
bru,
req,
res
}
console.log(JSON.stringify(vars, null, 2));
_.each(vars, (v) => {
result[v.name] = eval(v.value, context);
});
console.log(result);
}
}
module.exports = {
VarsRuntime
};