forked from extern/bruno
feat: simple vars runtime is working!
This commit is contained in:
parent
580d681e0a
commit
e22f164cbc
@ -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",
|
||||
|
@ -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);
|
||||
|
@ -6,7 +6,12 @@ const {
|
||||
TestRuntime
|
||||
} = require('./test-runtime');
|
||||
|
||||
const {
|
||||
VarsRuntime
|
||||
} = require('./vars-runtime');
|
||||
|
||||
module.exports = {
|
||||
ScriptRuntime,
|
||||
TestRuntime
|
||||
TestRuntime,
|
||||
VarsRuntime
|
||||
};
|
||||
|
46
packages/bruno-js/src/vars-runtime.js
Normal file
46
packages/bruno-js/src/vars-runtime.js
Normal 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
|
||||
};
|
Loading…
Reference in New Issue
Block a user