Skip to content

Commit f9a5ebb

Browse files
feat: save response to file (close #359)
1 parent 9e00468 commit f9a5ebb

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

Diff for: lua/rest-nvim/parser/init.lua

+42
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,41 @@ function parser.parse_request_handler(node, source, context)
314314
return script.load_post_req_hook(str, context)
315315
end
316316

317+
---@param node TSNode
318+
---@param source Source
319+
---@param ctx rest.Context
320+
---@return function?
321+
function parser.parse_redirect_path(node, source, ctx)
322+
local force = vim.treesitter.get_node_text(node, source):match("^>>!")
323+
local path = get_node_field_text(node, "path", source)
324+
if path then
325+
path = expand_variables(path, ctx)
326+
return function (res)
327+
if not res.body then
328+
return
329+
end
330+
logger.debug("save response body to:", path)
331+
if not force then
332+
local suffix_idx = 1
333+
while utils.file_exists(path) do
334+
local pathname, pathext = path:match("([^.]+)(.*)")
335+
path = ("%s_%d%s"):format(pathname, suffix_idx, pathext)
336+
suffix_idx = suffix_idx + 1
337+
end
338+
end
339+
local respfile, openerr = io.open(path, "w+")
340+
if not respfile then
341+
local err_msg = string.format("Failed to open response file (%s): %s", path, openerr)
342+
vim.notify(err_msg, vim.log.levels.ERROR, { title = "rest.nvim" })
343+
return
344+
end
345+
respfile:write(res.body)
346+
respfile:close()
347+
logger.debug("response body saved done")
348+
end
349+
end
350+
end
351+
317352
---@param source Source
318353
---@return string[]
319354
function parser.get_request_names(source)
@@ -433,10 +468,17 @@ function parser.parse(node, source, ctx)
433468
for child, _ in req_node:iter_children() do
434469
local child_type = child:type()
435470
if child_type == "res_handler_script" then
471+
logger.debug("find request node child:", child_type)
436472
local handler = parser.parse_request_handler(child, source, ctx)
437473
if handler then
438474
table.insert(handlers, handler)
439475
end
476+
elseif child_type == "res_redirect" then
477+
logger.debug("find request node child:", child_type)
478+
local handler = parser.parse_redirect_path(child, source, ctx)
479+
if handler then
480+
table.insert(handlers, handler)
481+
end
440482
end
441483
end
442484
if not name then

Diff for: lua/rest-nvim/request.lua

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ local function run_request(req)
7373
logger.info("request success")
7474

7575
-- run request handler scripts
76+
logger.debug(("run %d handers"):format(#req.handlers))
7677
vim.iter(req.handlers):each(function(f)
7778
f(res)
7879
end)

Diff for: spec/parser/http_parser_spec.lua

+8
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,14 @@ Authorization: Bearer {{TOKEN}}
311311
}, c.lv)
312312
end)
313313

314+
it("parse response-redirect syntax", function ()
315+
local source = "GET localhost:3000\n\n>> path/to/file.json\n"
316+
local _, tree = utils.ts_parse_source(source)
317+
local node = assert(tree:root():child(0))
318+
local req = assert(parser.parse(node, source))
319+
assert.same(1, #req.handlers)
320+
end)
321+
314322
-- TODO: update this testcase
315323
-- it("create context from source", function()
316324
-- local source = [[

0 commit comments

Comments
 (0)