From 8e0bc68adac4999b88f9bc088202b50a086902c5 Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Thu, 20 Apr 2023 10:51:29 +0530 Subject: [PATCH] feat: support node-fetch as an inbuilt library (#138) --- packages/bruno-electron/src/ipc/network/index.js | 6 +++--- packages/bruno-js/package.json | 2 ++ packages/bruno-js/src/runtime/script-runtime.js | 9 +++++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index 2468aa972..e65f67373 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -158,7 +158,7 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => { const responseScript = get(request, 'script.res'); if(responseScript && responseScript.length) { const scriptRuntime = new ScriptRuntime(); - const result = scriptRuntime.runResponseScript(responseScript, request, response, envVars, collectionVariables, collectionPath); + const result = await scriptRuntime.runResponseScript(responseScript, request, response, envVars, collectionVariables, collectionPath); mainWindow.webContents.send('main:script-environment-update', { envVariables: result.envVariables, @@ -333,7 +333,7 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => { const requestScript = get(request, 'script.req'); if(requestScript && requestScript.length) { const scriptRuntime = new ScriptRuntime(); - const result = scriptRuntime.runRequestScript(requestScript, request, envVars, collectionVariables, collectionPath); + const result = await scriptRuntime.runRequestScript(requestScript, request, envVars, collectionVariables, collectionPath); mainWindow.webContents.send('main:script-environment-update', { envVariables: result.envVariables, @@ -381,7 +381,7 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => { const responseScript = get(request, 'script.res'); if(responseScript && responseScript.length) { const scriptRuntime = new ScriptRuntime(); - const result = scriptRuntime.runResponseScript(responseScript, request, response, envVars, collectionVariables, collectionPath); + const result = await scriptRuntime.runResponseScript(responseScript, request, response, envVars, collectionVariables, collectionPath); mainWindow.webContents.send('main:script-environment-update', { envVariables: result.envVariables, diff --git a/packages/bruno-js/package.json b/packages/bruno-js/package.json index ae51884b1..a9a3a708a 100644 --- a/packages/bruno-js/package.json +++ b/packages/bruno-js/package.json @@ -16,12 +16,14 @@ "@usebruno/query": "0.1.0", "ajv": "^8.12.0", "atob": "^2.1.2", + "axios": "^0.26.0", "btoa": "^1.2.1", "crypto-js": "^4.1.1", "json-query": "^2.2.2", "lodash": "^4.17.21", "moment": "^2.29.4", "nanoid": "3.3.4", + "node-fetch": "2.*", "uuid": "^9.0.0" } } diff --git a/packages/bruno-js/src/runtime/script-runtime.js b/packages/bruno-js/src/runtime/script-runtime.js index ceef3e240..30ae3e7a1 100644 --- a/packages/bruno-js/src/runtime/script-runtime.js +++ b/packages/bruno-js/src/runtime/script-runtime.js @@ -19,6 +19,7 @@ const moment = require('moment'); const uuid = require('uuid'); const nanoid = require('nanoid'); const axios = require('axios'); +const fetch = require('node-fetch'); const CryptoJS = require('crypto-js'); class ScriptRuntime { @@ -56,6 +57,7 @@ class ScriptRuntime { uuid, nanoid, axios, + 'node-fetch': fetch, 'crypto-js': CryptoJS } } @@ -69,7 +71,7 @@ class ScriptRuntime { }; } - runResponseScript(script, request, response, envVariables, collectionVariables, collectionPath) { + async runResponseScript(script, request, response, envVariables, collectionVariables, collectionPath) { const bru = new Bru(envVariables, collectionVariables); const req = new BrunoRequest(request); const res = new BrunoResponse(response); @@ -92,12 +94,15 @@ class ScriptRuntime { moment, uuid, nanoid, + axios, + 'node-fetch': fetch, 'crypto-js': CryptoJS } } }); - vm.run(script, path.join(collectionPath, 'vm.js')); + const asyncVM = vm.run(`module.exports = async () => { ${script} }`, path.join(collectionPath, 'vm.js')); + await asyncVM(); return { response,