Skip to content

Commit b8cfe07

Browse files
committed
feat: re-implement pre and post request hooks, load env variables from environment file before running the requests
1 parent ee3f047 commit b8cfe07

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

lua/rest-nvim/api.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,28 @@ function api.register_rest_keybind(mode, lhs, cmd, opts)
5252
keybinds.register_keybind(mode, lhs, cmd, opts)
5353
end
5454

55+
---Execute all the pre-request hooks, functions that are meant to run before executing a request
56+
---
57+
---This function is called automatically during the execution of the requests, invoking it again could cause inconveniences
58+
---@see vim.api.nvim_exec_autocmds
59+
---@package
60+
function api.exec_pre_request_hooks()
61+
vim.api.nvim_exec_autocmds("User", {
62+
pattern = "RestStartRequest",
63+
modeline = false,
64+
})
65+
end
66+
67+
---Execute all the post-request hooks, functions that are meant to run after executing a request
68+
---
69+
---This function is called automatically during the execution of the requests, invoking it again could cause inconveniences
70+
---@see vim.api.nvim_exec_autocmds
71+
---@package
72+
function api.exec_post_request_hooks()
73+
vim.api.nvim_exec_autocmds("User", {
74+
pattern = "RestStopRequest",
75+
modeline = false,
76+
})
77+
end
78+
5579
return api

lua/rest-nvim/functions.lua

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ function functions.exec(scope)
2727
})
2828

2929
local api = require("rest-nvim.api")
30+
local env_vars = require("rest-nvim.parser.env_vars")
31+
3032
local logger = _G._rest_nvim.logger
3133
local ok, client = pcall(require, "rest-nvim.client." .. _G._rest_nvim.client)
3234
if not ok then
@@ -60,6 +62,17 @@ function functions.exec(scope)
6062

6163
utils.highlight(0, req.start, req.end_, api.namespace)
6264

65+
-- Set up a _rest_nvim_req_data Lua global table that holds the parsed request
66+
-- so the values can be modified from the pre-request hooks
67+
_G._rest_nvim_req_data = req
68+
-- Load environment variables from the env file
69+
env_vars.read_file(true)
70+
-- Run pre-request hooks
71+
api.exec_pre_request_hooks()
72+
-- Clean the _rest_nvim_req_data global after running the pre-request hooks
73+
-- as the req table will remain modified
74+
_G._rest_nvim_req_data = nil
75+
6376
if found_nio then
6477
req_results = nio
6578
.run(function()
@@ -82,6 +95,17 @@ function functions.exec(scope)
8295
else
8396
utils.highlight(0, req.start, req.end_, api.namespace)
8497

98+
-- Set up a _rest_nvim_req_data Lua global table that holds the parsed request
99+
-- so the values can be modified from the pre-request hooks
100+
_G._rest_nvim_req_data = req
101+
-- Load environment variables from the env file
102+
env_vars.read_file(true)
103+
-- Run pre-request hooks
104+
api.exec_pre_request_hooks()
105+
-- Clean the _rest_nvim_req_data global after running the pre-request hooks
106+
-- as the req table will remain modified
107+
_G._rest_nvim_req_data = nil
108+
85109
if found_nio then
86110
req_results = nio
87111
.run(function()
@@ -94,15 +118,24 @@ function functions.exec(scope)
94118
end
95119
end
96120

121+
-- We should not be trying to show a result or evaluate code if the request failed
97122
if not vim.tbl_isempty(req_results) then
98-
-- We should not be trying to show a result if the request failed
99123
local result_buf = result.get_or_create_buf()
100124
result.write_res(result_buf, req_results)
101125

102126
-- Load the script variables
103127
if req_results.script ~= nil or not req_results.script == "" then
104128
script_vars.load(req_results.script, req_results)
105129
end
130+
131+
-- Set up a _rest_nvim_res_data Lua global table that holds the request results
132+
-- so the values can be modified from the post-request hooks
133+
_G._rest_nvim_res_data = req_results
134+
-- Run post-request hooks
135+
api.exec_post_request_hooks()
136+
-- Clean the _rest_nvim_res_data global after running the post-request hooks
137+
-- as the req_results table will remain modified
138+
_G._rest_nvim_res_data = nil
106139
end
107140
end
108141

0 commit comments

Comments
 (0)