Skip to content

add callbacks on start/stop request #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions doc/rest-nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ CONTENTS *rest-nvim-contents*
3. Import body from external file......|rest-nvim-usage-external-files|
4. Environment Variables........|rest-nvim-usage-environment-variables|
5. Dynamic Variables................|rest-nvim-usage-dynamic-variables|
6. Callbacks................................|rest-nvim-usage-callbacks|
5. Known issues..........................................|rest-nvim-issues|
6. License..............................................|rest-nvim-license|
7. Contributing....................................|rest-nvim-contributing|
Expand Down Expand Up @@ -220,6 +221,22 @@ You can extend or overwrite built-in dynamic variables, with the config key
` },`
`})`

===============================================================================
CALLBACKS *rest-nvim-usage-callbacks*

rest.nvim fires different events upon requests:
- a User RestStartRequest event when launching the request
- a User RestStopRequest event when the requests finishes or errors out

vim.api.nvim_create_autocmd("User", {
pattern = "RestStartRequest",
once = true,
callback = function(opts)
print("IT STARTED")
vim.pretty_print(opts)
end,
})


===============================================================================
KNOWN ISSUES *rest-nvim-issues*
Expand Down
13 changes: 13 additions & 0 deletions lua/rest-nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,20 @@ rest.run_request = function(req, opts)
request.highlight(result.bufnr, result.start_line, result.end_line)
end

local request_id = vim.loop.now()
local data = {
requestId = request_id,
request = req
}

vim.api.nvim_exec_autocmds("User", {
pattern = "RestStartRequest",
modeline = false,
data = data
})
local success_req, req_err = pcall(curl.curl_cmd, Opts)
vim.api.nvim_exec_autocmds("User", { pattern = "RestStopRequest", modeline = false,
data = vim.tbl_extend("keep", { status = success_req, message = req_err }, data) })

if not success_req then
vim.api.nvim_err_writeln(
Expand Down