mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-22 07:53:34 +01:00
feat: scripting support (#16)
This commit is contained in:
parent
fa40685a6a
commit
1ec24d1138
@ -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"
|
||||
|
@ -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' ? (
|
||||
|
@ -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);
|
||||
|
@ -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": {
|
||||
|
@ -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) {
|
||||
|
12
packages/bruno-js/package.json
Normal file
12
packages/bruno-js/package.json
Normal 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"
|
||||
}
|
||||
}
|
7
packages/bruno-js/src/index.js
Normal file
7
packages/bruno-js/src/index.js
Normal file
@ -0,0 +1,7 @@
|
||||
const {
|
||||
ScriptRuntime
|
||||
} = require('./scripts/script-runtime');
|
||||
|
||||
module.exports = {
|
||||
ScriptRuntime
|
||||
};
|
47
packages/bruno-js/src/scripts/bruno-request.js
Normal file
47
packages/bruno-js/src/scripts/bruno-request.js
Normal 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;
|
26
packages/bruno-js/src/scripts/script-runtime.js
Normal file
26
packages/bruno-js/src/scripts/script-runtime.js
Normal 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
|
||||
};
|
Loading…
Reference in New Issue
Block a user