diff --git a/packages/bruno-lang/example/request.bru b/packages/bruno-lang/example/request.bru index 578fa99b0..8cfcf754e 100644 --- a/packages/bruno-lang/example/request.bru +++ b/packages/bruno-lang/example/request.bru @@ -19,13 +19,13 @@ headers body(type=json) { - apikey: "secret", - numbers: "+91998877665", - data: { - sender: "TXTLCL", - messages: [{ - numbers: "+91998877665", - message: "Hello World" + "apikey": "secret", + "numbers": "+91998877665", + "data": { + "sender": "TXTLCL", + "messages": [{ + "numbers": "+91998877665", + "message": "Hello World" }] } } diff --git a/packages/bruno-lang/src/body-tag.js b/packages/bruno-lang/src/body-tag.js index 208c31f14..a14fad2c4 100644 --- a/packages/bruno-lang/src/body-tag.js +++ b/packages/bruno-lang/src/body-tag.js @@ -1,37 +1,18 @@ const { - str, - sequenceOf, - optionalWhitespace, - choice, - endOfInput, between, regex, - everyCharUntil + everyCharUntil, } = require("arcsecond"); const { safeParseJson } = require('./utils'); -const newline = regex(/^\r?\n/); -const newLineOrEndOfInput = choice([newline, endOfInput]); // body(type=json) -const bodyJsonBegin = sequenceOf([ - str('body'), - optionalWhitespace, - str('('), - optionalWhitespace, - str('type'), - optionalWhitespace, - str('='), - optionalWhitespace, - str('json'), - optionalWhitespace, - regex(/^\)\s*\r?\n/), -]); +const bodyJsonBegin = regex(/^body\s*\(\s*type\s*=\s*json\s*\)\s*\r?\n/); -const bodyEnd = sequenceOf([ - regex(/^\/body\s*/), - newLineOrEndOfInput -]); +// body(type=graphql) +const bodyGraphqlBegin = regex(/^body\s*\(\s*type\s*=\s*graphql\s*\)\s*\r?\n/); + +const bodyEnd = regex(/^\/body\s*[\r?\n]*/); const bodyJsonTag = between(bodyJsonBegin)(bodyEnd)(everyCharUntil(bodyEnd)).map((bodyJson) => { if(bodyJson && bodyJson.length) { @@ -40,20 +21,37 @@ const bodyJsonTag = between(bodyJsonBegin)(bodyEnd)(everyCharUntil(bodyEnd)).map if(!safelyParsed) { return { - bodyJson + body: { + json: bodyJson + } } } return { - bodyJson: JSON.stringify(safelyParsed) + body: { + json: JSON.stringify(safelyParsed) + } }; } return { - bodyJson + body: { + json: bodyJson + } }; }); +const bodyGraphqlTag = between(bodyGraphqlBegin)(bodyEnd)(everyCharUntil(bodyEnd)).map((bodyGraphql) => { + return { + body: { + graphql: { + query: bodyGraphql + } + } + } +}); + module.exports = { - bodyJsonTag + bodyJsonTag, + bodyGraphqlTag }; diff --git a/packages/bruno-lang/src/index.js b/packages/bruno-lang/src/index.js index fc4388ca4..6e9a53eb8 100644 --- a/packages/bruno-lang/src/index.js +++ b/packages/bruno-lang/src/index.js @@ -3,12 +3,14 @@ const { choice, anyChar } = require("arcsecond"); +const _ = require('lodash'); const inlineTag = require('./inline-tag'); const paramsTag = require('./params-tag'); const headersTag = require('./headers-tag'); const { - bodyJsonTag + bodyJsonTag, + bodyGraphqlTag } = require('./body-tag'); const bruToJson = (fileContents) => { @@ -17,18 +19,14 @@ const bruToJson = (fileContents) => { paramsTag, headersTag, bodyJsonTag, + bodyGraphqlTag, anyChar ])); const parsed = parser .run(fileContents) .result - .reduce((acc, item) => { - return { - ...acc, - ...item - }; - }, {}); + .reduce((acc, item) => _.merge(acc, item), {}); return { ver: parsed.ver, @@ -38,7 +36,7 @@ const bruToJson = (fileContents) => { url: parsed.url, params: parsed.params, headers: parsed.headers, - bodyJson: parsed.bodyJson + body: parsed.body } }; diff --git a/packages/bruno-lang/tests/body-tag.spec.js b/packages/bruno-lang/tests/body-tag.spec.js index cd67bfc5b..2d9fa141d 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.bodyJson).toEqual('{"foo":"bar"}'); + expect(result.result.body.json).toEqual('{"foo":"bar"}'); }; // simple case diff --git a/packages/bruno-lang/tests/fixtures/request.bru b/packages/bruno-lang/tests/fixtures/request.bru index ac2e85524..205009d4b 100644 --- a/packages/bruno-lang/tests/fixtures/request.bru +++ b/packages/bruno-lang/tests/fixtures/request.bru @@ -24,3 +24,11 @@ body(type=json) } /body +body(type=graphql) + { + launchesPast { + launch_success + } + } +/body + diff --git a/packages/bruno-lang/tests/parser.spec.js b/packages/bruno-lang/tests/parser.spec.js index 359811b03..7f28ce229 100644 --- a/packages/bruno-lang/tests/parser.spec.js +++ b/packages/bruno-lang/tests/parser.spec.js @@ -50,7 +50,12 @@ describe('bruToJson', () => { "value": "{{transactionId}}" } ], - "bodyJson": '{"apikey":"secret","numbers":"+91998877665"}' + "body": { + "json": '{"apikey":"secret","numbers":"+91998877665"}', + "graphql": { + "query": " {\n launchesPast {\n launch_success\n }\n }\n" + } + } }); }); });