Skip to content

Commit f04b205

Browse files
committed
feat: add verbose command to preview cURL requests
1 parent a02bf7a commit f04b205

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

doc/rest-nvim.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,12 @@ Notes:
8787
===============================================================================
8888
COMMANDS *rest-nvim-usage-commands*
8989

90-
| `<Plug>RestNvim` | Run `rest.nvim` in the current cursor position.
90+
- `<Plug>RestNvim`
91+
Run `rest.nvim` in the current cursor position.
92+
93+
- `<Plug>RestNvimPreview`
94+
Same as `RestNvim` but it returns the cURL command without executing the
95+
request. Intended for debugging purposes.
9196

9297

9398
===============================================================================

lua/rest-nvim/init.lua

+14-3
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,15 @@ end
232232
-- @param opts curl arguments
233233
local function curl_cmd(opts)
234234
local res = curl[opts.method](opts)
235+
if opts.dry_run then
236+
print(
237+
'[rest.nvim] Request preview:\n'
238+
.. 'curl '
239+
.. table.concat(res, ' ')
240+
)
241+
return
242+
end
243+
235244
local res_bufnr = get_or_create_buf()
236245
local parsed_url = parse_url(fn.getline('.'))
237246
local json_body = false
@@ -301,7 +310,7 @@ end
301310

302311
-- run will retrieve the required request information from the current buffer
303312
-- and then execute curl
304-
local function run()
313+
local function run(verbose)
305314
local bufnr = api.nvim_win_get_buf(0)
306315
local parsed_url = parse_url(fn.getline('.'))
307316
local last_query_line_number = fn.line('.')
@@ -331,18 +340,20 @@ local function run()
331340
local auth = get_auth(bufnr, last_query_line_number)
332341
local accept = get_accept(bufnr, last_query_line_number)
333342

334-
local success_req = pcall(curl_cmd, {
343+
local success_req, req_err = pcall(curl_cmd, {
335344
method = parsed_url.method:lower(),
336345
url = parsed_url.url,
337346
headers = headers,
338347
accept = accept,
339348
body = body,
340349
auth = auth,
350+
dry_run = verbose and verbose or false,
341351
})
342352

343353
if not success_req then
344354
error(
345-
'[rest.nvim] Failed to perform the request.\nMake sure that you have entered the proper URL and the server is running.',
355+
'[rest.nvim] Failed to perform the request.\nMake sure that you have entered the proper URL and the server is running.\n\nTraceback: '
356+
.. req_err,
346357
2
347358
)
348359
end

plugin/rest-nvim.vim

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ endif
66
if exists('g:loaded_rest_nvim') | finish | endif
77

88
nnoremap <Plug>RestNvim :lua require('rest-nvim').run()<CR>
9+
nnoremap <Plug>RestNvimPreview :lua require('rest-nvim').run(true)<CR>
910
1011
let s:save_cpo = &cpo
1112
set cpo&vim

0 commit comments

Comments
 (0)