mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-25 01:14:23 +01:00
feat: bruno lang support parsing headers in .bru file
This commit is contained in:
parent
b75baf57ba
commit
60e613fac8
54
packages/bruno-lang/src/headers-tag.js
Normal file
54
packages/bruno-lang/src/headers-tag.js
Normal 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;
|
@ -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
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -9,4 +9,10 @@ params
|
||||
1 apiKey secret
|
||||
1 numbers 998877665
|
||||
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
|
@ -32,6 +32,23 @@ describe('bruToJson', () => {
|
||||
"key": "message",
|
||||
"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}}"
|
||||
}
|
||||
]
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user