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,
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

View File

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

View File

@ -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;
}
module.exports = {
safeParseJson,
indentString
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,
outdentString
};

View File

@ -52,7 +52,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}"
},

View File

@ -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}"
},

View File

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