feat: scripting support (#16)

This commit is contained in:
Anoop M D 2023-01-24 18:27:47 +05:30
parent fa40685a6a
commit 1ec24d1138
9 changed files with 107 additions and 3 deletions

View File

@ -6,6 +6,7 @@
"packages/bruno-electron",
"packages/bruno-tauri",
"packages/bruno-schema",
"packages/bruno-js",
"packages/bruno-lang",
"packages/bruno-testbench",
"packages/bruno-graphql-docs"

View File

@ -75,12 +75,12 @@ const HttpRequestPane = ({ item, collection, leftPaneWidth }) => {
<div className={getTabClassname('headers')} role="tab" onClick={() => selectTab('headers')}>
Headers
</div>
{/* <div className={getTabClassname('script')} role="tab" onClick={() => selectTab('script')}>
<div className={getTabClassname('script')} role="tab" onClick={() => selectTab('script')}>
Script
</div>
<div className={getTabClassname('tests')} role="tab" onClick={() => selectTab('tests')}>
Tests
</div> */}
</div>
{/* Moved to post mvp */}
{/* <div className={getTabClassname('auth')} role="tab" onClick={() => selectTab('auth')}>Auth</div> */}
{focusedTab.requestPaneTab === 'body' ? (

View File

@ -86,7 +86,9 @@ const sendHttpRequest = async (request, options, onRequestSent) => {
axiosRequest.data = graphqlQuery;
}
console.log('>>> Sending Request');
if (request.script && request.script.length) {
axiosRequest.script = request.script;
}
console.log(axiosRequest);
onRequestSent(axiosRequest);

View File

@ -13,6 +13,7 @@
"pack-app": "electron-builder --dir"
},
"dependencies": {
"@usebruno/js": "0.1.0",
"@usebruno/lang": "0.1.0",
"@usebruno/schema": "0.1.0",
"axios": "^0.26.0",
@ -25,6 +26,7 @@
"is-valid-path": "^0.1.1",
"lodash": "^4.17.21",
"nanoid": "3.3.4",
"vm2": "^3.9.13",
"yup": "^0.32.11"
},
"optionalDependencies": {

View File

@ -2,6 +2,7 @@ const axios = require('axios');
const FormData = require('form-data');
const { ipcMain } = require('electron');
const { forOwn, extend } = require('lodash');
const { ScriptRuntime } = require('@usebruno/js');
const { cancelTokens, saveCancelToken, deleteCancelToken } = require('../utils/cancel-token');
const registerNetworkIpc = () => {
@ -25,6 +26,12 @@ const registerNetworkIpc = () => {
saveCancelToken(options.cancelTokenUid, cancelToken);
}
if(request.script && request.script.length) {
request.script = request.script += '\n onRequest(brunoRequest);';
const scriptRuntime = new ScriptRuntime();
scriptRuntime.run(request.script, request);
}
const result = await axios(request);
if(options && options.cancelTokenUid) {

View File

@ -0,0 +1,12 @@
{
"name": "@usebruno/js",
"version": "0.1.0",
"main": "src/index.js",
"files": [
"src",
"package.json"
],
"peerDependencies": {
"vm2": "^3.9.13"
}
}

View File

@ -0,0 +1,7 @@
const {
ScriptRuntime
} = require('./scripts/script-runtime');
module.exports = {
ScriptRuntime
};

View File

@ -0,0 +1,47 @@
class BrunoRequest {
constructor(request) {
this._request = request;
}
getUrl() {
return this._request.url;
}
setUrl(url) {
this._request.url = url;
}
getMethod() {
return this._request.method;
}
setMethod(method) {
this._request.method = method;
}
getHeaders() {
return this._request.headers;
}
setHeaders(headers) {
this._request.headers = headers;
}
getHeader(name) {
return this._request.headers[name];
}
setHeader(name, value) {
this._request.headers[name] = value;
}
getData() {
return this._request.data;
}
setData(data) {
this._request.data = data;
}
}
module.exports = BrunoRequest;

View File

@ -0,0 +1,26 @@
const { NodeVM } = require('vm2');
const BrunoRequest = require('./bruno-request');
class ScriptRuntime {
constructor() {
}
run(script, request) {
const brunoRequest = new BrunoRequest(request);
const context = {
brunoRequest
};
const vm = new NodeVM({
sandbox: context
});
vm.run(script);
return request;
}
}
module.exports = {
ScriptRuntime
};