feat: bruno land outdent strings during parsing

This commit is contained in:
Anoop M D 2023-01-17 00:55:47 +05:30
parent b5116b54af
commit 23076b41c6
7 changed files with 79 additions and 46 deletions

View File

@ -11,8 +11,6 @@ const {
sepBy, sepBy,
sequenceOf sequenceOf
} = require("arcsecond"); } = require("arcsecond");
const { safeParseJson } = require('./utils');
// body(type=json) // body(type=json)
const bodyJsonBegin = regex(/^body\s*\(\s*type\s*=\s*json\s*\)\s*\r?\n/); 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 bodyEnd = regex(/^[\r?\n]+\/body\s*[\r?\n]*/);
const bodyJsonTag = between(bodyJsonBegin)(bodyEnd)(everyCharUntil(bodyEnd)).map((bodyJson) => { 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 { return {
body: { body: {
json: bodyJson json: bodyJson

View File

@ -5,8 +5,9 @@ const {
} = require("arcsecond"); } = require("arcsecond");
const _ = require('lodash'); const _ = require('lodash');
const { const {
safeParseJson, indentString,
indentString outdentString,
get
} = require('./utils'); } = require('./utils');
const inlineTag = require('./inline-tag'); const inlineTag = require('./inline-tag');
@ -40,7 +41,7 @@ const bruToJson = (fileContents) => {
.result .result
.reduce((acc, item) => _.merge(acc, item), {}); .reduce((acc, item) => _.merge(acc, item), {});
return { const json = {
type: parsed.type || '', type: parsed.type || '',
name: parsed.name || '', name: parsed.name || '',
request: { request: {
@ -51,6 +52,26 @@ const bruToJson = (fileContents) => {
body: parsed.body || {mode: 'none'} 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) => { 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) { 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 += ` bru += `
body(type=json) body(type=json)
${indentString(jsonText)} ${indentString(body.json)}
/body /body
`; `;
} }

View File

@ -13,9 +13,32 @@ const indentString = (str) => {
} }
return str.split("\n").map(line => " " + line).join("\n"); 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 = { module.exports = {
get,
safeParseJson, safeParseJson,
indentString indentString,
outdentString
}; };

View File

@ -5,7 +5,7 @@ describe('bodyJsonTag', () => {
const testbodyJson = (input, expected) => { const testbodyJson = (input, expected) => {
const result = bodyJsonTag.run(input); const result = bodyJsonTag.run(input);
expect(result.isError).toBe(false); expect(result.isError).toBe(false);
expect(result.result.body.json).toEqual('{"foo":"bar"}'); expect(result.result.body.json).toEqual('{ "foo": "bar" }');
}; };
// simple case // simple case

View File

@ -52,12 +52,12 @@ describe('bruToJson', () => {
], ],
"body": { "body": {
"mode": "json", "mode": "json",
"json": '{"apikey":"secret","numbers":"+91998877665"}', "json": '{\n "apikey": "secret",\n "numbers": "+91998877665"\n}',
"graphql": { "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", "text": "Hello, there. You must be from the past",
"xml": " <body>back to the ice age</body>", "xml": "<body>back to the ice age</body>",
"formUrlEncoded": [ "formUrlEncoded": [
{ {
"enabled": true, "enabled": true,

View File

@ -49,7 +49,7 @@ describe('bruToJson', () => {
], ],
"body": { "body": {
"mode": "json", "mode": "json",
"json": '{"apikey":"secret","numbers":"+91998877665"}', "json": '{\n "apikey": "secret",\n "numbers": "+91998877665"\n}',
"graphql": { "graphql": {
"query": "{\n launchesPast {\n launch_success\n }\n}" "query": "{\n launchesPast {\n launch_success\n }\n}"
}, },

View File

@ -1,6 +1,8 @@
const { const {
safeParseJson, safeParseJson,
indentString indentString,
outdentString,
get
} = require('../src/utils'); } = require('../src/utils');
describe('utils', () => { describe('utils', () => {
@ -25,4 +27,24 @@ describe('utils', () => {
expect(indentString(input)).toBe(expectedOutput); 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);
});
});
}); });