Skip to content

Commit 0aa1c42

Browse files
feat(config): ensure all options are working
1 parent 1be0231 commit 0aa1c42

File tree

16 files changed

+104
-31
lines changed

16 files changed

+104
-31
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ docgen:
1616
lemmy-help lua/rest-nvim/config/init.lua > doc/rest-nvim-config.txt
1717
lemmy-help lua/rest-nvim/cookie_jar.lua > doc/rest-nvim-cookies.txt
1818
lemmy-help lua/rest-nvim/script.lua > doc/rest-nvim-script.txt
19-
lemmy-help lua/rest-nvim/api.lua lua/rest-nvim/utils.lua > doc/rest-nvim-api.txt
19+
lemmy-help lua/rest-nvim/api.lua lua/rest-nvim/ui/result.lua lua/rest-nvim/utils.lua > doc/rest-nvim-api.txt
2020
lemmy-help lua/rest-nvim/client/curl/cli.lua lua/rest-nvim/client/curl/utils.lua > doc/rest-nvim-client-curl.txt

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ CLI. For more information on this, please see this [blog post](https://amartin.c
4444

4545
> [!NOTE]
4646
>
47-
> rest.nvim requires Neovim >= 0.9.2 to work.
47+
> rest.nvim requires Neovim >= 0.10.1 to work.
4848
4949
### Dependencies
5050

lua/rest-nvim/client/curl/cli.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ end
7575
function parser.parse_verbose_line(line)
7676
local prefix, str = line:match("(.) ?(.*)")
7777
if not prefix then
78-
-- TODO: parse failed
78+
log.error("Error while parsing verbose curl output:\n" .. line)
7979
return
8080
end
8181
return {

lua/rest-nvim/context.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ local rest_variables = {
3030
return os.date("%Y-%m-%d") --[[@as string]]
3131
end,
3232
["$timestamp"] = function()
33-
return string(os.time()) or ""
33+
return tostring(os.time()) or ""
3434
end,
3535
["$randomInt"] = function()
36-
return string(math.random(0, 1000))
36+
return tostring(math.random(0, 1000))
3737
end,
3838
}
3939

lua/rest-nvim/dotenv.lua

+9-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,15 @@ end
3232
---@param path string file path of dotenv file
3333
---@param bufnr number? buffer identifier, default to current buffer
3434
function M.register_file(path, bufnr)
35-
-- TODO: validate file extension
35+
vim.validate({
36+
path = {
37+
path,
38+
function (p)
39+
return vim.endswith(p, ".env") or vim.endswith(p, ".json")
40+
end,
41+
"`.env` or `.json` filetype"
42+
}
43+
})
3644
bufnr = bufnr or 0
3745
vim.b[bufnr]._rest_nvim_env_file = path
3846
vim.notify("[rest.nvim] Env file '" .. path .. "' has been registered")

lua/rest-nvim/parser/init.lua

-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ function parser.parse_body(body_node, source, context)
115115
end
116116
elseif body.__TYPE == "external" then
117117
local path = assert(get_node_field_text(body_node, "path", source))
118-
-- TODO: make sure expand happens in `.http` file
119118
path = vim.fs.normalize(vim.fs.joinpath(vim.fn.expand("%:h"), path))
120119
body.data = {
121120
name = get_node_field_text(body_node, "name", source),

lua/rest-nvim/request.lua

+9-9
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ local function run_request(req)
5252
---@cast res rest.Response
5353
logger.info("request success")
5454

55-
-- run request handler scripts
56-
vim.iter(req.handlers):each(function (f) f(res) end)
57-
logger.info("handler done")
58-
59-
-- update cookie jar
60-
jar.update_jar(req.url, res)
61-
6255
-- NOTE: wrap with schedule to do vim stuffs outside of lua callback loop (`on_exit`
6356
-- callback from `vim.system()` call)
6457
vim.schedule(function ()
58+
-- run request handler scripts
59+
vim.iter(req.handlers):each(function (f) f(res) end)
60+
logger.info("handler done")
61+
62+
-- update cookie jar
63+
jar.update_jar(req.url, res)
64+
6565
_G.rest_request = req
6666
_G.rest_response = res
6767
vim.api.nvim_exec_autocmds("User", {
@@ -87,7 +87,7 @@ function M.run()
8787
return
8888
end
8989
local ctx = parser.create_context(0)
90-
if vim.b._rest_nvim_env_file then
90+
if config.env.enable and vim.b._rest_nvim_env_file then
9191
ctx:load_file(vim.b._rest_nvim_env_file)
9292
end
9393
local req = parser.parse(req_node, 0, ctx)
@@ -112,7 +112,7 @@ function M.run_by_name(name)
112112
return
113113
end
114114
local ctx = parser.create_context(0)
115-
if vim.b._rest_nvim_env_file then
115+
if config.env.enable and vim.b._rest_nvim_env_file then
116116
ctx:load_file(vim.b._rest_nvim_env_file)
117117
end
118118
local req = parser.parse(req_node, 0, ctx)

lua/rest-nvim/ui/result.lua

+16-2
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,15 @@ local panes = {
5959
if data.response then
6060
table.insert(lines, ("%d %s %s"):format(data.response.status.code, data.response.status.version, data.response.status.text))
6161
local content_type = data.response.headers["content-type"]
62-
local body = res.try_format_body(content_type and content_type[1], data.response.body)
6362
table.insert(lines, "")
6463
table.insert(lines, "#+RES")
64+
---@type string[]
65+
local body
66+
if config.response.hooks.format then
67+
body = res.try_format_body(content_type and content_type[1], data.response.body)
68+
else
69+
body = vim.split(data.response.body, "\n")
70+
end
6571
vim.list_extend(lines, body)
6672
table.insert(lines, "#+END")
6773
else
@@ -138,6 +144,9 @@ winbar = winbar .. "%=%<"
138144
winbar = winbar .. "%{%v:lua.require('rest-nvim.ui.result').stat_winbar()%}"
139145
winbar = winbar .. " %#RestText#|%#Normal# "
140146
winbar = winbar .. "%#RestText#Press %#Keyword#?%#RestText# for help%#Normal# "
147+
148+
---Winbar component showing response statistics
149+
---@return string
141150
function ui.stat_winbar()
142151
local content = ""
143152
if not data.response then
@@ -169,7 +178,9 @@ local group = paneui.create_pane_group("rest_nvim_result", panes, {
169178
end, { buffer = self.bufnr })
170179
vim.keymap.set("n", "?", help.open, { buffer = self.bufnr })
171180
vim.bo[self.bufnr].filetype = "rest_nvim_result"
172-
utils.nvim_lazy_set_wo(self.bufnr, "winbar", winbar)
181+
if config.ui.winbar then
182+
utils.nvim_lazy_set_wo(self.bufnr, "winbar", winbar)
183+
end
173184
end,
174185
})
175186

@@ -189,6 +200,7 @@ vim.api.nvim_set_hl(0, "RestPaneTitle", {
189200
underline = true,
190201
})
191202

203+
---Check if UI window is shown in current tabpage
192204
---@return boolean
193205
function ui.is_open()
194206
local winnr = vim.iter(vim.api.nvim_tabpage_list_wins(0)):find(function(id)
@@ -203,11 +215,13 @@ function ui.enter(winnr)
203215
group:enter(winnr)
204216
end
205217

218+
---Clear the UI
206219
function ui.clear()
207220
data = {}
208221
group:render()
209222
end
210223

224+
---Update data and rerender the UI
211225
---@param new_data rest.UIData
212226
function ui.update(new_data)
213227
data = vim.tbl_deep_extend("force", data, new_data)

spec/api_spec.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ describe("parser", function()
3333
}, parser.parse(req_node, source))
3434
end)
3535
it("parse from http file", function()
36-
local source = open "spec/api.http"
36+
local source = open "spec/examples/basic_get.http"
3737
local _, tree = utils.ts_parse_source(source)
3838
local req_node = assert(tree:root():child(0))
3939
assert.same({
40-
name = "api#1",
40+
name = "basic_get#1",
4141
context = context:new(),
4242
method = "GET",
4343
url = "https://api.github.com/users/boltlessengineer",

spec/client_curl_cli_spec.lua renamed to spec/client/curl/cli_spec.lua

+31-1
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,9 @@ describe("curl cli response parser", function()
126126
}, response)
127127
end)
128128
end)
129+
130+
-- don't run real request on test by default
129131
describe("curl cli request", function()
130-
-- TODO: don't send actual request on test
131132
nio.tests.it("basic GET request", function()
132133
local response = curl
133134
.request({
@@ -151,4 +152,33 @@ describe("curl cli request", function()
151152
-- HACK: have no idea how to make sure it is table<string,string>
152153
assert.are_table(response.headers)
153154
end)
155+
nio.tests.it("basic POST request", function()
156+
local response = curl
157+
.request({
158+
context = Context:new(),
159+
url = "https://reqres.in/api/register",
160+
handlers = {},
161+
headers = {
162+
["content-type"] = { "application/json" }
163+
},
164+
cookies = {},
165+
method = "POST",
166+
body = {
167+
__TYPE = "json",
168+
data = '{ "email": "[email protected]", "password": "pistol" }',
169+
}
170+
})
171+
.wait()
172+
assert.same(
173+
'{"id":4,"token":"QpwL5tke4Pnpja7X4"}',
174+
response.body
175+
)
176+
assert.same({
177+
version = "HTTP/2",
178+
code = 200,
179+
text = ""
180+
}, response.status)
181+
-- HACK: have no idea how to make sure it is table<string,string>
182+
assert.are_table(response.headers)
183+
end)
154184
end)
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
GET https://api.github.com/users/boltlessengineer
22
user-agent: neovim
3-
###
4-
GET https://api.github.com/users/NTBBloodbath
5-
user-agent: neovim
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
### post-request script
2+
GET https://jsonplaceholder.typicode.com/posts/3
3+
4+
> {%
5+
local body = vim.json.decode(response.body)
6+
client.global.set("postId", body.id)
7+
%}
8+
9+
### run request with variable
10+
11+
GET https://jsonplaceholder.typicode.com/posts/{{postId}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### run request with variable set via pre-request script
2+
3+
< {%
4+
request.variables.set("postId", "3")
5+
%}
6+
7+
GET https://jsonplaceholder.typicode.com/posts/{{postId}}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
POST https://reqres.in/api/users
2+
Content-Type: application/json
3+
4+
{
5+
"userid" : "{{$uuid}}",
6+
"time" : "{{$timestamp}}",
7+
"randomValue": "{{$randomInt}}",
8+
"name": "morpheus",
9+
"job": "leader",
10+
"array": ["a", "b", "c"],
11+
"object_ugly_closing": {
12+
"some_key": "some_value"
13+
}
14+
}

spec/test_server/post_json.http

-4
This file was deleted.

spec/test_server/post_json.json

-3
This file was deleted.

0 commit comments

Comments
 (0)