@@ -314,6 +314,41 @@ function parser.parse_request_handler(node, source, context)
314314 return script .load_post_req_hook (str , context )
315315end
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[]
319354function 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
0 commit comments