Skip to content

Commit

Permalink
Merge pull request #50 from mistweaverco/feat/external-processing-of-…
Browse files Browse the repository at this point in the history
…request

Feature: Add metatags and add ability to process responses externally
  • Loading branch information
gorillamoe authored Jul 13, 2024
2 parents 5948c51 + bfbae4a commit fb5396d
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 40 deletions.
20 changes: 20 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
stds.nvim = {
read_globals = { "jit" },
}

std = "lua51+nvim"

read_globals = {
"vim",
}

globals = {
"vim.g",
"vim.b",
"vim.w",
"vim.o",
"vim.bo",
"vim.wo",
"vim.go",
"vim.env",
}
2 changes: 1 addition & 1 deletion docs/_coverpage.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# kulala.nvim <small>2.0.1</small>
# kulala.nvim <small>2.1.0</small>

> A minimal 🤏 HTTP-client 🐼 interface 🖥️ for Neovim ❤️.
Expand Down
18 changes: 0 additions & 18 deletions lua/kulala/client_pipe/init.lua

This file was deleted.

21 changes: 12 additions & 9 deletions lua/kulala/cmd/init.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
local GLOBALS = require("kulala.globals")
local CONFIG = require("kulala.config")
local FORMATTER = require("kulala.formatter")
local CLIENT_PIPE = require("kulala.client_pipe")
local FS = require("kulala.utils.fs")
local FORMATTER = require("kulala.formatter")
local EXT_PROCESSING = require("kulala.external_processing")

local M = {}

-- runs the cmd and maybe formats the result
M.run = function(result, callback)
vim.fn.jobstart(result.cmd, {
on_stdout = function(_, datalist)
-- do nothing
end,
on_stderr = function(_, datalist)
if callback then
if #datalist > 0 and #datalist[1] > 0 then
Expand All @@ -23,11 +19,18 @@ M.run = function(result, callback)
local success = code == 0
if success then
local body = FS.read_file(GLOBALS.BODY_FILE)
if result.ft ~= "text" and not result.client_pipe then
if result.ft ~= "text" then
FS.write_file(GLOBALS.BODY_FILE, FORMATTER.format(result.ft, body))
end
if result.client_pipe then
body = CLIENT_PIPE.pipe(result.client_pipe, body)
vim.notify(vim.inspect(result.metadata), vim.log.levels.INFO)
for _, metadata in ipairs(result.metadata) do
if metadata then
if metadata.name == "stdin-cmd" then
EXT_PROCESSING.stdin_cmd(metadata.value, body)
elseif metadata.name == "env-stdin-cmd" then
EXT_PROCESSING.env_stdin_cmd(metadata.value, body)
end
end
end
end
if callback then
Expand Down
41 changes: 41 additions & 0 deletions lua/kulala/external_processing/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
local FS = require("kulala.utils.fs")

local M = {}

M.env_stdin_cmd = function(cmdstring, contents)
local cmd = {}
local splitted = vim.split(cmdstring, " ")
local env_name = splitted[1]
local cmd_exists = FS.command_exists(splitted[2])
if not cmd_exists then
vim.notify("env_stdin_cmd --> Command not found: " .. cmd[2] .. ".", vim.log.levels.ERROR)
return
end
table.remove(splitted, 1)
for _, token in ipairs(splitted) do
table.insert(cmd, token)
end
local res = vim.system(cmd, { stdin = contents, text = true }):wait().stdout
if res == nil then
vim.notify("env_stdin_cmd --> Command failed: " .. cmd[2] .. ".", vim.log.levels.ERROR)
return
else
vim.env[env_name] = res
end
end

M.stdin_cmd = function(cmdstring, contents)
local cmd = {}
for token in string.gmatch(cmdstring, "[^%s]+") do
table.insert(cmd, token)
end
local cmd_exists = FS.command_exists(cmd[1])
if not cmd_exists then
vim.notify("stdin_cmd --> Command not found: " .. cmd[1] .. ". Returning plain contents..", vim.log.levels.ERROR)
return contents
end
local res = vim.system(cmd, { stdin = contents, text = true }):wait().stdout
return res
end

return M
2 changes: 1 addition & 1 deletion lua/kulala/globals/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local FS = require("kulala.utils.fs")

local M = {}

M.VERSION = "2.0.1"
M.VERSION = "2.1.0"
M.UI_ID = "kulala://ui"
M.HEADERS_FILE = FS.get_plugin_tmp_dir() .. "/headers.txt"
M.BODY_FILE = FS.get_plugin_tmp_dir() .. "/body.txt"
Expand Down
24 changes: 14 additions & 10 deletions lua/kulala/parser/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local GLOBALS = require("kulala.globals")
local CONFIG = require("kulala.config")
local DYNAMIC_VARS = require("kulala.parser.dynamic_vars")
local STRING_UTILS = require("kulala.utils.string")
local TABLE_UTILS = require("kulala.utils.table")
local ENV_PARSER = require("kulala.parser.env")
local PLUGIN_TMP_DIR = FS.get_plugin_tmp_dir()
local M = {}
Expand Down Expand Up @@ -99,6 +100,7 @@ M.get_document = function()
local block_line_count = #lines
local request = {
headers = {},
metadata = {},
body = nil,
start_line = line_offset + 1,
block_line_count = block_line_count,
Expand All @@ -108,6 +110,14 @@ M.get_document = function()
for _, line in ipairs(lines) do
line = vim.trim(line)
if line:sub(1, 1) == "#" then
-- Metadata (e.g., # @this-is-name this is the value)
-- See: https://httpyac.github.io/guide/metaData.html
if line:sub(1, 3) == "# @" then
local meta_name, meta_value = line:match("^# @([%w+%-]+)%s*(.*)$")
if meta_name and meta_value then
table.insert(request.metadata, { name = meta_name, value = meta_value })
end
end
-- It's a comment, skip it
elseif line == "" and is_body_section == false then
-- Skip empty lines
Expand Down Expand Up @@ -229,12 +239,12 @@ end
---@return Request Table containing the request data
function M.parse()
local res = {
metadata = {},
method = "GET",
url = {},
headers = {},
body = {},
cmd = {},
client_pipe = nil,
ft = "text",
}

Expand All @@ -246,6 +256,7 @@ function M.parse()
res.http_version = req.http_version
res.headers = parse_headers(req.headers, document_variables)
res.body = parse_body(req.body, document_variables)
res.metadata = req.metadata

-- We need to append the contents of the file to
-- the body if it is a POST request,
Expand Down Expand Up @@ -290,15 +301,8 @@ function M.parse()
end
end
for key, value in pairs(res.headers) do
-- if key starts with `http-client-` then it is a special header
if key:find("^http%-client%-") then
if key == "http-client-pipe" then
res.client_pipe = value
end
else
table.insert(res.cmd, "-H")
table.insert(res.cmd, key .. ":" .. value)
end
table.insert(res.cmd, "-H")
table.insert(res.cmd, key .. ":" .. value)
end
if res.http_version ~= nil then
table.insert(res.cmd, "--http" .. res.http_version)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kulala.nvim",
"version": "2.0.1",
"version": "2.1.0",
"scripts": {
"docs": "docsify serve docs"
},
Expand Down

0 comments on commit fb5396d

Please sign in to comment.