feat: making changes in app to use the new bru lang format

This commit is contained in:
Anoop M D 2023-02-05 01:25:36 +05:30
parent 4a4208f272
commit c3236d4eb1
6 changed files with 115 additions and 8 deletions

View File

@ -4,7 +4,7 @@ import { Tooltip as ReactTooltip } from 'react-tooltip';
const Tooltip = ({ text, tooltipId }) => { const Tooltip = ({ text, tooltipId }) => {
return ( return (
<> <>
<svg tabindex="-1" id={tooltipId} xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="currentColor" className="inline-block ml-2 cursor-pointer" viewBox="0 0 16 16" style={{marginTop: 1}}> <svg tabIndex="-1" id={tooltipId} xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="currentColor" className="inline-block ml-2 cursor-pointer" viewBox="0 0 16 16" style={{marginTop: 1}}>
<path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"/> <path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"/>
<path d="M5.255 5.786a.237.237 0 0 0 .241.247h.825c.138 0 .248-.113.266-.25.09-.656.54-1.134 1.342-1.134.686 0 1.314.343 1.314 1.168 0 .635-.374.927-.965 1.371-.673.489-1.206 1.06-1.168 1.987l.003.217a.25.25 0 0 0 .25.246h.811a.25.25 0 0 0 .25-.25v-.105c0-.718.273-.927 1.01-1.486.609-.463 1.244-.977 1.244-2.056 0-1.511-1.276-2.241-2.673-2.241-1.267 0-2.655.59-2.75 2.286zm1.557 5.763c0 .533.425.927 1.01.927.609 0 1.028-.394 1.028-.927 0-.552-.42-.94-1.029-.94-.584 0-1.009.388-1.009.94z"/> <path d="M5.255 5.786a.237.237 0 0 0 .241.247h.825c.138 0 .248-.113.266-.25.09-.656.54-1.134 1.342-1.134.686 0 1.314.343 1.314 1.168 0 .635-.374.927-.965 1.371-.673.489-1.206 1.06-1.168 1.987l.003.217a.25.25 0 0 0 .25.246h.811a.25.25 0 0 0 .25-.25v-.105c0-.718.273-.927 1.01-1.486.609-.463 1.244-.977 1.244-2.056 0-1.511-1.276-2.241-2.673-2.241-1.267 0-2.655.59-2.75 2.286zm1.557 5.763c0 .533.425.927 1.01.927.609 0 1.028-.394 1.028-.927 0-.552-.42-.94-1.029-.94-.584 0-1.009.388-1.009.94z"/>
</svg> </svg>

View File

@ -4,11 +4,13 @@ const path = require('path');
const chokidar = require('chokidar'); const chokidar = require('chokidar');
const { hasJsonExtension, hasBruExtension, writeFile } = require('../utils/filesystem'); const { hasJsonExtension, hasBruExtension, writeFile } = require('../utils/filesystem');
const { const {
bruToJson,
jsonToBru,
bruToEnvJson, bruToEnvJson,
envJsonToBru, envJsonToBru,
} = require('@usebruno/lang'); } = require('@usebruno/lang');
const {
bruToJson,
jsonToBru
} = require('../bru');
const { itemSchema } = require('@usebruno/schema'); const { itemSchema } = require('@usebruno/schema');
const { uuid } = require('../utils/common'); const { uuid } = require('../utils/common');
const { getRequestUid } = require('../cache/requestUids'); const { getRequestUid } = require('../cache/requestUids');
@ -179,6 +181,7 @@ const add = async (win, pathname, collectionUid, collectionPath) => {
try { try {
const bru = fs.readFileSync(pathname, 'utf8'); const bru = fs.readFileSync(pathname, 'utf8');
file.data = bruToJson(bru); file.data = bruToJson(bru);
console.log(JSON.stringify(file.data, null, 2));
hydrateRequestWithUuid(file.data, pathname); hydrateRequestWithUuid(file.data, pathname);
win.webContents.send('main:collection-tree-updated', 'addFile', file); win.webContents.send('main:collection-tree-updated', 'addFile', file);
} catch (err) { } catch (err) {

View File

@ -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
};

View File

@ -3,10 +3,13 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const { ipcMain } = require('electron'); const { ipcMain } = require('electron');
const { const {
jsonToBru,
bruToJson,
envJsonToBru, envJsonToBru,
} = require('@usebruno/lang'); } = require('@usebruno/lang');
const {
bruToJson,
jsonToBru
} = require('../bru');
const { const {
isValidPathname, isValidPathname,
writeFile, writeFile,

View File

@ -4,6 +4,8 @@
"main": "src/index.js", "main": "src/index.js",
"files": [ "files": [
"src", "src",
"v1",
"v2",
"package.json" "package.json"
], ],
"scripts": { "scripts": {

View File

@ -3,9 +3,10 @@ const {
jsonToBru, jsonToBru,
bruToEnvJson, bruToEnvJson,
envJsonToBru 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 = { module.exports = {
bruToJson, bruToJson,
@ -13,5 +14,6 @@ module.exports = {
bruToEnvJson, bruToEnvJson,
envJsonToBru, envJsonToBru,
bruToJsonV2 bruToJsonV2,
jsonToBruV2
}; };