mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-25 09:23:17 +01:00
feat: bruno lang - parse graphql body
This commit is contained in:
parent
87a4778a91
commit
24e11a864c
@ -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"
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
};
|
||||
|
@ -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
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -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
|
||||
|
@ -24,3 +24,11 @@ body(type=json)
|
||||
}
|
||||
/body
|
||||
|
||||
body(type=graphql)
|
||||
{
|
||||
launchesPast {
|
||||
launch_success
|
||||
}
|
||||
}
|
||||
/body
|
||||
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user