feat: bruno lang support parsing headers in .bru file

This commit is contained in:
Anoop M D 2023-01-14 20:21:54 +05:30
parent b75baf57ba
commit 60e613fac8
4 changed files with 83 additions and 15 deletions

View File

@ -0,0 +1,54 @@
const {
sequenceOf,
whitespace,
optionalWhitespace,
choice,
endOfInput,
everyCharUntil,
between,
digit,
many,
regex,
sepBy
} = require("arcsecond");
const newline = regex(/^\r?\n/);
const newLineOrEndOfInput = choice([newline, endOfInput]);
const begin = sequenceOf([
regex(/^headers[^\S\r\n]*/),
newline
]);
const end = sequenceOf([
regex(/^\/headers[^\S\r\n]*/),
newLineOrEndOfInput
]);
const key = everyCharUntil(whitespace);
const value = everyCharUntil(whitespace);
const line = sequenceOf([
optionalWhitespace,
digit,
whitespace,
key,
whitespace,
value,
newLineOrEndOfInput
]).map(([_, enabled, __, key, ___, value]) => {
return {
"enabled": enabled,
"key": key,
"value": value
};
});
const lines = many(line);
const headersLines = sepBy(newline)(lines);
const headersTag = between(begin)(end)(headersLines).map(([headers]) => {
return {
headers
};
});
module.exports = headersTag;

View File

@ -1,6 +1,4 @@
const { const {
sepBy,
regex,
many, many,
choice, choice,
anyChar anyChar
@ -8,21 +6,13 @@ const {
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 bruToJson = (fileContents) => { const bruToJson = (fileContents) => {
const newline = regex(/^\r?\n/); const parser = many(choice([
const line = inlineTag;
const lines = many(line);
// const parser = sepBy(newline)(lines);
let parser = choice([
sepBy(newline)(lines),
paramsTag
]);
parser = many(choice([
inlineTag, inlineTag,
paramsTag, paramsTag,
headersTag,
anyChar anyChar
])); ]));
@ -42,7 +32,8 @@ const bruToJson = (fileContents) => {
name: parsed.name, name: parsed.name,
method: parsed.method, method: parsed.method,
url: parsed.url, url: parsed.url,
params: parsed.params params: parsed.params,
headers: parsed.headers
} }
}; };

View File

@ -10,3 +10,9 @@ params
1 numbers 998877665 1 numbers 998877665
1 message hello 1 message hello
/params /params
headers
1 content-type application/json
1 accept-language en-US,en;q=0.9,hi;q=0.8
0 transaction-id {{transactionId}}
/headers

View File

@ -32,6 +32,23 @@ describe('bruToJson', () => {
"key": "message", "key": "message",
"value": "hello" "value": "hello"
} }
],
"headers": [
{
"enabled": "1",
"key": "content-type",
"value": "application/json"
},
{
"enabled": "1",
"key": "accept-language",
"value": "en-US,en;q=0.9,hi;q=0.8"
},
{
"enabled": "0",
"key": "transaction-id",
"value": "{{transactionId}}"
}
] ]
}); });
}); });