Skip to content

Commit

Permalink
feat: add verbose command to preview cURL requests
Browse files Browse the repository at this point in the history
  • Loading branch information
NTBBloodbath committed Jun 30, 2021
1 parent a02bf7a commit f04b205
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
7 changes: 6 additions & 1 deletion doc/rest-nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ Notes:
===============================================================================
COMMANDS *rest-nvim-usage-commands*

| `<Plug>RestNvim` | Run `rest.nvim` in the current cursor position.
- `<Plug>RestNvim`
Run `rest.nvim` in the current cursor position.

- `<Plug>RestNvimPreview`
Same as `RestNvim` but it returns the cURL command without executing the
request. Intended for debugging purposes.


===============================================================================
Expand Down
17 changes: 14 additions & 3 deletions lua/rest-nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,15 @@ end
-- @param opts curl arguments
local function curl_cmd(opts)
local res = curl[opts.method](opts)
if opts.dry_run then
print(
'[rest.nvim] Request preview:\n'
.. 'curl '
.. table.concat(res, ' ')
)
return
end

local res_bufnr = get_or_create_buf()
local parsed_url = parse_url(fn.getline('.'))
local json_body = false
Expand Down Expand Up @@ -301,7 +310,7 @@ end

-- run will retrieve the required request information from the current buffer
-- and then execute curl
local function run()
local function run(verbose)
local bufnr = api.nvim_win_get_buf(0)
local parsed_url = parse_url(fn.getline('.'))
local last_query_line_number = fn.line('.')
Expand Down Expand Up @@ -331,18 +340,20 @@ local function run()
local auth = get_auth(bufnr, last_query_line_number)
local accept = get_accept(bufnr, last_query_line_number)

local success_req = pcall(curl_cmd, {
local success_req, req_err = pcall(curl_cmd, {
method = parsed_url.method:lower(),
url = parsed_url.url,
headers = headers,
accept = accept,
body = body,
auth = auth,
dry_run = verbose and verbose or false,
})

if not success_req then
error(
'[rest.nvim] Failed to perform the request.\nMake sure that you have entered the proper URL and the server is running.',
'[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,
2
)
end
Expand Down
1 change: 1 addition & 0 deletions plugin/rest-nvim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ endif
if exists('g:loaded_rest_nvim') | finish | endif

nnoremap <Plug>RestNvim :lua require('rest-nvim').run()<CR>
nnoremap <Plug>RestNvimPreview :lua require('rest-nvim').run(true)<CR>
let s:save_cpo = &cpo
set cpo&vim
Expand Down

0 comments on commit f04b205

Please sign in to comment.