From c3236d4eb19ae47ce51f28d91ca667dbafa37ac0 Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Sun, 5 Feb 2023 01:25:36 +0530 Subject: [PATCH] feat: making changes in app to use the new bru lang format --- .../bruno-app/src/components/Tooltip/index.js | 2 +- packages/bruno-electron/src/app/watcher.js | 7 +- packages/bruno-electron/src/bru/index.js | 97 +++++++++++++++++++ packages/bruno-electron/src/ipc/collection.js | 7 +- packages/bruno-lang/package.json | 2 + packages/bruno-lang/src/index.js | 8 +- 6 files changed, 115 insertions(+), 8 deletions(-) create mode 100644 packages/bruno-electron/src/bru/index.js diff --git a/packages/bruno-app/src/components/Tooltip/index.js b/packages/bruno-app/src/components/Tooltip/index.js index 88f5e41f3..7abbaef9d 100644 --- a/packages/bruno-app/src/components/Tooltip/index.js +++ b/packages/bruno-app/src/components/Tooltip/index.js @@ -4,7 +4,7 @@ import { Tooltip as ReactTooltip } from 'react-tooltip'; const Tooltip = ({ text, tooltipId }) => { return ( <> - + diff --git a/packages/bruno-electron/src/app/watcher.js b/packages/bruno-electron/src/app/watcher.js index bb148b67d..d82f3f71f 100644 --- a/packages/bruno-electron/src/app/watcher.js +++ b/packages/bruno-electron/src/app/watcher.js @@ -4,11 +4,13 @@ const path = require('path'); const chokidar = require('chokidar'); const { hasJsonExtension, hasBruExtension, writeFile } = require('../utils/filesystem'); const { - bruToJson, - jsonToBru, bruToEnvJson, envJsonToBru, } = require('@usebruno/lang'); +const { + bruToJson, + jsonToBru +} = require('../bru'); const { itemSchema } = require('@usebruno/schema'); const { uuid } = require('../utils/common'); const { getRequestUid } = require('../cache/requestUids'); @@ -179,6 +181,7 @@ const add = async (win, pathname, collectionUid, collectionPath) => { try { const bru = fs.readFileSync(pathname, 'utf8'); file.data = bruToJson(bru); + console.log(JSON.stringify(file.data, null, 2)); hydrateRequestWithUuid(file.data, pathname); win.webContents.send('main:collection-tree-updated', 'addFile', file); } catch (err) { diff --git a/packages/bruno-electron/src/bru/index.js b/packages/bruno-electron/src/bru/index.js new file mode 100644 index 000000000..bbe52223e --- /dev/null +++ b/packages/bruno-electron/src/bru/index.js @@ -0,0 +1,97 @@ +const _ = require('lodash'); +const { + bruToJson: bruToJsonV1, + bruToJsonV2, + jsonToBruV2 +} = require('@usebruno/lang'); + +/** + * The transformer function for converting a BRU file to JSON. + * + * We map the json response from the bru lang and transform it into the DSL + * format that the app users + * + * @param {string} bru The BRU file content. + * @returns {object} The JSON representation of the BRU file. + */ +const bruToJson = (bru) => { + try { + const json = bruToJsonV2(bru); + + let requestType = _.get(json, "meta.type"); + if(requestType === "http") { + requestType = "http-request" + } else if(requestType === "graphql") { + requestType = "graphql-request"; + } else { + requestType = "http"; + } + + const sequence = _.get(json, "meta.seq") + + const transformedJson = { + "type": requestType, + "name": _.get(json, "meta.name"), + "seq": !isNaN(sequence) ? Number(sequence) : 1, + "request": { + "method": _.upperCase(_.get(json, "http.method")), + "url": _.get(json, "http.url"), + "params": _.get(json, "query", []), + "headers": _.get(json, "headers", []), + "body": _.get(json, "body", {}), + }, + "script": _.get(json, "script", ""), + "test": _.get(json, "test", "") + }; + + transformedJson.request.body.mode = _.get(json, "http.mode", "none"); + + return transformedJson; + } catch (e) { + return bruToJsonV1(bru); + } +}; +/** + * The transformer function for converting a JSON to BRU file. + * + * We map the json response from the app and transform it into the DSL + * format that the bru lang understands + * + * @param {object} json The JSON representation of the BRU file. + * @returns {string} The BRU file content. + */ +const jsonToBru = (json) => { + let type = _.get(json, 'type'); + if (type === 'http-request') { + type = "http"; + } else if (type === 'graphql-request') { + type = "graphql"; + } else { + type = "http"; + } + + const bruJson = { + meta: { + name: _.get(json, 'name'), + type: type, + seq: _.get(json, 'seq'), + }, + http: { + method: _.lowerCase(_.get(json, 'request.method')), + url: _.get(json, 'request.url'), + mode: _.get(json, 'request.body.mode', 'none') + }, + query: _.get(json, 'request.params', []), + headers: _.get(json, 'request.headers', []), + body: _.get(json, 'request.body', {}), + script: _.get(json, 'script', ''), + test: _.get(json, 'test', ''), + }; + + return jsonToBruV2(bruJson); +}; + +module.exports = { + bruToJson, + jsonToBru +}; diff --git a/packages/bruno-electron/src/ipc/collection.js b/packages/bruno-electron/src/ipc/collection.js index 30706a7e8..4df6100ba 100644 --- a/packages/bruno-electron/src/ipc/collection.js +++ b/packages/bruno-electron/src/ipc/collection.js @@ -3,10 +3,13 @@ const fs = require('fs'); const path = require('path'); const { ipcMain } = require('electron'); const { - jsonToBru, - bruToJson, envJsonToBru, } = require('@usebruno/lang'); +const { + bruToJson, + jsonToBru +} = require('../bru'); + const { isValidPathname, writeFile, diff --git a/packages/bruno-lang/package.json b/packages/bruno-lang/package.json index 632aed4c5..d606657a6 100644 --- a/packages/bruno-lang/package.json +++ b/packages/bruno-lang/package.json @@ -4,6 +4,8 @@ "main": "src/index.js", "files": [ "src", + "v1", + "v2", "package.json" ], "scripts": { diff --git a/packages/bruno-lang/src/index.js b/packages/bruno-lang/src/index.js index bea612a85..36a3375ec 100644 --- a/packages/bruno-lang/src/index.js +++ b/packages/bruno-lang/src/index.js @@ -3,9 +3,10 @@ const { jsonToBru, bruToEnvJson, envJsonToBru -} = require('./v1/src'); +} = require('../v1/src'); -const bruToJsonV2 = require('./v2/src/bruToJson'); +const bruToJsonV2 = require('../v2/src/bruToJson'); +const jsonToBruV2 = require('../v2/src/jsonToBru'); module.exports = { bruToJson, @@ -13,5 +14,6 @@ module.exports = { bruToEnvJson, envJsonToBru, - bruToJsonV2 + bruToJsonV2, + jsonToBruV2 }; \ No newline at end of file