Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lua/rest-nvim/curl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ M.get_or_create_buf = function()
return new_bufnr
end

local function create_callback(method, url)
local function create_callback(method, url, req_var)
return function(res)
if res.exit ~= 0 then
log.error("[rest.nvim] " .. utils.curl_error(res.exit))
Expand Down Expand Up @@ -133,6 +133,13 @@ local function create_callback(method, url)
}, false, {})
end
end
-- check if the response is a json
-- parse the json response and store the data on memory
if content_type == "json" and req_var ~= "" then
local req_var_store = vim.api.nvim_get_var('req_var_store')
req_var_store[req_var] = vim.json.decode(res.body)
vim.api.nvim_set_var('req_var_store', req_var_store)
end

-- append response container
res.body = "#+RESPONSE\n" .. res.body .. "\n#+END"
Expand Down Expand Up @@ -210,7 +217,7 @@ M.curl_cmd = function(opts)
vim.api.nvim_echo({ { "[rest.nvim] Request preview:\n", "Comment" }, { curl_cmd } }, false, {})
return
else
opts.callback = vim.schedule_wrap(create_callback(opts.method, opts.url))
opts.callback = vim.schedule_wrap(create_callback(opts.method, opts.url, opts.req_var))
curl[opts.method](opts)
end
end
Expand Down
2 changes: 2 additions & 0 deletions lua/rest-nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local rest = {}
local Opts = {}
local LastOpts = {}

vim.api.nvim_set_var("req_var_store", { __loaded = true })
rest.setup = function(user_configs)
config.set(user_configs or {})
end
Expand Down Expand Up @@ -33,6 +34,7 @@ rest.run = function(verbose)
bufnr = result.bufnr,
start_line = result.start_line,
end_line = result.end_line,
req_var = result.req_var,
}

if not verbose then
Expand Down
28 changes: 28 additions & 0 deletions lua/rest-nvim/request/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,19 @@ local function start_request()
return vim.fn.search("^GET\\|^POST\\|^PUT\\|^PATCH\\|^DELETE", "cbn", 1)
end

-- request_var will find the request variable if it exists, e.g.
-- ```
-- #@foo
-- GET http://localhost:8081/bar
-- ````
-- of the current request and returns the linenumber of the found request variable
-- the request variable is defined as the variable that starts with '#@'
-- one line above the request line in the example the variable would be 'foo'
-- @param url_line The request line
local function request_var(url_line)
return vim.fn.search("^#@", "cbn", url_line - 1)
end

-- end_request will find the next request line (e.g. POST http://localhost:8081/foo)
-- and returns the linenumber before this request line or the end of the buffer
-- @param bufnr The buffer nummer of the .http-file
Expand Down Expand Up @@ -218,6 +231,14 @@ local function parse_url(stmt)
}
end

-- parse_req_var returns a string with the name of the request variable, e.g.
-- #@foo -> parse_req_var -> foo
-- @param stmt the request variable (#@foo)
local function parse_req_var(stmt)
local parsed = stmt:sub(3)
return parsed
end

local M = {}
M.get_current_request = function()
local curpos = vim.fn.getcurpos()
Expand All @@ -231,6 +252,12 @@ M.get_current_request = function()

local parsed_url = parse_url(vim.fn.getline(start_line))

local req_var_line = request_var(start_line)
local parsed_req_var_str = ""
if req_var_line ~= 0 then
parsed_req_var_str = parse_req_var(vim.fn.getline(req_var_line))
end

local headers, headers_end = get_headers(bufnr, start_line, end_line)

local curl_args, body_start = get_curl_args(bufnr, headers_end, end_line)
Expand Down Expand Up @@ -265,6 +292,7 @@ M.get_current_request = function()
bufnr = bufnr,
start_line = start_line,
end_line = end_line,
req_var = parsed_req_var_str,
}
end

Expand Down
45 changes: 44 additions & 1 deletion lua/rest-nvim/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@ M.read_document_variables = function()

for node in root:iter_children() do
local type = node:type()

if type == "header" then
local name = node:named_child(0)
local value = node:named_child(1)
variables[M.get_node_value(name, bufnr)] = M.get_node_value(value, bufnr)
-- check if variable has assigned other variable, e.g. foo: {{bar}}
local value_processed = M.replace_req_varibles(M.get_node_value(value, bufnr))
variables[M.get_node_value(name, bufnr)] = value_processed
elseif type ~= "comment" then
break
end
Expand All @@ -131,6 +134,46 @@ M.read_variables = function()
return vim.tbl_extend("force", first, second, third)
end

-- replaces the variables that have assigned another variable, e.g. foo: {{bar}} or foo: {{bar.baz}}
-- if so, then replace {{bar}} or {{bar.baz}} with the proper value else return the same string
-- only works if `bar` is a key in req_var_store
-- @param value_str the value to evaluate
M.replace_req_varibles = function(value_str)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lacks a a

-- first check if 'value_str' has the form {{bar}} if not then return them as is
local match = string.match(value_str, "{{[^}]+}}")
if match == nil then
return value_str
end

match = match:gsub("{", ""):gsub("}", "")

-- split the value_str, e.g. 'foo.bar.baz' -> {'foo', 'bar', 'baz'}
local splitted_values = {}
for var in match:gmatch("([^.]+)") do
table.insert(splitted_values, var)
end

local result = vim.api.nvim_get_var('req_var_store')
if not result.__loaded then
error(
string.format(
"rest-nvim's global JSON variable has been unset, it is needed to get %s from there",
match
)
)
end
for _, val in pairs(splitted_values) do
if result[val] then
result = result[val]
else
result = ""
break
end
end

return result
end

-- replace_vars replaces the env variables fields in the provided string
-- with the env variable value
-- @param str Where replace the placers for the env variables
Expand Down
14 changes: 14 additions & 0 deletions tests/request_vars/basic_usage.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
page: {{response.page}}
url: {{response.support.url}}

# normal var
foo: 4

#@response
GET https://reqres.in/api/users?page=5

GET https://reqres.in/api/users?page={{foo}}

GET https://reqres.in/api/users?page={{page}}

GET {{url}}