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