Skip to content

Commit 97cc922

Browse files
feat: expand variables inside external body (fix #455)
1 parent 9f6f9dd commit 97cc922

File tree

6 files changed

+46
-3
lines changed

6 files changed

+46
-3
lines changed

lua/rest-nvim/client/curl/cli.lua

+5-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,11 @@ function builder.build(req, ignore_stats)
299299
insert(args, builder.cookies(req.cookies))
300300
if req.body then
301301
if req.body.__TYPE == "external" then
302-
insert(args, builder.file(req.body.data.path))
302+
if req.body.data.content then
303+
insert(args, builder.raw_body(req.body.data.content))
304+
else
305+
insert(args, builder.file(req.body.data.path))
306+
end
303307
elseif req.body.__TYPE == "multipart_form_data" then
304308
log.error("multipart-form-data body is not supportted yet")
305309
elseif vim.list_contains({ "json", "xml", "raw", "graphql" }, req.body.__TYPE) then

lua/rest-nvim/parser/init.lua

+8
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ function parser.parse_body(content_type, body_node, source, context)
136136
name = get_node_field_text(body_node, "name", source),
137137
path = path,
138138
}
139+
local body_text = vim.treesitter.get_node_text(body_node, source)
140+
if vim.startswith(body_text, "<@") then
141+
logger.debug("external body with '<@' prefix")
142+
return body
143+
end
144+
local file_content = utils.read_file(path)
145+
file_content = expand_variables(file_content, context)
146+
body.data.content = file_content
139147
elseif node_type == "graphql_body" then
140148
body.__TYPE = "graphql"
141149
local query_text = vim.treesitter.get_node_text(assert(body_node:named_child(0)), source)

spec/examples/examples_spec.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ describe("builtin request hooks", function()
109109
it("with external body", function()
110110
local source = open("spec/examples/request_body/external_body.http")
111111
local _, tree = utils.ts_parse_source(source)
112-
local req_node = assert(tree:root():child(0))
112+
local req_node = assert(tree:root():child(1))
113113
local req = assert(parser.parse(req_node, source))
114114
_G.rest_request = req
115115
vim.api.nvim_exec_autocmds("User", {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
@bar=baz
12
### External body
23
POST https://example.com:8080/api/html/post
34
# rest.nvim can guess and fill the Content-Type header from file path
45

56
< ./input.json
7+
8+
### External body (raw)
9+
POST https://example.com:8080/api/html/post
10+
# rest.nvim can guess and fill the Content-Type header from file path
11+
12+
<@ ./input.json

spec/examples/request_body/input.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"foo": "{{bar}}"
3+
}

spec/parser/http_parser_spec.lua

+22-1
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,35 @@ key5 = value5
186186
-- external body can be only sourced when
187187
local source = open("spec/examples/request_body/external_body.http")
188188
local _, tree = utils.ts_parse_source(source)
189-
local req_node = assert(tree:root():child(0))
189+
local req_node = assert(tree:root():child(1))
190190
assert.same({
191191
method = "POST",
192192
url = "https://example.com:8080/api/html/post",
193193
headers = {},
194194
cookies = {},
195195
handlers = {},
196196
name = "External body",
197+
body = {
198+
__TYPE = "external",
199+
data = {
200+
path = "spec/examples/request_body/input.json",
201+
content = '{\n "foo": "baz"\n}\n',
202+
},
203+
},
204+
}, parser.parse(req_node, source))
205+
end)
206+
it("parse external body (raw)", function()
207+
-- external body can be only sourced when
208+
local source = open("spec/examples/request_body/external_body.http")
209+
local _, tree = utils.ts_parse_source(source)
210+
local req_node = assert(tree:root():child(2))
211+
assert.same({
212+
method = "POST",
213+
url = "https://example.com:8080/api/html/post",
214+
headers = {},
215+
cookies = {},
216+
handlers = {},
217+
name = "External body (raw)",
197218
body = {
198219
__TYPE = "external",
199220
data = {

0 commit comments

Comments
 (0)