mirror of
https://github.com/usebruno/bruno.git
synced 2025-06-20 11:48:03 +02:00
feat: bruno lang - parse graphql body
This commit is contained in:
parent
87a4778a91
commit
24e11a864c
@ -19,13 +19,13 @@ headers
|
|||||||
|
|
||||||
body(type=json)
|
body(type=json)
|
||||||
{
|
{
|
||||||
apikey: "secret",
|
"apikey": "secret",
|
||||||
numbers: "+91998877665",
|
"numbers": "+91998877665",
|
||||||
data: {
|
"data": {
|
||||||
sender: "TXTLCL",
|
"sender": "TXTLCL",
|
||||||
messages: [{
|
"messages": [{
|
||||||
numbers: "+91998877665",
|
"numbers": "+91998877665",
|
||||||
message: "Hello World"
|
"message": "Hello World"
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,37 +1,18 @@
|
|||||||
const {
|
const {
|
||||||
str,
|
|
||||||
sequenceOf,
|
|
||||||
optionalWhitespace,
|
|
||||||
choice,
|
|
||||||
endOfInput,
|
|
||||||
between,
|
between,
|
||||||
regex,
|
regex,
|
||||||
everyCharUntil
|
everyCharUntil,
|
||||||
} = require("arcsecond");
|
} = require("arcsecond");
|
||||||
const { safeParseJson } = require('./utils');
|
const { safeParseJson } = require('./utils');
|
||||||
|
|
||||||
const newline = regex(/^\r?\n/);
|
|
||||||
const newLineOrEndOfInput = choice([newline, endOfInput]);
|
|
||||||
|
|
||||||
// body(type=json)
|
// body(type=json)
|
||||||
const bodyJsonBegin = sequenceOf([
|
const bodyJsonBegin = regex(/^body\s*\(\s*type\s*=\s*json\s*\)\s*\r?\n/);
|
||||||
str('body'),
|
|
||||||
optionalWhitespace,
|
|
||||||
str('('),
|
|
||||||
optionalWhitespace,
|
|
||||||
str('type'),
|
|
||||||
optionalWhitespace,
|
|
||||||
str('='),
|
|
||||||
optionalWhitespace,
|
|
||||||
str('json'),
|
|
||||||
optionalWhitespace,
|
|
||||||
regex(/^\)\s*\r?\n/),
|
|
||||||
]);
|
|
||||||
|
|
||||||
const bodyEnd = sequenceOf([
|
// body(type=graphql)
|
||||||
regex(/^\/body\s*/),
|
const bodyGraphqlBegin = regex(/^body\s*\(\s*type\s*=\s*graphql\s*\)\s*\r?\n/);
|
||||||
newLineOrEndOfInput
|
|
||||||
]);
|
const bodyEnd = regex(/^\/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) {
|
if(bodyJson && bodyJson.length) {
|
||||||
@ -40,20 +21,37 @@ const bodyJsonTag = between(bodyJsonBegin)(bodyEnd)(everyCharUntil(bodyEnd)).map
|
|||||||
|
|
||||||
if(!safelyParsed) {
|
if(!safelyParsed) {
|
||||||
return {
|
return {
|
||||||
bodyJson
|
body: {
|
||||||
|
json: bodyJson
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
bodyJson: JSON.stringify(safelyParsed)
|
body: {
|
||||||
|
json: JSON.stringify(safelyParsed)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
bodyJson
|
body: {
|
||||||
|
json: bodyJson
|
||||||
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const bodyGraphqlTag = between(bodyGraphqlBegin)(bodyEnd)(everyCharUntil(bodyEnd)).map((bodyGraphql) => {
|
||||||
|
return {
|
||||||
|
body: {
|
||||||
|
graphql: {
|
||||||
|
query: bodyGraphql
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
bodyJsonTag
|
bodyJsonTag,
|
||||||
|
bodyGraphqlTag
|
||||||
};
|
};
|
||||||
|
@ -3,12 +3,14 @@ const {
|
|||||||
choice,
|
choice,
|
||||||
anyChar
|
anyChar
|
||||||
} = require("arcsecond");
|
} = require("arcsecond");
|
||||||
|
const _ = require('lodash');
|
||||||
|
|
||||||
const inlineTag = require('./inline-tag');
|
const inlineTag = require('./inline-tag');
|
||||||
const paramsTag = require('./params-tag');
|
const paramsTag = require('./params-tag');
|
||||||
const headersTag = require('./headers-tag');
|
const headersTag = require('./headers-tag');
|
||||||
const {
|
const {
|
||||||
bodyJsonTag
|
bodyJsonTag,
|
||||||
|
bodyGraphqlTag
|
||||||
} = require('./body-tag');
|
} = require('./body-tag');
|
||||||
|
|
||||||
const bruToJson = (fileContents) => {
|
const bruToJson = (fileContents) => {
|
||||||
@ -17,18 +19,14 @@ const bruToJson = (fileContents) => {
|
|||||||
paramsTag,
|
paramsTag,
|
||||||
headersTag,
|
headersTag,
|
||||||
bodyJsonTag,
|
bodyJsonTag,
|
||||||
|
bodyGraphqlTag,
|
||||||
anyChar
|
anyChar
|
||||||
]));
|
]));
|
||||||
|
|
||||||
const parsed = parser
|
const parsed = parser
|
||||||
.run(fileContents)
|
.run(fileContents)
|
||||||
.result
|
.result
|
||||||
.reduce((acc, item) => {
|
.reduce((acc, item) => _.merge(acc, item), {});
|
||||||
return {
|
|
||||||
...acc,
|
|
||||||
...item
|
|
||||||
};
|
|
||||||
}, {});
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
ver: parsed.ver,
|
ver: parsed.ver,
|
||||||
@ -38,7 +36,7 @@ const bruToJson = (fileContents) => {
|
|||||||
url: parsed.url,
|
url: parsed.url,
|
||||||
params: parsed.params,
|
params: parsed.params,
|
||||||
headers: parsed.headers,
|
headers: parsed.headers,
|
||||||
bodyJson: parsed.bodyJson
|
body: parsed.body
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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.bodyJson).toEqual('{"foo":"bar"}');
|
expect(result.result.body.json).toEqual('{"foo":"bar"}');
|
||||||
};
|
};
|
||||||
|
|
||||||
// simple case
|
// simple case
|
||||||
|
@ -24,3 +24,11 @@ body(type=json)
|
|||||||
}
|
}
|
||||||
/body
|
/body
|
||||||
|
|
||||||
|
body(type=graphql)
|
||||||
|
{
|
||||||
|
launchesPast {
|
||||||
|
launch_success
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/body
|
||||||
|
|
||||||
|
@ -50,7 +50,12 @@ describe('bruToJson', () => {
|
|||||||
"value": "{{transactionId}}"
|
"value": "{{transactionId}}"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"bodyJson": '{"apikey":"secret","numbers":"+91998877665"}'
|
"body": {
|
||||||
|
"json": '{"apikey":"secret","numbers":"+91998877665"}',
|
||||||
|
"graphql": {
|
||||||
|
"query": " {\n launchesPast {\n launch_success\n }\n }\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user