forked from rest-nvim/rest.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
68 lines (57 loc) · 1.98 KB
/
init.lua
File metadata and controls
68 lines (57 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
local rest = {}
local request = require("rest-nvim.request")
local config = require("rest-nvim.config")
local curl = require("rest-nvim.curl")
local LastOpts = {}
rest.setup = function(user_configs)
config.set(user_configs or {})
end
-- run will retrieve the required request information from the current buffer
-- and then execute curl
-- @param verbose toggles if only a dry run with preview should be executed (true = preview)
rest.run = function(verbose)
local ok, result = pcall(request.get_current_request)
if not ok then
vim.api.nvim_err_writeln("[rest.nvim] Failed to get the current HTTP request: " .. result)
return
end
LastOpts = {
method = result.method:lower(),
url = result.url,
headers = result.headers,
raw = config.skip_ssl_verification and { "-k" } or nil,
body = result.body,
dry_run = verbose or false,
bufnr = result.bufnr,
start_line = result.start_line,
end_line = result.end_line,
}
if config.get("highlight").enabled == true then
request.highlight(result.bufnr, result.start_line, result.end_line)
end
local success_req, req_err = pcall(curl.curl_cmd, LastOpts)
if not success_req then
vim.api.nvim_err_writeln(
"[rest.nvim] Failed to perform the request.\nMake sure that you have entered the proper URL and the server is running.\n\nTraceback: "
.. req_err
)
end
end
-- last will run the last curl request, if available
rest.last = function()
if LastOpts.url == nil then
vim.api.nvim_err_writeln("[rest.nvim]: Last request not found")
return
end
if config.get("highlight").enabled == true then
request.highlight(LastOpts.bufnr, LastOpts.start_line, LastOpts.end_line)
end
local success_req, req_err = pcall(curl.curl_cmd, LastOpts)
if not success_req then
vim.api.nvim_err_writeln(
"[rest.nvim] Failed to perform the request.\nMake sure that you have entered the proper URL and the server is running.\n\nTraceback: "
.. req_err
)
end
end
return rest