mirror of
https://github.com/macocianradu/nvim-http.git
synced 2026-07-16 02:48:46 +00:00
Added input parsing, and curl response parsing
This commit is contained in:
@@ -17,8 +17,4 @@ M.defaults = {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
function M.merge(opts)
|
|
||||||
return vim.tbl_deep_extend("force", {}, M.defaults, opts or {})
|
|
||||||
end
|
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
+42
-2
@@ -1,12 +1,52 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
local output_fields = {"http_code", "time_total"}
|
||||||
|
|
||||||
function M.get_current_line()
|
function M.get_current_line()
|
||||||
return vim.api.nvim_get_current_line()
|
return vim.api.nvim_get_current_line()
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.parse_line(line)
|
function M.parse_line(line)
|
||||||
local a, b = line:match"^(%S+)%s+(.+)"
|
local method, url = line:match"^%s*(%S+)%s+(.+)"
|
||||||
return a, b
|
return method, url
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.execute(method, url, fields)
|
||||||
|
local outFormat = "\\n--metadata--\\n"
|
||||||
|
for _, field in pairs(fields) do
|
||||||
|
outFormat = outFormat .. field .. ":%{" .. field .. "}\\n"
|
||||||
|
end
|
||||||
|
local result = vim.system({"curl", url, "-X", method, "-w", outFormat}, {
|
||||||
|
text = true,
|
||||||
|
}):wait()
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.parse_output(response, fields)
|
||||||
|
local body, metadata = response:match"(.-)%s*\n%-%-metadata%-%-\n(.*)"
|
||||||
|
local result = {
|
||||||
|
body = body or ""
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, field in pairs(fields) do
|
||||||
|
local value = metadata:match(field .. ":([^\n]*)")
|
||||||
|
if value then
|
||||||
|
result[field] = value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.run_http_under_cursor()
|
||||||
|
local line = M.get_current_line()
|
||||||
|
vim.notify("Current line: " .. line, vim.log.levels.TRACE)
|
||||||
|
local method, url = M.parse_line(line)
|
||||||
|
vim.notify("Parsed - Method/Command: " .. tostring(method) .. ", URL/Args: " .. tostring(url), vim.log.levels.TRACE)
|
||||||
|
local response = M.execute(method, url, output_fields)
|
||||||
|
vim.notify("Response: " .. response.stdout, vim.log.levels.TRACE)
|
||||||
|
local result = M.parse_output(response.stdout, output_fields)
|
||||||
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
package = "nvim-http"
|
|
||||||
version = "scm-1"
|
|
||||||
rockspec_format = "3.0"
|
|
||||||
|
|
||||||
source = {
|
|
||||||
url = "git+https://github.com/macocianradu/nvim-http",
|
|
||||||
}
|
|
||||||
|
|
||||||
description = {
|
|
||||||
summary = "Neovim HTTP runner plugin",
|
|
||||||
license = "MIT",
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies = {
|
|
||||||
"lua >= 5.1",
|
|
||||||
"lyaml",
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
local ok, nvim_http = pcall(require, "nvim_http")
|
local ok, nvim_http = pcall(require, "nvim_http")
|
||||||
if not ok then
|
if not ok then
|
||||||
|
vim.notify("Failed to load nvim_http: " .. tostring(nvim_http), vim.log.levels.ERROR)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
vim.notify("nvim_http loaded successfully", vim.log.levels.INFO)
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("NvimHttpRun", function()
|
vim.api.nvim_create_user_command("NvimHttpRun", function()
|
||||||
nvim_http.run_http_under_cursor()
|
nvim_http.run_http_under_cursor()
|
||||||
@@ -9,9 +11,7 @@ end, {
|
|||||||
desc = "Run HTTP command under cursor and open Telescope response tree",
|
desc = "Run HTTP command under cursor and open Telescope response tree",
|
||||||
})
|
})
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("NvimHttpClear", function()
|
-- Set up keybinding
|
||||||
nvim_http.clear_http_result()
|
vim.keymap.set('n', '<leader>hr', function()
|
||||||
end, {
|
nvim_http.run_http_under_cursor()
|
||||||
desc = "Close and clear HTTP response tree",
|
end, { desc = "Run HTTP under cursor" })
|
||||||
})
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
GET https://jsonplaceholder.typicode.com/todos/1
|
||||||
Reference in New Issue
Block a user