From 23076b41c674e97791294668ed925f039ea67452 Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Tue, 17 Jan 2023 00:55:47 +0530 Subject: [PATCH] feat: bruno land outdent strings during parsing --- packages/bruno-lang/src/body-tag.js | 21 ---------- packages/bruno-lang/src/index.js | 41 +++++++++++-------- packages/bruno-lang/src/utils.js | 27 +++++++++++- packages/bruno-lang/tests/body-tag.spec.js | 2 +- packages/bruno-lang/tests/bru-to-json.spec.js | 8 ++-- packages/bruno-lang/tests/json-to-bru.spec.js | 2 +- packages/bruno-lang/tests/utils.spec.js | 24 ++++++++++- 7 files changed, 79 insertions(+), 46 deletions(-) diff --git a/packages/bruno-lang/src/body-tag.js b/packages/bruno-lang/src/body-tag.js index 7910ca6f..0dfbc338 100644 --- a/packages/bruno-lang/src/body-tag.js +++ b/packages/bruno-lang/src/body-tag.js @@ -11,8 +11,6 @@ const { sepBy, sequenceOf } = require("arcsecond"); -const { safeParseJson } = require('./utils'); - // body(type=json) const bodyJsonBegin = regex(/^body\s*\(\s*type\s*=\s*json\s*\)\s*\r?\n/); @@ -29,25 +27,6 @@ const bodyXmlBegin = regex(/^body\s*\(\s*type\s*=\s*xml\s*\)\s*\r?\n/); const bodyEnd = regex(/^[\r?\n]+\/body\s*[\r?\n]*/); const bodyJsonTag = between(bodyJsonBegin)(bodyEnd)(everyCharUntil(bodyEnd)).map((bodyJson) => { - if(bodyJson && bodyJson.length) { - bodyJson = bodyJson.trim(); - const safelyParsed = safeParseJson(bodyJson); - - if(!safelyParsed) { - return { - body: { - json: bodyJson - } - } - } - - return { - body: { - json: JSON.stringify(safelyParsed) - } - }; - } - return { body: { json: bodyJson diff --git a/packages/bruno-lang/src/index.js b/packages/bruno-lang/src/index.js index 14cac214..715be190 100644 --- a/packages/bruno-lang/src/index.js +++ b/packages/bruno-lang/src/index.js @@ -5,8 +5,9 @@ const { } = require("arcsecond"); const _ = require('lodash'); const { - safeParseJson, - indentString + indentString, + outdentString, + get } = require('./utils'); const inlineTag = require('./inline-tag'); @@ -40,7 +41,7 @@ const bruToJson = (fileContents) => { .result .reduce((acc, item) => _.merge(acc, item), {}); - return { + const json = { type: parsed.type || '', name: parsed.name || '', request: { @@ -51,6 +52,26 @@ const bruToJson = (fileContents) => { body: parsed.body || {mode: 'none'} } } + + const body = get(json, 'request.body'); + + if(body && body.text) { + body.text = outdentString(body.text); + } + + if(body && body.json) { + body.json = outdentString(body.json); + } + + if(body && body.xml) { + body.xml = outdentString(body.xml); + } + + if(body && body.graphql && body.graphql.query) { + body.graphql.query = outdentString(body.graphql.query); + } + + return json; }; const jsonToBru = (json) => { @@ -90,21 +111,9 @@ ${headers.map(header => ` ${header.enabled ? 1 : 0} ${header.name} ${header.val } if(body && body.json && body.json.length) { - let jsonText = ''; - let bodyJson = body.json; - if(bodyJson && bodyJson.length) { - bodyJson = bodyJson.trim(); - const safelyParsed = safeParseJson(bodyJson); - - if(safelyParsed) { - jsonText = JSON.stringify(safelyParsed, null, 2); - } else { - jsonText = bodyJson; - } - } bru += ` body(type=json) -${indentString(jsonText)} +${indentString(body.json)} /body `; } diff --git a/packages/bruno-lang/src/utils.js b/packages/bruno-lang/src/utils.js index 38182d3c..fed6fdf4 100644 --- a/packages/bruno-lang/src/utils.js +++ b/packages/bruno-lang/src/utils.js @@ -13,9 +13,32 @@ const indentString = (str) => { } return str.split("\n").map(line => " " + line).join("\n"); -} +}; + +const outdentString = (str) => { + if(!str || !str.length) { + return str; + } + + return str.split("\n").map(line => line.replace(/^ /, '')).join("\n"); +}; + +// implement lodash _.get functionality +const get = (obj, path, defaultValue) => { + const pathParts = path.split('.'); + let current = obj; + for(let i = 0; i < pathParts.length; i++) { + if(current[pathParts[i]] === undefined) { + return defaultValue; + } + current = current[pathParts[i]]; + } + return current; +}; module.exports = { + get, safeParseJson, - indentString + indentString, + outdentString }; diff --git a/packages/bruno-lang/tests/body-tag.spec.js b/packages/bruno-lang/tests/body-tag.spec.js index 2d9fa141..10c161a9 100644 --- a/packages/bruno-lang/tests/body-tag.spec.js +++ b/packages/bruno-lang/tests/body-tag.spec.js @@ -5,7 +5,7 @@ describe('bodyJsonTag', () => { const testbodyJson = (input, expected) => { const result = bodyJsonTag.run(input); expect(result.isError).toBe(false); - expect(result.result.body.json).toEqual('{"foo":"bar"}'); + expect(result.result.body.json).toEqual('{ "foo": "bar" }'); }; // simple case diff --git a/packages/bruno-lang/tests/bru-to-json.spec.js b/packages/bruno-lang/tests/bru-to-json.spec.js index fb7e69d8..07576af9 100644 --- a/packages/bruno-lang/tests/bru-to-json.spec.js +++ b/packages/bruno-lang/tests/bru-to-json.spec.js @@ -52,12 +52,12 @@ describe('bruToJson', () => { ], "body": { "mode": "json", - "json": '{"apikey":"secret","numbers":"+91998877665"}', + "json": '{\n "apikey": "secret",\n "numbers": "+91998877665"\n}', "graphql": { - "query": " {\n launchesPast {\n launch_success\n }\n }" + "query": "{\n launchesPast {\n launch_success\n }\n}" }, - "text": " Hello, there. You must be from the past", - "xml": " back to the ice age", + "text": "Hello, there. You must be from the past", + "xml": "back to the ice age", "formUrlEncoded": [ { "enabled": true, diff --git a/packages/bruno-lang/tests/json-to-bru.spec.js b/packages/bruno-lang/tests/json-to-bru.spec.js index b28530ca..bb47c177 100644 --- a/packages/bruno-lang/tests/json-to-bru.spec.js +++ b/packages/bruno-lang/tests/json-to-bru.spec.js @@ -49,7 +49,7 @@ describe('bruToJson', () => { ], "body": { "mode": "json", - "json": '{"apikey":"secret","numbers":"+91998877665"}', + "json": '{\n "apikey": "secret",\n "numbers": "+91998877665"\n}', "graphql": { "query": "{\n launchesPast {\n launch_success\n }\n}" }, diff --git a/packages/bruno-lang/tests/utils.spec.js b/packages/bruno-lang/tests/utils.spec.js index 618923a7..1d0a32b1 100644 --- a/packages/bruno-lang/tests/utils.spec.js +++ b/packages/bruno-lang/tests/utils.spec.js @@ -1,6 +1,8 @@ const { safeParseJson, - indentString + indentString, + outdentString, + get } = require('../src/utils'); describe('utils', () => { @@ -25,4 +27,24 @@ describe('utils', () => { expect(indentString(input)).toBe(expectedOutput); }); }); + + describe('outdentString', () => { + it('correctly outdents a multiline string', () => { + const input = " line1\n line2\n line3"; + const expectedOutput = "line1\nline2\nline3"; + expect(outdentString(input)).toBe(expectedOutput); + }); + }); + + describe('get', () => { + it('returns the value at the given path', () => { + const input = { a: { b: { c: 1 } } }; + expect(get(input, 'a.b.c')).toBe(1); + }); + + it('returns the defaultValue if the path does not exist', () => { + const input = { a: { b: { c: 1 } } }; + expect(get(input, 'a.b.d', 2)).toBe(2); + }); + }); }); \ No newline at end of file