mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-22 16:03:39 +01:00
feat: support loading external libraries
This commit is contained in:
parent
905f459ed0
commit
d89f12c071
@ -110,7 +110,7 @@ export const sendRequest = (item, collectionUid) => (dispatch, getState) => {
|
||||
|
||||
const environment = findEnvironmentInCollection(collectionCopy, collection.activeEnvironmentUid);
|
||||
|
||||
sendNetworkRequest(itemCopy, collectionUid, environment)
|
||||
sendNetworkRequest(itemCopy, collection, environment)
|
||||
.then((response) => {
|
||||
return dispatch(
|
||||
responseReceived({
|
||||
|
@ -1,8 +1,8 @@
|
||||
export const sendNetworkRequest = async (item, collectionUid, environment) => {
|
||||
export const sendNetworkRequest = async (item, collection, environment) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (['http-request', 'graphql-request'].includes(item.type)) {
|
||||
const timeStart = Date.now();
|
||||
sendHttpRequest(item, collectionUid, environment)
|
||||
sendHttpRequest(item, collection, environment)
|
||||
.then((response) => {
|
||||
const timeEnd = Date.now();
|
||||
resolve({
|
||||
@ -20,12 +20,12 @@ export const sendNetworkRequest = async (item, collectionUid, environment) => {
|
||||
});
|
||||
};
|
||||
|
||||
const sendHttpRequest = async (item, collectionUid, environment) => {
|
||||
const sendHttpRequest = async (item, collection, environment) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const { ipcRenderer } = window;
|
||||
|
||||
ipcRenderer
|
||||
.invoke('send-http-request', item, collectionUid, environment)
|
||||
.invoke('send-http-request', item, collection.uid, collection.pathname, environment)
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
});
|
||||
|
@ -32,7 +32,7 @@ const getEnvVars = (environment = {}) => {
|
||||
|
||||
const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
|
||||
// handler for sending http request
|
||||
ipcMain.handle('send-http-request', async (event, item, collectionUid, environment) => {
|
||||
ipcMain.handle('send-http-request', async (event, item, collectionUid, collectionPath, environment) => {
|
||||
const cancelTokenUid = uuid();
|
||||
|
||||
try {
|
||||
@ -59,7 +59,7 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
|
||||
if(request.script && request.script.length) {
|
||||
let script = request.script + '\n if (typeof onRequest === "function") {onRequest(brunoRequest);}';
|
||||
const scriptRuntime = new ScriptRuntime();
|
||||
const res = scriptRuntime.runRequestScript(script, request, envVars);
|
||||
const res = scriptRuntime.runRequestScript(script, request, envVars, collectionPath);
|
||||
|
||||
mainWindow.webContents.send('main:script-environment-update', {
|
||||
environment: res.environment,
|
||||
@ -67,6 +67,7 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
|
||||
});
|
||||
}
|
||||
|
||||
interpolateVars(request, envVars);
|
||||
mainWindow.webContents.send('main:http-request-sent', {
|
||||
requestSent: {
|
||||
url: request.url,
|
||||
@ -79,14 +80,12 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
|
||||
cancelTokenUid
|
||||
});
|
||||
|
||||
interpolateVars(request, envVars);
|
||||
|
||||
const result = await axios(request);
|
||||
|
||||
if(request.script && request.script.length) {
|
||||
let script = request.script + '\n if (typeof onResponse === "function") {onResponse(brunoResponse);}';
|
||||
const scriptRuntime = new ScriptRuntime();
|
||||
const res = scriptRuntime.runResponseScript(script, result, envVars);
|
||||
const res = scriptRuntime.runResponseScript(script, result, envVars, collectionPath);
|
||||
|
||||
mainWindow.webContents.send('main:script-environment-update', {
|
||||
environment: res.environment,
|
||||
|
@ -1,4 +1,5 @@
|
||||
const { NodeVM } = require('vm2');
|
||||
const path = require('path');
|
||||
const Bru = require('./bru');
|
||||
const BrunoRequest = require('./bruno-request');
|
||||
const BrunoResponse = require('./bruno-response');
|
||||
@ -7,7 +8,7 @@ class ScriptRuntime {
|
||||
constructor() {
|
||||
}
|
||||
|
||||
runRequestScript(script, request, environment) {
|
||||
runRequestScript(script, request, environment, collectionPath) {
|
||||
const bru = new Bru(environment);
|
||||
const brunoRequest = new BrunoRequest(request);
|
||||
|
||||
@ -16,10 +17,15 @@ class ScriptRuntime {
|
||||
brunoRequest
|
||||
};
|
||||
const vm = new NodeVM({
|
||||
sandbox: context
|
||||
sandbox: context,
|
||||
require: {
|
||||
context: 'sandbox',
|
||||
external: true,
|
||||
root: [collectionPath]
|
||||
}
|
||||
});
|
||||
|
||||
vm.run(script);
|
||||
vm.run(script, path.join(collectionPath, 'vm.js'));
|
||||
|
||||
return {
|
||||
request,
|
||||
@ -27,7 +33,7 @@ class ScriptRuntime {
|
||||
};
|
||||
}
|
||||
|
||||
runResponseScript(script, response, environment) {
|
||||
runResponseScript(script, response, environment, collectionPath) {
|
||||
const bru = new Bru(environment);
|
||||
const brunoResponse = new BrunoResponse(response);
|
||||
|
||||
@ -36,10 +42,15 @@ class ScriptRuntime {
|
||||
brunoResponse
|
||||
};
|
||||
const vm = new NodeVM({
|
||||
sandbox: context
|
||||
sandbox: context,
|
||||
require: {
|
||||
context: 'sandbox',
|
||||
external: true,
|
||||
root: [collectionPath]
|
||||
}
|
||||
});
|
||||
|
||||
vm.run(script);
|
||||
vm.run(script, path.join(collectionPath, 'vm.js'));
|
||||
|
||||
return {
|
||||
response,
|
||||
|
Loading…
Reference in New Issue
Block a user